Geek Logbook

Tech sea log book

The elements of programming style: Epilogue


There are many good books on languages, algorithms and numerical methods available to those who want to learn programming in greater depth. Our goal was not to teach languages or algorithms, but to teach you to program well. 

Programmers have a strong tendency to underrate the importance of good style. Eternally optimistic, we all like to think that once we throw a piece of code together, however haphazardly, it will work properly the first time and ever after. Why waste time cleaning up something that is almost certain to be correct? Besides, it probably will be used for only a few weeks. 

There are really two answers to the question. The first is suggested by the word “almost.” A slap-dash piece of code that falls short of perfection can be a difficult creature to deal with.

The second point is that phrase “only a few weeks.” Certainly we write code differently depending on the ultimate use we expect to make of it. But computer centers are full of programs that were written for a short-term use, then were pressed into years of service.

The essence of what we are trying to convey is summed up in the elusive word “style.” It is not a list of rules so much as an approach and an attitude. “Good programmers” are those who already have learned a set of rules that ensures good style; many of them will read this book and see no reason to change. 

Summary of rules

  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 the Fortran arithmetic IF. 
  11. Avoid unnecessary branches. 
  12. Use the good features of a language; avoid the bad ones. 
  13. Don’t use conditional branches as a substitute for a logical expression. 
  14. Use the “telephone test” for readability. 
  15. Use DO-END and indenting to delimit groups of statements. 
  16. Use IF-ELSE to emphasize that only one of two actions is to be performed. 
  17. Use DO and DO-WHILE to emphasize the presence of loops. 
  18. Make your programs read from top to bottom. 
  19. Use IF … ELSE IF … ELSE IF … ELSE … to implement multi-way branches. 
  20. Use the fundamental control flow constructs. 
  21. Write first in an easy-to-understand pseudo-language; then translate into whatever language you have to use. 
  22. Avoid THEN-IF and null ELSE. A void ELSE GOTO and ELSE RETURN. 
  23. Follow each decision as closely as possible with its associated action. 
  24. Use data arrays to avoid repetitive control sequences. 
  25. Choose a data representation that makes the program simple. 
  26. Don’t stop with your first draft. Modularize. Use subroutines. 
  27. Make the coupling between modules visible. 
  28. Each module should do one thing well. 
  29. Make sure every module hides something. 
  30. Let the data structure the program.
  31. Don’t patch bad code – rewrite it. 
  32. Write and test a big program in small pieces. 
  33. Use recursive procedures for recursively-defined data structures.
  34. Test input for validity and plausibility.
  35. Make sure input cannot violate the limits of the program.
  36. Terminate input by end-of-file or marker, not by count.
  37. Identify bad input; recover if possible. 
  38. Treat end of file conditions in a uniform manner. 
  39. Make input easy to prepare and output self-explanatory. 
  40. Use uniform input formats.
  41. Make input easy to proofread. 
  42. Use free-form input when possible. 
  43. Use self-identifying input. Allow defaults. Echo both on output. 
  44. Localize input and output in subroutines. 
  45. Make sure all variables are initialized before use. 
  46. Don’t stop at one bug. Use debugging compilers. 
  47. Initialize constants with DATA statements or INITIAL attributes; initialize variables with executable code. 
  48. Watch out for off-by-one errors. 
  49. Take care to branch the right way on equality.  
  50. Avoid multiple exits from loops.
  51. Make sure your code “does nothing” gracefully. 
  52. Test programs at their boundary values. 
  53. Program defensively. 
  54. 10.0 times 0.1 is hardly ever 1.0. 
  55. Don’t compare floating point numbers just for equality. 
  56. Make it right before you make it faster. 
  57. Keep it right when you make it faster. 
  58. Make it clear before you make it faster. 
  59. Don’t sacrifice clarity for small gains in “efficiency.” 
  60. Let your compiler do the simple optimizations. 
  61. Don’t strain to re-use code; reorganize instead. 
  62. Make sure special cases are truly special. 
  63. Keep it simple to make it faster. 
  64. Don’t diddle code to make it faster – find a better algorithm. 
  65. Instrument your programs. Measure before making “efficiency” changes. 
  66. Make sure comments and code agree.
  67. Don’t just echo the code with comments – make every comment count. 
  68. Don’t comment bad code – rewrite it. 
  69. Use variable names that mean something. 
  70. Use statement labels that mean something. 
  71. Format a program to help the reader understand it. 
  72. Indent to show the logical structure of a program. 
  73. Document your data layouts. 
  74. Don’t over-comment.  

Leave a Reply

Your email address will not be published. Required fields are marked *.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>