Brian Kernighan, P. J. Plauger
Introduction
介绍
- Write clearly - don't be too clever.
清楚地写-但是不要过于聪明
Expression
表达
- Say what you mean, simply and directly.
简单,明了地表达你想要表达的 - Use library functions.
使用库函数 - Avoid temporary variables.
避免使用临时变量 - Write clearly - don't sacrifice clarity for 'efficiency.'
写清楚-不要为了“效率”牺牲清晰性 - Let the machine do the dirty work.
让计算机去做复杂的事情 - Replace repetitive expressions by calls to a common function.
使用通用的函数调用来代替重复的代码 - Parenthesize to avoid ambiguity.
使用括号来避免歧义 - Choose variable names that won't be confused.
选择没有歧义的变量名称 - Avoid the Fortran arithmetic IF.
避免使用Fortran中的IF - Avoid unnecessary branches.
避免使用不必要的分支 - Use the good features of a language; avoid the bad ones.
使用一门语言中那些好的特性,避免使用那些坏的特性 - Don't use conditional branches as a substitute for a logical expression.
不要使用条件分支来代替逻辑表达式 - Use the 'telephone test' for readability.
使用telephone test来检查可读性
Control Structure
控制结构
- Use DO-END and indenting to delimit groups of statements.
使用DO-END和缩进来为你的一组表达式定界 - Use IF-ELSE to emphasize that only one of two actions is to be performed.
使用IF-ELSE来强调只会实现一到两种行为 - Use DO and DO-WHILE to emphasize the presence of loops.
使用DO和DO-WHILE来强调循环的存在 - Make your programs read from top to bottom.
使你的程序可以用从上到下的方式进行阅读 - Use IF, ELSE IF, ELSE IF, ELSE to implement multi-way branches.
使用IF, ELSE IF, ELSE IF, ELSE来实现多路分支 - Use the fundamental control flow constructs.
使用基本的控制流结构 - Write first in an easy-to-understand pseudo-language; then translate into whatever language you have to use.
先用简单可读的伪码编写代码,然后把它翻译成你想使用的那种语言 - Avoid THEN-IF and null ELSE.
避免THEN-IF和空ELSE - Avoid ELSE GOTO and ELSE RETURN.
避免使用ELSE GOTO和ELSE RETURN - Follow each decision as closely as possible with its associated action.
使每个决策尽可能和相联系的动作挨在一起 - Use data arrays to avoid repetitive control sequences.
使用数组来避免反复的控制序列 - Choose a data representation that makes the program simple.
采用能够使你程序简单的数据表示方法 - Don't stop with your first draft.
不要止步于你的第一版程序
Program Structure
程序结构
- Modularize. Use subroutines.
模块化,使用子例程 - Make the coupling between modules visible.
使模块间的耦合可见 - Each module should do one thing well.
每个模块只做好一件事 - Make sure every module hides something.
确保每个模块都隐藏了些东西 - Let the data structure the program.
让数据将程序变得结构化 - Don't patch bad code - rewrite it.
不要修补糟糕的代码-重写吧 - Write and test a big program in small pieces.
从小的模块开始编写和测试大的程序 - Use recursive procedures for recursively-defined data structures.
为递归的数据结构使用递归的过程
Input and Output
输入和输出
- Test input for validity and plausibility.
测试输入的有效性和合理性 - Make sure input cannot violate the limits of the program.
保证输入不会违反程序的内在限制 - Terminate input by end-of-file or marker, not by count.
以文件结束或者是标志来结束输入,而不是用计数的方式 - Identify bad input; recover if possible.
识别坏的输入,如果可能的话恢复它 - Treat end of file conditions in a uniform manner.
以统一的方式来处理文件结束状态 - Make input easy to prepare and output self-explanatory.
使输入准备简单化,使输出可以自我解释 - Use uniform input formats.
使用统一的输入格式 - Make input easy to proofread.
使输入能够轻易被校正 - Use free-form input when possible.
尽可能使用无格式的输入 - Use self-identifying input. Allow defaults. Echo both on output.
使用自我识别的输入,允许默认情况,在输出中现实它们 - Localize input and output in subroutines.
将输入和输出局部化于子例程
Common Blunders
常见错误
- Make sure all variables are initialized before use.
确保所有的变量在使用前都被初始化 - Don't stop at one bug.
不要在bug前止步 - Use debugging compilers.
使用带有debug功能的编译器 - Initialize constants with DATA statements or INITIAL attributes; initialize variables with executable code.
用DATA声明或者INITIAL属性来初始化常量;用可执行代码来初始化变量。 - Watch out for off-by-one errors.
小心off by one错误(多算或者少算一个的错误,类似于定界错误) - Take care to branch the right way on equality.
注意等价比较的分支 - Avoid multiple exits from loops.
避免循环中有多处出口 - Make sure your code 'does nothing' gracefully.
确保你的代码没有做任何“优美的事情” - Test programs at their boundary values.
测试程序的边界值 - Program defensively.
编写有自我保护功能的代码 - 10.0 times 0.1 is hardly ever 1.0.
0.1乘10.0几乎从不等于1.0 - Don't compare floating point numbers just for equality.
不要试图比较浮点数
Efficiency and Instrumentation
- Make it right before you make it faster.
先确保程序运行正确后,再图速度 - Keep it right when you make it faster.
当你想要提高程序运行速度的时候,请保持它的正确性 - Make it clear before you make it faster.
当你想要提高程序运行速度的时候,请保持它的清晰性 - Don't sacrifice clarity for small gains in 'efficiency.'
不要为了获得小的效率提升而牺牲清晰性 - Let your compiler do the simple optimizations.
让你的编译器来做简单的优化 - Don't strain to re-use code; reorganize instead.
不要过度重用代码;重构他们 - Make sure special cases are truly special.
确保特殊情况真的特殊 - Keep it simple to make it faster.
保持程序简单可以使他运行的更快 - Don't diddle code to make it faster - find a better algorithm.
采用好的算法而不是仅仅修改代码来让提高程序的效率 - Instrument your programs. Measure before making 'efficiency' changes.
度量你的程序;再做“更有效率的”改变前估量
Documentation
文档
- Make sure comments and code agree.
确保代码和注释一致 - Don't just echo the code with comments - make every comment count.
不要在用代码来进行注释,让每个注释看起来都有价值 - Don't comment bad code - rewrite it.
不要注释糟糕的代码,你应该重写糟糕的代码 - Use variable names that mean something.
使用有意义的变量名 - Use statement labels that mean something.
使用有意义的语句标签 - Format a program to help the reader understand it.
格式化你的程序可以帮助阅读它的人理解 - Indent to show the logical structure of a program.
使用缩进来表达程序的逻辑 - Document your data layouts.
文档化你的数据设计 - Don't over-comment.
不要过于地注释
阅读(769) | 评论(0) | 转发(1) |