Monday, May 16, 2011

Code Complete by Steve McConnell : Chapter 14

In this chapter, the author provides guidelines for writing conditional statements as follows:

  • Write nominal paths through the code first then the exceptions.
  • Branch correctly on inequality, i.e. use $<$ and $<=$ correctly.
  • Put normal case in if condition rather than in else.
The author then provides some guidelines in writing chains of if-then-else statements:
  • Simplify complicated tests with boolean function calls
  • Put the most common case first
  • Make sure all the cases are covered
  • If the language allows, replace the chain with other constructs such as case/switch
The author then provides some guidelines to write case statements:
  • Order case statements sorted alphabetically or numerically
  • Order cases by frequency
  • Put the normal case first
  • Keep actions of each case simple
  • Don't use phony variables in order to use case statement
  • Use the default clause to detect errors

No comments:

Post a Comment