[转] 編程風格要素-The Elements of Programming Style 中文英文中英對照
2013-08-27 10:41 fangzhao.lee 阅读(376) 评论(0) 编辑 收藏 举报转自: http://www.loliman3000.com/tech/2fe33ce32906f0302412881.php
下面的程序風格規則提煉自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.
不要過分注釋