Fly With My Heart

誓言无声,人生无悔。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

The Elements of Programming Style
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.
  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.
Control Structure
  1. Use DO-END and indenting to delimit groups of statements.
  2. Use IF-ELSE to emphasize that only one of two actions is to be performed.
  3. Use DO and DO-WHILE to emphasize the presence of loops.
  4. Make your programs read from top to bottom.
  5. Use IF, ELSE IF, ELSE IF, ELSE to implement multi-way branches.
  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.
  9. Avoid ELSE GOTO and ELSE 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.
  3. Use debugging compilers.
  4. Initialize constants with DATA statements or INITIAL attributes; initialize variables with executable code.
  5. Watch out for off-by-one errors.
  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.
  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.
posted on 2008-04-03 18:58  FlyWithMyHeart  阅读(4234)  评论(1编辑  收藏  举报