原创 2004年10月09日 22:05:00

Guidelines when writing if statements
1. Write the nominal path through the code first; then write the unusual cases.
2. Make sure that you branch correctly on equality.
3. Follow the if clause with a meaningful statement.
4. Conside the else clause.
5. Test the else clause for correctness.
6. Check for revseal of the if and else clauses.
7. Simplify complicated tests with boolean function calls.
8. Put the most common cases first.
9. Make sure that all cases are covered.
10. Replace if-then-else chains with other constructs if your language supports them.

Case statement
1. Order casesalphabetically or numerically if they are equally important.
2. Put the normal case first.
3. Order cases by frequency.
4. Keep the actions of each case simple.
5. Don't make up phony variables in order to be able to use the case statment.
6. Use the default clause only to detect legitimate defaults.
7. Use the default clause to detect errors.
8. In C++ and Java, avoid dropping through the end of a case statement.