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.
博客介绍了编写if语句和case语句的准则。编写if语句时,要先写正常路径代码、确保分支正确等;编写case语句时,要按字母或数字顺序排序、将正常情况放首位等,还给出了避免错误和简化代码的建议。
1080

被折叠的 条评论
为什么被折叠?



