The Elements of Programming Style (编程风格的元素)读书总结

The Elements Of Programming Style, Second Endition, Kernigan and Plauger

这是一本关于编程风格经典的老书了,一些经典的CS著作里面都提过此书,而且都是强烈推荐阅读,当初就是因为些书评价非常高,所以才想读的,不过很可惜,由于此书出版年月太早,而且书中使用的语言为fortran语言,所以已经停止印刷了。网上有pdf版,可以在这里下载(点击打开链接)。

虽然是用fortran语言写的,不过里面关于编程风格的每一条建议对于每一个程序员都还是很有帮助的。看的过程中,发现里面很多观点都似曾相识,不是在别的书里面,就是在别人的blog中看到过,我想大概这就是英雄所见略同吧(或者都是受此书影响)。书里面程序不是很重要,关键是要体会作者传达的思想,里面重要的条例全部被单独列出来了,每章最后的总结写的也很好,一定要好好看一看,那是一章内容的精华。


本来想总结一下书中提到的那些条例,不过发现网上已经有总结的非常好的了,下面是来自Clive Studio博客中的内容。

查看原文请点击这里

下面的程序风格规则提炼自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.
不要过分注释


  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值