程序设计要素——规则汇编

———— Brian W.Kernighan, P.J.Plauger

一直奇怪,两位UNIX大牛写这本书时为什么基于Fortran语言编写,后来仔细一看出版时间——1978年,那时别说C语言,连UNIX还未诞生,C语言1972年才诞生,1978年才正式发布。这样来看就没什么问题了。
以下条款大部分与语言无关,但有些是与Fortran语言有关,因此可以忽略。

01.Write clearly - don’t be too clever.
01.清楚地写-但是不要过于聪明

02.Say what you mean, simply and directly.
02.简单,明了地表达你想要表达的

03.Avoid temporary variables.
03.避免使用临时变量

04.Write clearly - don’t sacrifice clarity for ‘efficiency.’
04.写清楚-不要为了“效率”牺牲清晰性

05.Let the machine do the dirty work.
05.让计算机去做复杂的事情

06.Replace repetitive expressions by calls to a common function.
06.使用通用的函数调用来代替重复的代码

07.Parenthesize to avoid ambiguity.
07.使用括号来避免歧义

08.Choose variable names that won’t be confused.
08.选择没有歧义的变量名称

09.Avoid the Fortran arithmetic IF.
09.避免使用Fortran中的IF

10.Avoid unnecessary branches.
10.避免使用不必要的分支

11.Use the good features of a language; avoid the bad ones.
11.使用一门语言中那些好的特性,避免使用那些坏的特性

12.Don’t use conditional branches as a substitute for a logical expression.
12.不要使用条件分支来代替逻辑表达式

13.Use the ‘telephone test’ for readability.
13.使用telephone test来检查可读性

14.Use DO-END and indenting to delimit groups of statements.
14.使用DO-END和缩进来为你的一组表达式定界

15.Use IF-ELSE to emphasize that only one of two actions is to be performed.
15.使用IF-ELSE来强调只会实现一到两种行为

16.Use DO and DO-WHILE to emphasize the presence of loops.
16.使用DO和DO-WHILE来强调循环的存在

17.Make your programs read from top to bottom.
17.使你的程序可以用从上到下的方式进行阅读

18.Use IF, ELSE IF, ELSE IF, ELSE to implement multi-way branches.
18.使用IF, ELSE IF, ELSE IF, ELSE来实现多路分支

19.Use the fundamental control flow constructs.
19.使用基本的控制流结构

20.Write first in an easy-to-understand pseudo-language; then translate into whatever language you have to use.
20.先用简单可读的伪码编写代码,然后把它翻译成你想使用的那种语言

21.Avoid THEN-IF and null ELSE.
21.避免THEN-IF和空ELSE

22.Avoid ELSE GOTO and ELSE RETURN.
22.避免使用ELSE GOTO和ELSE RETURN

23.Follow each decision as closely as possible with its associated action.
23.使每个决策尽可能和相联系的动作挨在一起

24.Use data arrays to avoid repetitive control sequences.
24.使用数组来避免反复的控制序列

25.Choose a data representation that makes the program simple.
25.采用能够使你程序简单的数据表示方法

26.Don’t stop with your first draft.
26.不要止步于你的第一版程序

27.Modularize. Use subroutines.
27.模块化,使用子例程

28.Make the coupling between modules visible.
28.使模块间的耦合可见

29.Each module should do one thing well.
29.每个模块只做好一件事

30.Make sure every module hides something.
30.确保每个模块都隐藏了些东西

31.Let the data structure the program.
31.让数据将程序变得结构化

32.Don’t patch bad code - rewrite it.
32.不要修补糟糕的代码-重写吧

33.Write and test a big program in small pieces.
33.从小的模块开始编写和测试大的程序

34.Use recursive procedures for recursively-defined data structures.
34.为递归的数据结构使用递归的过程

35.Test input for validity and plausibility.
35.测试输入的有效性和合理性

36.Make sure input cannot violate the limits of the program.
36.保证输入不会违反程序的内在限制

37.Terminate input by end-of-file or marker, not by count.
37.以文件结束或者是标志来结束输入,而不是用计数的方式

38.Identify bad input; recover if possible.
38.识别坏的输入,如果可能的话恢复它

39.Treat end of file conditions in a uniform manner.
39.以统一的方式来处理文件结束状态

40.Make input easy to prepare and output self-explanatory.
40.使输入准备简单化,使输出可以自我解释

41.Use uniform input formats.
41.使用统一的输入格式

42.Make input easy to proofread.
42.使输入能够轻易被校正

43.Use free-form input when possible.
43.尽可能使用无格式的输入

44.Use self-identifying input. Allow defaults. Echo both on output.
44.使用自我识别的输入,允许默认情况,在输出中现实它们

45.Localize input and output in subroutines.
45.将输入和输出局部化于子例程

46.Make sure all variables are initialized before use.
46.确保所有的变量在使用前都被初始化

47.Don’t stop at one bug.
47.不要在bug前止步

48.Use debugging compilers.
48.使用带有debug功能的编译器

49.Initialize constants with DATA statements or INITIAL attributes; initialize variables with executable code.
49.用DATA声明或者INITIAL属性来初始化常量;用可执行代码来初始化变量。

50.Watch out for off-by-one errors.
50.小心off by one错误(多算或者少算一个的错误,类似于定界错误)

51.Take care to branch the right way on equality.
51.注意等价比较的分支

52.Avoid multiple exits from loops.
52.避免循环中有多处出口

53.Make sure your code ‘does nothing’ gracefully.
53.确保你的代码没有做任何“优美的事情”

54.Test programs at their boundary values.
54.测试程序的边界值

55.Program defensively.
55.编写有自我保护功能的代码

56.10.0 times 0.1 is hardly ever 1.0.
56.0.1乘10.0几乎从不等于1.0

57.Don’t compare floating point numbers just for equality.
57.不要试图比较浮点数

58.Make it right before you make it faster.
58.先确保程序运行正确后,再图速度

59.Keep it right when you make it faster.
59.当你想要提高程序运行速度的时候,请保持它的正确性

60.Make it clear before you make it faster.
60.当你想要提高程序运行速度的时候,请保持它的清晰性

61.Don’t sacrifice clarity for small gains in ‘efficiency.’
61.不要为了获得小的效率提升而牺牲清晰性

62.Let your compiler do the simple optimizations.
62.让你的编译器来做简单的优化

62.Don’t strain to re-use code; reorganize instead.
62.不要过度重用代码;重构他们

63.Make sure special cases are truly special.
63.确保特殊情况真的特殊

64.Keep it simple to make it faster.
64.保持程序简单可以使他运行的更快

65.Don’t diddle code to make it faster - find a better algorithm.
65.采用好的算法而不是仅仅修改代码来让提高程序的效率

66.Instrument your programs. Measure before making ‘efficiency’ changes.
66.度量你的程序;再做“更有效率的”改变前估量

67.Make sure comments and code agree.
67.确保代码和注释一致

68.Don’t just echo the code with comments - make every comment count.
68.不要在用代码来进行注释,让每个注释看起来都有价值

69.Don’t comment bad code - rewrite it.
69.不要注释糟糕的代码,你应该重写糟糕的代码

70.Use variable names that mean something.
70.使用有意义的变量名

71.Use statement labels that mean something.
71.使用有意义的语句标签

72.Format a program to help the reader understand it.
72.格式化你的程序可以帮助阅读它的人理解

73.Indent to show the logical structure of a program.
73.使用缩进来表达程序的逻辑

74.Document your data layouts.
74.文档化你的数据设计

75.Don’t over-comment.
75.不要过度注释
<完>