The Elements of Programming Style 编程风格要素 中文英文中英对照

下面的程序风格规则提炼自Brian Kernighan和P. J. Plauger的经典著作《The Elements of Programming Style》(由McGraw Hill出版)。由于原书出版较早并主要针对Fortran语言,本文仅提炼适用于当今主流编程语言的风格要素。

——Clive Studio

1.Write clearly - don't be too clever.
要写清楚,不要自作聪明

 

2.Say what you mean, simply and directly.
简单、直截了当地表达你的意思
3.Use library functions whenever feasible.
尽可能使用库函数
4.Avoid too many temporary variables.
不要使用太多的临时变量
5.Write clearly - don't sacrifice clarity for “efficiency.”
要写清楚-不要为所谓的“效率”牺牲清晰度
6.Let the machine do the dirty work.
让机器去做麻烦的事情
7.Replace repetitive expressions by calls to a common function.
将重复代码替代为调用通用函数
8.Parenthesize to avoid ambiguity.
使用括号避免歧义
9.Choose variable names that won't be confused.
选择没有歧义的变量名
10.Avoid unnecessary branches.
避免使用不必要的分支
11.If a logical expression is hard to understand, try transforming it.
如果逻辑表达式不好理解,就试着做下变形

 

12.Choose a data representation that makes the program simple.
采用让程序更简洁的数据表达形式
13.Write first in easy-to-understand pseudo language; then translate into whatever language you have to use.
先用易于理解的伪代码写;再翻译成你使用的语言
14.Modularize. Use procedures and functions.
模块化.使用过程和函数
15.Avoid gotos completely if you can keep the program readable.
只要你能保证程序的可读性,能不用goto就别用
16.Don't patch bad code - rewrite it.
不要给糟糕的代码打补丁 - 重写就是了.
17.Write and test a big program in small pieces.
编写以及测试一个大型程序的时候,分块进行
18.Use recursive procedures for recursively-defined data structures.
使用递归过程来处理递归定义的数据结构

 

19.Test input for plausibility and validity.
始终要测试输入的正确性和有效性
20.Make sure input doesn't violate the limits of the program.
确保输入不会超出程序的限制
21.Terminate input by end-of-file marker, not by count.
通过文件结束符来终止输入,而不是通过记数
22.Identify bad input; recover if possible.
识别错误输入;并尽可能修复
23.Make input easy to prepare and output self-explanatory.
让输入容易构造;让输出表述清楚
24.Use uniform input formats.
使用统一的输入格式
25.Make input easy to proofread.
让输入容易校对
26.Use self-identifying input. Allow defaults. Echo both on output.
使用输入提示.接受默认值.并将其显示

 

27.Make sure all variable are initialized before use.
确保所有的变量在使用前都被初始化
28.Don't stop at one bug.
不要因某一bug而停止不前
29.Use debugging compilers.
打开编译程序的调试选项
30.watch out for off-by-one errors.
小心off-by-one错误(http://en.wikipedia.org/wiki/Off-by-one_error)
31.Take care to branch the right way on equality.
注意等价比较的分支
32.Be careful if a loop exits to the same place from the middle and the bottom.
当循环中有多个跳出点时要小心
33.Make sure your code does “nothing” gracefully.
如果什么都不做,那么也要优雅地表达这个意思
34.Test programs at their boundary values.
用边界值测试程序
35.Check some answers by hand.
手工检查一些答案
36.10.0 times 0.1 is hardly ever 1.0.
10.0乘以0.1很难保证永远是1.0
37.7/8 is zero while 7.0/8.0 is not zero.
7/8 等于0,而7.0/8.0不等于0
38.Don't compare floating point numbers solely for equality.
不要简单地判断两个浮点数是否相等

 

39.Make it right before you make it faster.
先做对,再做快
40.Make it fail-safe before you make it faster.
先使其可靠,再让其更快
41.Make it clear before you make it faster.
先把代码弄干净,再让它变快
42.Don't sacrifice clarity for small gains in “efficiency.”
别为一丁点”性能”就牺牲掉整洁
43.Let your compiler do the simple optimizations.
简单的优化让编译器去做
44.Don't strain to re-use code; reorganize instead.
不要过分追求重用代码;下次用的时候重新组织一下即可
45.Make sure special cases are truly special.
确保特殊的情况的确是特殊的
46.Keep it simple to make it faster.
保持简单以使其更快
47.Don't diddle code to make it faster - find a better algorithm.
不要死磕代码来加快速度 - 找个更好的算法
48.Instrument your programs. Measure before making “efficiency” changes.
用工具分析你的程序.在做”性能”改进前先评测一下

 

49.Make sure comments and code agree.
确保注释和代码一致
50.Don't just echo the code with comments - make every comment count.
不要在注释里仅仅重复代码 - 让每处注释都有价值
51.Don't comment bad code - rewrite it.
不要给糟糕的代码做注释 - 应该重写它
52.Use variable names that mean something.
采用有意义的变量名
53.Use statement labels that mean something.
使用有意义的语句标签
54.Format a program to help the reader understand it.
格式化程序让阅读代码的人更容易理解
55.Document your data layouts.
为数据布局撰写文档
56.Don't over-comment.
不要过分注释

posted on 2010-01-03 17:56  CLive Studio  阅读(3366)  评论(3编辑  收藏  举报

导航