How to think like a programmer ?
If you are interested in programming, you should have heard the following passage: Everyone in this country should learn programming because it can teach you how to think. ——Steve Jobs
If you are interested in programming, you should have heard the following passage:
Everyone in this country should learn programming because it can teach you how to think. ——Steve Jobs
Why is this important?
The ability to solve problems is a basic skill. We are all constantly encountering problems, large and small. Sometimes, how do we face them? Very casual.
Unless you have your own system, the next way is probably your steps to solve the problem (this is how I started to learn programming):
- Try a solution.
- If it doesn't work, try another solution.
- If it doesn't work, repeat step 2 until you get lucky and finally try it out.
Look, sometimes you might be lucky. This is the worst way to solve the problem. And, it will waste you a lot, a lot of time.
Understanding the problem
You should understand the problem you are trying to solve. Many difficult questions are difficult because you do not understand the problem (this is why understanding the problem is the first step)
How can you know that you have fully understood the problem? It is when you can describe it in very straightforward terms. When you get stuck on a problem, try to explain the problem. Then you will find your logic flaws right away. Do you have the feeling of deja vu? Many programmers feel this way. This is why you have to write down the question, draw a diagram, or tell someone what it is (or something... some people will use a little yellow duck)
This is also something in Feynman's learning method, try to explain what you have learned like others.
If you can't simply describe something, then you don't understand it. —- Richard Feynman
Don't blindly solve the problem without planning. Plan your solution. There is nothing better than being able to write clear resolution steps.
In terms of programming, this means that you don't want to solve the problem directly from the beginning. Analyze the problem and process the information first.
To get a good plan, you should try to answer the following questions:
"Enter X and get Y what are the necessary steps?"
Note: Programmers have a very good tool to help them complete such plans-comments!
Break down the problem
Attention, this should be the most important of all issues. Don't try to solve a big problem directly. You will collapse. Instead, you should disassemble the problem into sub-problems that are easy to solve. Then, starting from the simplest, solve these sub-problems one by one. The simplest is that you can know the answer at a glance (or very close to the answer). Of course, the simplest also means that the solution to this problem does not depend on the solution of other problems.
After you solve each sub-problem, connect them. Connect the solutions to all of your sub-problems, and you will get the complete solution to the original problem. Congratulations!
Decomposing the problem is the cornerstone of solving the problem. Remember the following (if necessary, take a look at the following steps):
If I were to teach every programming beginner a problem-solving skill, it would be a "problem splitting skill". For example, suppose you are a beginner in programming and you are asked to program to read 10 numbers and find the third-largest number. Even if only a little basic programming language syntax is needed to solve this problem, it may be a relatively difficult task for a programming beginner.
If you get stuck, you should split the problem into simpler ones. Not to think about the third-largest number, but to think about finding the largest number of all numbers? Still too difficult? What about the largest of these three numbers? Or find the largest of the two numbers?
Keep splitting the problem until you know how to solve it or can write a solution directly. Then expand the problem and write the solution again until you get back to the problem you encountered in the first place.
—— V. Anton Spraul
By now, you might be thinking, "Hey... this looks interesting. But if I get stuck, even a sub-problem can't be solved?"
First, take a deep breath. Then, this is normal. Don't panic, everyone will have this situation.
The difference between good programmers is that they are full of curiosity when facing bugs or mistakes, rather than irritable.
In fact, you can try the next three steps when you encounter problems:
- Debugging: step by step to see where your program went wrong. Programmers call such a process debugging. (In fact, this is the full function of the debugger.)
- The art of debugging is to find out what you told the program to do instead of what you thought you told it to do. —— Andrew Singer
- Reassess: look at this issue from another angle. Can this be abstracted? Is there a general way to solve such problems?
- Sometimes we get lost in the details of the problem so that we ignore that the problem can be solved in a more general way. The most classic problem is the problem of summing a continuous sequence of numbers that Gauss encountered when he was a child: 1 + 2 + 3 +… + n. He soon realized that there is a general formula that can be used directly: n(n+1 )/2 to avoid so many additional calculations. —— C. Jordan Ball
- Note: Another good way to reassess is to start over. Delete everything and start again. This is not a joke, because you will be surprised to find that this is effective.
- Research: Wow, Google search, you read that right. No matter what problem you encounter, someone may have solved it. Find other people’s solutions. However, in fact, even if you have solved the problem, you should search to see how others have solved it. You can learn more from it.
Warning: Don't look for solutions to big problems. It should only look for solutions to sub-problems. why? Because it won't let you learn anything. If you can't learn anything, you are wasting your own time.