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.
- 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
- 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