Fly With My Heart
誓言无声,人生无悔。
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
::
54 随笔 :: 2 文章 :: 6 评论 :: 0 引用
The Elements of Programming Style
The Elements of Programming Style
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.
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.
Control Structure
Use DO-END and indenting to delimit groups of statements.
Use IF-ELSE to emphasize that only one of two actions is to be performed.
Use DO and DO-WHILE to emphasize the presence of loops.
Make your programs read from top to bottom.
Use IF, ELSE IF, ELSE IF, ELSE to implement multi-way branches.
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.
Avoid ELSE GOTO and 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.
Use debugging compilers.
Initialize constants with DATA statements or INITIAL attributes; initialize variables with executable code.
Watch out for off-by-one errors.
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.
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.
绿色通道:
好文要顶
关注我
收藏该文
与我联系
posted on 2008-04-03 18:58
FlyWithMyHeart
阅读(2623)
评论(1)
编辑
收藏
评论
1605145
#1楼
2009-08-04 17:18
不见不散
学习。
回复
引用
查看
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
Chrome将给老机带来更快的3D绘图性能
·
在线支付创业公司Stripe获红杉资本等1800万美元的投资,公司估值达1亿美元
·
创新工场孵化公司磊友科技今天正式推出首款大型手机HTML5网页游戏《黎明帝国》
·
霍金的伟大与不幸
·
Linux为什么成功?因为它的失败是免费的!
»
更多新闻...
最新知识库文章
:
·
高级编程语言的发展历程
·
如何学习一门新的编程语言?
·
学习不同编程语言的重要性
·
为什么我喜欢富于表达性的编程语言
·
计算机专业的女生为什么要学编程
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
<
2008年4月
>
日
一
二
三
四
五
六
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
公告
1.问题定义
2.算法设计
3.数据结构
所有程序的本质就是逻辑
昵称:
FlyWithMyHeart
园龄:
4年5个月
粉丝:
0
关注:
0
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
随笔分类
C++/C/VC(97)
(rss)
CryptoPP(3)
(rss)
English(3)
(rss)
Linux2VC(2)
(rss)
Matlab(2)
(rss)
MPEG2(1)
(rss)
psychology practice(1)
(rss)
TM历程(7)
(rss)
Ubuntu(3)
(rss)
Unix命令(1)
(rss)
windows(4)
(rss)
编程之道(54)
(rss)
计算机基础/原理(8)
(rss)
设计模式(3)
(rss)
生活(29)
(rss)
试题(10)
(rss)
数据库(2)
(rss)
数学(2)
(rss)
水印&隐写(1)
(rss)
算法/数据结构(3)
(rss)
通信(1)
(rss)
项目配置管理(10)
(rss)
信号/语音(7)
(rss)
信息论(1)
(rss)
知识管理(KM)(1)
(rss)
随笔档案
2010年6月 (1)
2010年3月 (1)
2009年11月 (1)
2009年10月 (2)
2009年8月 (1)
2009年7月 (1)
2009年6月 (2)
2009年5月 (2)
2009年4月 (7)
2009年2月 (2)
2008年12月 (5)
2008年11月 (1)
2008年10月 (3)
2008年4月 (12)
2008年3月 (4)
2007年8月 (9)
文章分类
视频标准(2)
(rss)
最新评论
阅读排行榜
评论排行榜
推荐排行榜