避免使用不必要的 if
if 的问题:
- Making it hard to see what the business logic is through the routing logic.
- It’s all too easy to increase coupling by bringing things together than don’t need to be. Making code harder to read and change.
- The third problem with if statements is that you have to simulate execution in your own head. That’s taking away from your mental energy, energy that would be better spent thinking about solving the problem, rather than how the intracate code branches weave together.
Pattern 1: Boolean Params
拆分成多个方法
Pattern 2: Switch to Polymorphism
Use Polymorphism.
Pattern 3: NullObject/Optional over null passing
Pattern 4: Inline statements into expressions
Pattern 5: Give a coping strategy
要求参数中提供