2022年15个编程好习惯j计算机IT工程师_第1页
2022年15个编程好习惯j计算机IT工程师_第2页
2022年15个编程好习惯j计算机IT工程师_第3页
2022年15个编程好习惯j计算机IT工程师_第4页
2022年15个编程好习惯j计算机IT工程师_第5页
全文预览已结束

下载本文档

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

文档简介

1、15 个编程好习惯 15 Good Computer Programming Habits编者按:这是国外程序员Al katib总结的一些编程习惯;1. 动手编码之前,你需要对要编码实现的解决方案有一个正式的或粗略的设计;永久不要在没有任何设计的前提下就开头编码,除非所编代码不重要;2. 优秀的代码文档跟编程语言学问一样重要;在代码源文件中,为每个主要的代码段添加注释,说明代码的基本规律;最好注明程序的构建和修改日期,以及修改的缘由也是非常有必要的;3. 保护程序的各个版本同样重要;当前有些编程工具都自带一个版本治理工具;无论你什么时候转变自己的程序,它们都会将其储存为 .bak 文件;我的方

2、法是为每个程序保护三个不同的版本;比如说,我有一个名为 program.c 的文件,这个文件同时也被其他项目组成员使用;我把这个文件复制为 program.c.old 作为备份文件,并且当我修改时,我会备份另一个名为 program.c.wrk 的副本文件;当胜利完成修改时替换 program.c.wrk 文件;你仍可以给自己的程序版本添加一个日期或一些注释,像 program260505.c 或programReadFnWrking.c ;4. 假如工程包含多个源文件,就创建一个README文件,注明每个源文件、数据文件、暂时文件以及日志文件 假如有的话 的作用;你仍可以注明编译和运行步骤;

3、5. 有时候,你肯定想知道为什么IF 语句没有得到预想的结果;可能你使用的是等号,也就是“=” ,而不是条件判定符号“=” ;一个比较好的方法是用相反的次序写条件语句;因此,你的条件语句应当如下:1 if10=i 因此,假如你错误地写成了单个等于号,在编译的时候也能检查出来并报 错;6. 使用循环和条件语句时,先把左右括号对应起来,然后再在里面写其他语句;也就是:代码 : 1 forint i=0;i10;i+ 2 4 printf“ i=%dn” ,i; 3 注:每一行开头的数字说明写循环代码的次序;7. 防止使用幻数(magic numbers );例如,不要写代码 : circleAre

4、a = 3.14 * powradius,2; 而要使用如下代码:代码 : #define PI 3.14 circleArea = PI * powradius,2; r 8. 使用有意义的变量和函数名称;例如,使用radius 来代替圆的半径,而不是用来表示;同样,函数名calculateArea要比其他任何隐晦的缩写要好得多;匆忙之下,我们或许会使用缩写的变量名,但一开头节约时间的话,之后会铺张更多的时间,去猜 测缩写变量名代表什么;(编注:)9. 为后面的调试使用打印语句,这是个好习惯;但是,当完成最终代码后,去掉这些 语句,有时也是一项危急的任务;添加一个方法,用于输出调试信息;当最

5、终版本生成时,只要把这个方法注释掉就行;因此,只在一个地方做修改就可以了;10. 代码编写完之后,开头优化代码;之前声明的一些变量,现在可能没用了;同样,并不依靠循环的一些声明可以移到循环模块之外去 化有所帮忙;扎实的编译学问同样会对以后的代码优能;11. 对自己的操作系统和硬件要有足够的明白,你可以从资源占用等方面提升程序的性12. 编写代码时要合理使用缩进,以使代码清楚可读;13. 把项目文件放到 SOURCE、HEADERS、MAKE、EXES等不同的文件夹中;14. 讨论别人编写的代码;这可以让你学习到新的编程技术,以及他们解决和你相同的任务时所使用的方法;15. 最终一条(但不是最不

6、重要的一条 问题发生时,不至于前功尽弃; ,备份源代码文件,这样当硬盘出错或相同的附加:补充一条,坚持使用一种命名模式;假如你准备用匈牙利命名法,那就坚持并广泛使用,否就将适得其反;15 Good Computer Programming Habits 1. 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 trivia

7、l 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.

8、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.

9、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. 3 You can also append a date or some explanation phrase to y

10、our 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 won

11、dered 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 m

12、istake 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 forint i=0;i10;i+ 2 4 printf“ i=%dn” ,i; 3 The numbers at the starting of eac

13、h line indicate sequence of writing loop code. 7. Avoid using magic numbers. For example, instead of writing circleArea = 3.14 * powradius,2; use following code: #define PI 3.14 circleArea = PI * powradius,2; 8. Use meaningful variable and function names. For e.g. instead of using r use radius to re

14、present radius of a circle. Similarly, function name calculateAreais 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 lat

15、er debugging is a good habit. But, removing them when final code is ready is, sometimes, a risky task. So, make a function that 4 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.

16、 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.

17、 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 yo

温馨提示

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

评论

0/150

提交评论