15个编程好习惯
1.动手编码之前,你需要对要编码实现的解决方案有一个正式的或粗略的设计。永远不要在没有任何设计的前提下就开始编码,除非所编代码不重要。2.优秀的代码文档跟编程语言知识一样重要。在代码源文件中,为每个主要的代码段添加注释,解释代码的基本逻辑。最好注明程序的构建和修改日期,以及修改的原因也是非常有必要的。3.维护程序的各个版本同样重要。当前有些编程工具都自带一个版本管理工具。无论你什么时候改变自己的程序,它们都会将其保存为.bak文件。 我的方法是为每个程序维护三个不同的版本。比如说,我有一个名为program.c的文件,这个文件同时也被其他项目组成员使用。我把这个文件复制为program.c.old作为备份文件,并且当我修改时,我会备份另一个名为program.c.wrk的副本文件。当成功完成修改时替换program.c.wrk文件。 你还可以给自己的程序版本添加一个日期或一些注释,像program260505.c或programReadFnWrking.c。4.如果工程包含多个源文件,则创建一个README文件,注明每个源文件、数据文件、临时文件以及日志文件(如果有的话)的作用。你还可以注明编译和运行步骤。5.有时候,你一定想知道为什么IF语句没有得到预想的结果。可能你使用的是等号,也就是“=”,而不是条件判定符号“==”。一个比较好的办法是用相反的顺序写条件语句。因此,你的条件语句应该如下:if(10==i)…因此,如果你错误地写成了单个等于号,在编译的时候也能检查出来并报错。6.使用循环和条件语句时,先把左右括号对应起来,然后再在里面写其他语句。也就是:
代码:
1for(inti=0;i<10;i++)
2{
4printf(“i=%dn”,i);
3}
注:每一行开头的数字表明写循环代码的顺序。7.避免使用幻数(magic numbers)。例如,不要写
代码:
circleArea = 3.14 * pow(radius,2);
而要使用如下代码:
代码:
#define PI 3.14
circleArea = PI * pow(radius,2);
8.使用有意义的变量和函数名称。例如,使用‘radius’来代替圆的半径,而不是用‘r’来表示。同样,函数名‘calculateArea’要比其他任何隐晦的缩写要好得多。匆忙之下,我们也许会使用缩写的变量名,但一开始节省时间的话,之后会浪费更多的时间,去猜测缩写变量名代表什么。(编注:)9.为后面的调试使用打印语句,这是个好习惯。但是,当完成最后代码后,去掉这些语句,有时也是一项危险的任务。添加一个方法,用于输出调试信息。当最终版本生成时,只要把这个方法注释掉就行。因此,只在一个地方做修改就可以了。10.代码编写完之后,开始优化代码。之前声明的一些变量,现在可能没用了。同样,并不依赖循环的一些声明可以移到循环模块之外去。扎实的编译知识同样会对以后的代码优化有所帮助。11.对自己的操作系统和硬件要有足够的了解,你可以从资源占用等方面提升程序的性能。12.编写代码时要合理使用缩进,以使代码清晰可读。13.把项目文件放到SOURCE、HEADERS、MAKE、EXES等不同的文件夹中。14.研究别人编写的代码。这可以让你学习到新的编程技术,以及他们解决和你相同的任务时所使用的方法。15.最后一条(但不是最不重要的一条),备份源代码文件,这样当硬盘出错或相同的问题发生时,不至于前功尽弃。 附加:补充一条,坚持使用一种命名模式。如果你打算用匈牙利命名法,那就坚持并广泛使用,否则将适得其反。
15 Good Computer Programming Habits
1. Before sitting down for coding, you must have formal or a paper-napkindesign of the solution to be coded. Never start coding without any designunless the code is trivial one.
2. Good code documentation is as important as good knowledge of aprogramming language. Write brief logic for each major block of your code ascomments in source code file itself. Its good to mention creation andmodification dates of your program along-with why modification was required.
3. Maintaining versions of your program is another important task. Somepresent-day programming tools already have a built-in version management.Whenever you make any change to your program, they save its copy as.bak file.
My approach is to maintain 3 versions of a program. Say, I have a fileprogram.c which is used by other project team members also. I copy this file asprogram.c.old as backup and make another copy as program.c.wrk where I domodifications. When modifications are successfully compiled, replace program.cwith.wrk file.
You can also append a date or some explanation phrase to your programversions like program260505.c or programReadFnWrking.c.
4. If your project contains multiple source files then maintain a READMEfile stating purpose of each source files, data files, intermediate and logfiles (if any). You may also mention the compilation and execution steps.
5. Ever wondered why your IF statement is not working as it should do. Maybe your are using single equal i.e.“=” instead of “==” inthe condition check. A good approach is to write condition in reverse order.So, your condition should read something like this:
if ( 10==i)…. So, if you put single equal sign by mistake then itwill be detected at compilation time only as an error.
6. While using loops and conditional statements, always first put closingbraces corresponding opening braces and then write the inner statements i.e.
1) for(int i=0;i<10;i++)
2) {
4) printf(“i=%dn”,i);
3) }
The numbers at the starting of each line indicate sequence of writing loopcode.
7. Avoid using magic numbers. For example, instead of writing
circleArea = 3.14 * pow(radius,2);
use following code:
#define PI 3.14
circleArea = PI * pow(radius,2);
8. Use meaningful variable and function names. For e.g. instead of using‘r’ use ‘radius’ to represent radius of a circle. Similarly, function name ‘calculateArea’ is better than any crypticshort name. In a hurry, we may use short variable names but the time savedleads to double wastage of time later when you guess for what that shortvariable name stands for.
9. Using print statements for later debugging is a good habit. But,removing them when final code is ready is, sometimes, a risky task. So, make a functionthat displays debugging information passed to it. When your final version isready, simply comment the internals of this function. So, this requires changesonly at one place.
10. Once you are done with coding, start optimizing your code. Some of thevariables you declared earlier may not be of use at this stage. Similarly,statements which are not loop dependent can be moved out of loop block. Soundknowledge of compiler can also help in optimizing the code further.
11. With good knowledge of your operating system and hardware, you canimprove performance of your program in terms of resource requirements etc.
12. Always indent your code for clarity and easy readability.
13. You will also like the idea of organizing project files in into variousfolders like SOURCE, HEADERS, MAKE, EXES etc.
14. Study the code written by others. This will bring to you newprogramming techniques and what approach they have followed for the task forwhich you have also coded.
15. Last but not least important, take backup of your source-code files sothat your effort don’t go waste if hard-disk crashes ora similar mishappening occurs.