Chinaunix首页 | 论坛 | 博客
  • 博客访问: 129954
  • 博文数量: 47
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-30 12:26
文章分类
文章存档

2010年(32)

2009年(15)

我的朋友

分类:

2009-06-05 14:32:44

Brian Kernighan, P. J. Plauger

Introduction

介绍

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

Expression

表达

  1. Say what you mean, simply and directly.
    简单,明了地表达你想要表达的
  2. Use library functions.
    使用库函数
  3. Avoid temporary variables.
    避免使用临时变量
  4. Write clearly - don't sacrifice clarity for 'efficiency.'
    写清楚-不要为了“效率”牺牲清晰性
  5. Let the machine do the dirty work.
    让计算机去做复杂的事情
  6. Replace repetitive expressions by calls to a common function.
    使用通用的函数调用来代替重复的代码
  7. Parenthesize to avoid ambiguity.
    使用括号来避免歧义
  8. Choose variable names that won't be confused.
    选择没有歧义的变量名称
  9. Avoid the Fortran arithmetic IF.
    避免使用Fortran中的IF
  10. Avoid unnecessary branches.
    避免使用不必要的分支
  11. Use the good features of a language; avoid the bad ones.
    使用一门语言中那些好的特性,避免使用那些坏的特性
  12. Don't use conditional branches as a substitute for a logical expression.
    不要使用条件分支来代替逻辑表达式
  13. Use the 'telephone test' for readability.
    使用telephone test来检查可读性

Control Structure

控制结构

  1. Use DO-END and indenting to delimit groups of statements.
    使用DO-END和缩进来为你的一组表达式定界
  2. Use IF-ELSE to emphasize that only one of two actions is to be performed.
    使用IF-ELSE来强调只会实现一到两种行为
  3. Use DO and DO-WHILE to emphasize the presence of loops.
    使用DODO-WHILE来强调循环的存在
  4. Make your programs read from top to bottom.
    使你的程序可以用从上到下的方式进行阅读
  5. Use IF, ELSE IF, ELSE IF, ELSE to implement multi-way branches.
    使用IF, ELSE IF, ELSE IF, ELSE来实现多路分支
  6. Use the fundamental control flow constructs.
    使用基本的控制流结构
  7. Write first in an easy-to-understand pseudo-language; then translate into whatever language you have to use.
    先用简单可读的伪码编写代码,然后把它翻译成你想使用的那种语言
  8. Avoid THEN-IF and null ELSE.
    避免THEN-IF和空ELSE
  9. Avoid ELSE GOTO and ELSE RETURN.
    避免使用ELSE GOTOELSE RETURN
  10. Follow each decision as closely as possible with its associated action.
    使每个决策尽可能和相联系的动作挨在一起
  11. Use data arrays to avoid repetitive control sequences.
    使用数组来避免反复的控制序列
  12. Choose a data representation that makes the program simple.
    采用能够使你程序简单的数据表示方法
  13. Don't stop with your first draft.
    不要止步于你的第一版程序

Program Structure 
程序结构

  1. Modularize. Use subroutines.
    模块化,使用子例程
  2. Make the coupling between modules visible.
    使模块间的耦合可见
  3. Each module should do one thing well.
    每个模块只做好一件事
  4. Make sure every module hides something.
    确保每个模块都隐藏了些东西
  5. Let the data structure the program.
    让数据将程序变得结构化
  6. Don't patch bad code - rewrite it.
    不要修补糟糕的代码-重写吧
  7. Write and test a big program in small pieces.
    从小的模块开始编写和测试大的程序
  8. Use recursive procedures for recursively-defined data structures.
    为递归的数据结构使用递归的过程

Input and Output 
输入和输出

  1. Test input for validity and plausibility.
    测试输入的有效性和合理性
  2. Make sure input cannot violate the limits of the program.
    保证输入不会违反程序的内在限制
  3. Terminate input by end-of-file or marker, not by count.
    以文件结束或者是标志来结束输入,而不是用计数的方式
  4. Identify bad input; recover if possible.
    识别坏的输入,如果可能的话恢复它
  5. Treat end of file conditions in a uniform manner.
    以统一的方式来处理文件结束状态
  6. Make input easy to prepare and output self-explanatory.
    使输入准备简单化,使输出可以自我解释
  7. Use uniform input formats.
    使用统一的输入格式
  8. Make input easy to proofread.
    使输入能够轻易被校正
  9. Use free-form input when possible.
    尽可能使用无格式的输入
  10. Use self-identifying input. Allow defaults. Echo both on output.
    使用自我识别的输入,允许默认情况,在输出中现实它们
  11. Localize input and output in subroutines.
    将输入和输出局部化于子例程

Common Blunders

常见错误

  1. Make sure all variables are initialized before use.
    确保所有的变量在使用前都被初始化
  2. Don't stop at one bug.
    不要在bug前止步
  3. Use debugging compilers.
    使用带有debug功能的编译器
  4. Initialize constants with DATA statements or INITIAL attributes; initialize variables with executable code.
    DATA声明或者INITIAL属性来初始化常量;用可执行代码来初始化变量。
  5. Watch out for off-by-one errors.
    小心off by one错误(多算或者少算一个的错误,类似于定界错误)
  6. Take care to branch the right way on equality.
    注意等价比较的分支
  7. Avoid multiple exits from loops.
    避免循环中有多处出口
  8. Make sure your code 'does nothing' gracefully.
    确保你的代码没有做任何“优美的事情”
  9. Test programs at their boundary values.
    测试程序的边界值
  10. Program defensively.
    编写有自我保护功能的代码
  11. 10.0 times 0.1 is hardly ever 1.0.
    0.1
    10.0几乎从不等于1.0
  12. Don't compare floating point numbers just for equality.
    不要试图比较浮点数

Efficiency and Instrumentation

  1. Make it right before you make it faster.
    先确保程序运行正确后,再图速度
  2. Keep it right when you make it faster.
    当你想要提高程序运行速度的时候,请保持它的正确性
  3. Make it clear before you make it faster.
    当你想要提高程序运行速度的时候,请保持它的清晰性
  4. Don't sacrifice clarity for small gains in 'efficiency.'
    不要为了获得小的效率提升而牺牲清晰性
  5. Let your compiler do the simple optimizations.
    让你的编译器来做简单的优化
  6. Don't strain to re-use code; reorganize instead.
    不要过度重用代码;重构他们
  7. Make sure special cases are truly special.
    确保特殊情况真的特殊
  8. Keep it simple to make it faster.
    保持程序简单可以使他运行的更快
  9. Don't diddle code to make it faster - find a better algorithm.
    采用好的算法而不是仅仅修改代码来让提高程序的效率
  10. Instrument your programs. Measure before making 'efficiency' changes.
    度量你的程序;再做“更有效率的”改变前估量

Documentation

文档

  1. Make sure comments and code agree.
    确保代码和注释一致
  2. Don't just echo the code with comments - make every comment count.
    不要在用代码来进行注释,让每个注释看起来都有价值
  3. Don't comment bad code - rewrite it.
    不要注释糟糕的代码,你应该重写糟糕的代码
  4. Use variable names that mean something.
    使用有意义的变量名
  5. Use statement labels that mean something.
    使用有意义的语句标签
  6. Format a program to help the reader understand it.
    格式化你的程序可以帮助阅读它的人理解
  7. Indent to show the logical structure of a program.
    使用缩进来表达程序的逻辑
  8. Document your data layouts.
    文档化你的数据设计
  9. Don't over-comment.
    不要过于地注释
阅读(769) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~