Friday, May 20, 2011

Code Complete by Steve McConnell : Chapter 17

In this chapter, the author discusses issues in writing general control constructs. The author starts discussing boolean expressions and provide following tips:
  • Use true and false instead of 1 and 0 for boolean expressions.
  • Compare boolean values to false implicitly
  • Break complicated tests in partial tests with boolean expressions
  • Move the complicated tests completely to a boolean function
  • Use decision tables to replace complicated conditions
  • Use De Morgan's theorem to simplify boolean expressions
  • Use parenthesis to clarify boolean expressions
The author suggests to use curly braces or begin/end to mark the boundary of block statements executed inside a loop or in a conditional statement. With regards to null statements, the author suggests to call attention to them and use preprocessor macro for null statements.

The author then provides tips to avoid deep nesting by converting nested ifs to if-then-else or to a case statement.

The author then discusses the power of structured programming and its three main constructs:
  • Sequence: A set of statements executed in order.
  • Selection: It is a construct that allows statements to be executed selectively.
  • Iteration: It is a construct that allows group of statements to be executed multiple times.
The author then provides techniques to emulate structured constructs using goto statement in different programming languages.

The author introduces the concept of cyclomatic complexity or control-flow complexity. The author also provides guidelines on how to reduce it.

No comments:

Post a Comment