copyright Steve J. Hodges   http://steveh.net/cs19/styletips.html

Style Tips

from The Elements of Programming Style, Second Edition, Brian W. Kernighan and P. J. Plauger, Bell Telephone Labs, 1978

  1. Write Clearly - Don't be too clever.
  2. Say what you mean, simply and directly.
  3. Use library functions.
  4. Avoid 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. Use the good features of a language; avoid the bad ones.
  12. Use the "telephone test" for readability.
  13. Make your programs read from top to bottom.
  14. Use arrays to avoid repetitive control sequences.
  15. Don't stop with your first draft.
  16. Modularize. Use subroutines.
  17. Each module should do one thing well.
  18. Make sure every module hides something.
  19. Don't patch bad code - rewrite it.
  20. Write and test a big program in small pieces.
  21. Test input for validity and plausibility.
  22. Make sure input cannot violate the limits of the program.
  23. Identify bad input; recover if possible.
  24. Make input easy to prepare and output self-explanatory.
  25. Localize input and output in subroutines.
  26. Make sure all variables are initialized before use.
  27. [During debugging…] Don't stop at one bug.
  28. Watch out for off-by-one errors.
  29. Avoid multiple exits from loops.
  30. Test programs at their boundary values.
  31. Program defensively.
  32. 10.0 times 0.1 is hardly ever 1.0.
  33. Don't compare floating point numbers just for equality.
  34. Make it right before you make it faster.
  35. Keep it right when you make it faster.
  36. Make it clear before you make it faster.
  37. Don't sacrifice clarity for small gains in "efficiency."
  38. Keep it simple to make it faster.
  39. Don't diddle code to make it faster - find a better algorithm.
  40. Make sure comments and code agree.
  41. Don't just echo the code with comments - make every comment count.
  42. Don't comment bad code- rewrite it.
  43. Use variable names that mean something.
  44. Indent to show the logical structure of a program.

Bonus tip from Brian W. Kernighan