Programming style / The Elements of Programming
Just by listening to general principles, we wanted a good grasp of programming, which is impossible.
I. Introduction
Write simple programs - do not play smart
Second, the expression
Simple and direct expression meaning you have to say
Use the library functions
Avoid the use of temporary variables
Code should be clear, not to "efficiency" sacrifice readability
Let the machine do the dirty work
Alternative repeated calls by the function expression
Bracketed avoid ambiguity
Select the variable name will not be confused
Avoid using Fortran arithmetic IF
Avoid unnecessary branch
Use a good characteristic language, avoiding the use of undesirable characteristics
Do not use the conditional branch instead of a logical expression
With "telephone test" to check the readability
Third, the control structure
Use DO-WHILE statement to define the feeding group
IF-ELSE stressed with only one of two operations to be executed
With DO-WHILE loop to emphasize the existence of
Make sure your program is top-down reading
Use IF ... ELSE IF ... ELSE IF ... ELSE to achieve multiple branches
Using basic control flow constructs
Write your code with the pseudo-language easy to understand, and then translated into the language you need
Avoid using empty THEN-IF and ELSE
Avoid the use of ELSE GOTO and ELSE RETURN
To be willing to make the judgment immediately associated operations
Using the array to avoid duplication control flow
Selection data representation can be simplified procedure
Do not stop at the first pass of the draft Code
Fourth, the program structure
Modular, use subroutines
So that the coupling between modules becomes visible
Each module should do one thing
Each module ensure good hiding something
To build data-driven program structure
Do not repair bad code - rewrite it
Block write and test large program
For recursively defined data structure using a recursive procedure
Fifth, the input and output
Check the legality and legitimacy input
Ensure that the input data does not restrict the program
End of file or end use of the symbol flag to terminate the input, do not allow the user to count
Identify illegal input data, correct it if possible
Using the same forms processing end condition file
The input data is easy to prepare, and make the output data is self-explanatory
Use a unified input format
So that the input data is easy proofreading
Ken can choose to do free-form input
Input evident meaning, specify a default value, the two or more output
Limitations in the input and output subroutine
Six Common Mistakes
Make sure that all the variables are initialized before use
Do not stop at a bug on
Use the debugger compiler
在DATA语句或INITIAL属性初始化常量,用可执行语句初始化变量
小心“差一”错误
要注意对不等式进行正确的分支
避免循环有多个出口
确保你的代码巧妙地“不做事情”
在边界上测试程序
预防性编程
10.0乘以0.1不等于1.0
不要比较浮点数是否相等
七、效率和测试工具
先做对,再做快
在提高程序运行速度时,要保持其正确性
先把程序改得简介,再提高其运行速度
不要为了“效率”上的蝇头小利而牺牲程序的简洁性
让编译器执行平凡优化
不要勉强的服用代码,应该进行改变
保证特殊情况真的有特殊性
保持简单性,反而会更快
不要为了提高速度而画蛇添足——去寻找更好的算法
在程序中放置测试语句 ,“增效”之前先执行测算
八、文档
确保注释和代码一致
不要注释复述代码做的事情,每个注释都要有实际意义
不要注释糟糕的代码——重写它
使用含有意义的变量名
使用含有意义的语句标签
程序的格式要有助于读者理解
用缩进来体现程序的逻辑结构
记录你的数据规划
不要过度注释