科技和互联网

写代码卡住了?五个步骤解决编程难题

我相信每个程序猿总有碰到写代码卡主的时候,如何解决问题是程序员的必备技能,学会一些解决问题的窍门也是必需的。这里在这里总结的五个步骤,既有科学性,也有经验之谈,是应对编程问题的相对实际的方法和步骤,也是让入门者快速进步,变得更专业的基本方法。
首先,自己要读懂问题,直到你认为能清楚的向别人解释问题,其次,别急着用机器自动方式去解决它,毕竟现代的IDE都提供了这个功能,自动除错,除了有极大的可能本身会犯错以外,最大的坏处是让你习惯急于求成,不知其所以然,你首先要做的是自己手动解决问题,手动排错,再次检查和思考,看能否有更简单的方式处理问题;再次,学会编写伪代码,无论你的编程经验多么的丰富,如果你能养成习惯写伪代码,基本上,你的最终代码将会更简洁高效;最后,把伪代码转换成真实代码,并且在这个过程中,不断提炼迭代,写出最终的专业代码。用这五个的步骤解决编程难题,让代码更专业。

原文链接:https://medium.com/

抄录:

Stuck on a Coding Problem? Here are 5 Steps to Solve It

Adrian Prieto

Solving problems is a programmer’s bread and butter. While everyone has their own tricks to employ when they’re stuck, I’ve personally found five surefire steps that, more likely than not, will help you solve any programming problems you encounter—and do it faster and more efficiently.
1. Read the problem several times until you can explain it to someone else

Read Read Read!
This is by far, the most important step. Read the problem several times until you fully understand it. If you don’t understand it, you simply won’t be able to solve it. And the best way to know if you understand the problem is by being able to explain it to someone else.
2. Solve the problem manually
Nothing can be automated that cannot be done manually!
Any code we write has a foundation: the manual process. So before you start automating, before you start writing code like a maniac, solve your problem manually first. That way, you’ll know exactly what you want to automate as you move forward. This will save you a lot of time.
Test your process with more than one input and some corner cases to validate it; pay close attention to every single step you take in your head and write them down—each step counts.
3. Make your manual solution better
Now, see if you can make your process better, if there is an easier way to do it, or if there are some steps you can cut to simplify it (like loops). This step is very important—remember that it’s much easier to reconstruct your process in your head than it is in your code.
At this point, you will be tempted to write some code. Don’t do it yet! We have one more step to cover, and I promise you it will make your final code easier to write.
4. Write pseudocode
“Pseudocode” is a detailed description of what a program must do; and writing it out will help you write every line of code needed in order to solve your problem.
Experienced programmers sometimes omit this step, but I can assure you: no matter how experienced you are, if you write some pseudocode, the process of writing your final code will be much easier since you only have to translate each line of pseudo code into actual code.
Here’s what that might look like for “square (n)”:

Now that we know exactly what our code is supposed to do, we have one more step… can you guess what it is?
5. Replace pseudocode with real code
Here’s the fun part. Now that you know for sure what your program should do, just write some code and test it. Remember, you can always make your code better along the way.
Let’s take a look at how we’d do this with our square example:

Then, we can further optimize it:

Following this five-step process has helped me out of so many programming binds. No matter how complex your problem is, I assure you, these steps will help you solve it in less time and with fewer headaches.
Note: If your problem is too complex, divide it into small problems; it’s a technique called “Divide and conquer”.

Tagged ,

我忍不住要留言