dreampuf
2014-11-21 04:13:14 +08:00
TLTR: “等于”比“不等于”更符合直觉,避免嵌套优先执行异常分支。选第二种。
via. Code Complete 2nd
Summary of Techniques for Reducing Deep Nesting
The following is a list of the techniques you can use to reduce deep nesting, along with references to the sections in this book that discuss the techniques:
- Retest part of the condition (this section)
- Convert to if-then-elses (this section)
- Convert to a case statement (this section)
- Factor deeply nested code into its own routine (this section)
- Use objects and polymorphic dispatch (this section)
- Rewrite the code to use a status variable (in Section 17.3)
- Use guard clauses to exit a routine and make the nominal path through the code clearer (in Section 17.1)
- Use exceptions (Section 8.4)
- Redesign deeply nested code entirely (this section)