15个编程好习惯.doc_第1页
15个编程好习惯.doc_第2页
15个编程好习惯.doc_第3页
15个编程好习惯.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

哈尔滨天一时代科技开发有限公司推荐文档2011070115个编程好习惯 (15 Good Computer Programming Habits)编者按:这是国外程序员Al katib总结的一些编程习惯。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.使用循环和条件语句时,先把左右括号对应起来,然后再在里面写其他语句。也就是:代码:1 for(int i=0;i10;i+) 2 4 printf(“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 Habits1. Before sitting down for coding, you must have formal or a paper-napkin design of the solution to be coded. Never start coding without any design unless the code is trivial one.2. Good code documentation is as important as good knowledge of a programming language. Write brief logic for each major block of your code as comments in source code file itself. Its good to mention creation and modification dates of your program along-with why modification was required.3. Maintaining versions of your program is another important task. Some present-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 file program.c which is used by other project team members also. I copy this file as program.c.old as backup and make another copy as program.c.wrk where I do modifications. When modifications are successfully compiled, replace program.c with.wrk file.You can also append a date or some explanation phrase to your program versions like program260505.c or programReadFnWrking.c.4. If your project contains multiple source files then maintain a README file stating purpose of each source files, data files, intermediate and log files (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. May be your are using single equal i.e. “=” instead of “=” in the 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 it will be detected at compilation time only as an error.6. While using loops and conditional statements, always first put closing braces corresponding opening braces and then write the inner statements i.e.1) for(int i=0;i10;i+)2) 4) printf(“i=%dn”,i);3) The numbers at the starting of each line indicate sequence of writing loop code.7. Avoid using magic numbers. For example, instead of writingcircleArea = 3.14 * pow(radius,2);use following code:#define PI 3.14circleArea = 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 cryptic short name. In a hurry, we may use short variable names but the time saved leads to double wastage of time later when you guess for what that short variable 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 function that displays debugging information passed to it. When your final version is ready, simply comment the internals of this function. So, this requires changes only at one place.10. Once you are done with coding, start optimizing your code. Some of the variables 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. Sound knowledge of compiler can also help in optimizing the code further.11. With good knowledge of your operating system and hardware, you can improve 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 various folders like SOURCE, HEADERS, MAKE, EXES etc.14. Study the code written by others. This will bring to

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论