When you sit down to write a program, generally you have some kind of idea for which you’d like to write a program for. New programmers often have trouble figuring out how to convert that idea into actual code. But it turns out, you have many of the problem solving skills you need already, acquired from everyday life.
当你坐下来写一个程序时,通常你会有一些完成这个程序的想法。新程序员常常很难弄清楚如何将这个想法转换成实际的代码。但事实证明,你已经具备了许多你需要的解决问题的技能,这些技能都是从日常生活中获得的。
The most important thing to remember (and hardest thing to do) is to design your program before you start coding. In many regards, programming is like architecture. What would happen if you tried to build a house without following an architectural plan? Odds are, unless you were very talented, you’d end up with a house that had a lot of problems: walls that weren’t straight, a leaky roof, etc… Similarly, if you try to program before you have a good game-plan moving forward, you’ll likely find that your code has a lot of problems, and you’ll have to spend a lot of time fixing problems that could have been avoided altogether with a little thinking ahead.
要记住的最重要的事情(也是最难做的事情)是在开始编码之前设计程序。在许多方面,编程就像建筑设计。如果你不按建筑计划建房子会怎么样?很有可能,除非你很有天赋,否则你最终会得到一栋有很多问题的房子:墙壁不直,屋顶漏水等等……同样,如果你在有一个好的游戏计划之前尝试编程,你可能会发现你的代码有很多问题你必须花大量的时间来解决一些问题,这些问题本来是可以避免的。
A little up-front planning will save you both time and frustration in the long run.
从长远来看,提前做一点计划可以节省你的时间和挫败感。
1 Outlining your main function(勾勒你的main()函数)
Outline your main program. Don’t worry about inputs and outputs for the time being.
勾勒主程序。暂时不要担心输入和输出。
int main()
{
// doBedroomThings();
// doBathroomThings();
// doBreakfastThings();
// doTransportationThings();
return 0;
}
Or in the case of the calculator:
int main()
{
// Get first number from user
// getUserInput();
// Get mathematical operation from user
// getMathematicalOperation();
// Get second number from user
// getUserInput();
// Calculate result
// calculateResult();
// Print result
// printResult();
return 0;
}
Note that if you’re going to use this “outline” method for constructing your programs, your functions won’t compile because the definitions don’t exist yet. Commenting out the function calls until you’re ready to implement the function definitions is one way to address this (and the way we’ll show here). Alternatively, you can stub out your functions (create placeholder functions with empty bodies) so your program will compile.
请注意,如果您要使用这个“大纲”方法来构建程序,您的函数将无法编译,因为定义还不存在。在您准备好实现函数定义之前注释掉函数调用是解决这个问题的一种方法(以及我们将在这里展示的方法)。或者,您可以删除函数(创建带有空主体的占位符函数),这样您的程序将被编译。
2 Implement each function(实现每一个函数)
In this step, for each function, you’ll do three things:
2.1 Define the function prototype (inputs and outputs)
2.2 Write the function
2.3 Test the function
If your functions are granular enough, each function should be fairly simple and straightforward. If a given function still seems overly-complex to implement, perhaps it needs to be broken down into subfunctions that can be more easily implemented (or it’s possible you did something in the wrong order, and need to revisit your sequencing of events).
如果您的函数足够精细,那么每个函数都应该相当简单和直接。如果一个给定的函数在实现时仍然显得过于复杂,那么可能需要将它分解为更容易实现的子函数(或者可能是您按错误的顺序执行了某些操作,并且需要重新查看事件的顺序)。
Let’s do the first function from the calculator example:
#include <iostream>
// Full implementation of the getUserInput function
int getUserInput()
{
std::cout << "Enter an integer ";
int input{ 0 };
std::cin >> input;
return input;
}
int main()
{
// Get first number from user
int value = getUserInput(); // Note we’ve included code here to test the return value!
std::cout << value;
// Get mathematical operation from user
// getMathematicalOperation();
// Get second number from user
// getUserInput();
// Calculate result
// calculateResult();
// Print result
// printResult();
return 0;
}
First, we’ve determined that the getUserInput function takes no arguments, and will return an int value back to the caller. That gets reflected in the function prototype having a return value of int and no parameters. Next, we’ve written the body of the function, which is a straightforward 4 statements. Finally, we’ve implemented some temporary code in function main to test that function getUserInput (including its return value) is working correctly.
首先,我们确定getuserinput函数不接受参数,并将返回一个int值给调用方。体现在返回值为int且没有参数的函数原型中。接下来,我们编写了函数体,这是一个简单的4条语句。最后,我们在函数main中实现了一些临时代码,以测试函数getuserinput(包括其返回值)是否正常工作。
We can run this program many times with different input values and make sure that the program is behaving as we expect at this point. If we find something that doesn’t work, we know the problem is in the code we’ve just written. Once we’re convinced the program is working as intended up to this point, we can remove the temporary testing code, and proceed to implementation of the next function (function getMathematicalOperation).
我们可以用不同的输入值运行这个程序很多次,并确保程序在这一点上的行为符合我们的预期。如果我们发现一些不起作用的东西,我们知道问题出在我们刚刚编写的代码中。一旦我们确信程序在这一点上按预期工作,我们就可以删除临时测试代码,并继续执行下一个函数(函数getMathematicalOperation)。
Remember: Don’t implement your entire program in one go. Work on it in steps, testing each step along the way before proceeding.
记住:不要一次完成整个程序。分步骤进行,在继续之前测试每一步。
3 Final testing(完整测试)
Once your program is “finished”, the last step is to test the whole program and ensure it works as intended. If it doesn’t work, fix it.
一旦你的程序“完成”,最后一步是测试整个程序并确保它按预期工作如果不起作用,就把它修好。
4 Words of advice when writing programs(一些建议)
4.1 Keep your programs simple to start.
4.2 Focus on one area at a time.
4.3 Test each piece of code as you go.
4.4 Don’t invest in perfecting early code.
4.1 开始时让你的程序保持简单。
4.2 一次只关注一个领域。
4.3 测试每一段代码。
4.4 早期代码不要太过于追求完美。
Most new programmers will shortcut many of these steps and suggestions (because it seems like a lot of work and/or it’s not as much fun as writing the code). However, for any non-trivial project, following these steps will definitely save you a lot of time in the long run. A little planning up front saves a lot of debugging at the end.
大多数新程序员会简化这些步骤和建议(因为这看起来像是很多工作和/或不像编写代码那么有趣)然而,对于任何非琐碎的项目,从长远来看,遵循这些步骤肯定会节省很多时间。一个小小的预先计划节省了大量的调试在最后。
The good news is that once you become comfortable with all of these concepts, they will start coming more naturally to you. Eventually you will get to the point where you can write entire functions without any pre-planning at all.
好消息是,一旦你对所有这些概念都感到满意,它们就会更自然地出现在你面前。最终,您将到达这样一个点:您可以编写整个函数而无需任何预先计划。
可用于练习的小实例(制作年历):

-End-