Conditional statements like "If" and "switch" multiply the number of possible flows the code execution can follows. So they increase the complexity and the number of tests required to verify the code.
=> Conditional statements are time and effort (costs €€€) multipliers
Here is a list of known techniques to eliminate conditionals :
- Replace conditional with polymorphism (look also the patterns template method and pluggable selector and abstract factory)
- The patterns Null Object and Special Case
- The pattern State
- The pattern Pluggable Object
- The pattern Strategy
- The pattern Composite
- The pattern Visitor
- Replace conditional with a Dictionary (or with a conversion operator)
- Replace Conditional Dispatcher with Command
- Linearize the algorithm
Look also "Replace nested conditionals with guards" to simplify the conditional before eliminating it.
some solutions like 6 and 10 completely eliminate the IF, some like 1 and 2 eliminate the duplication of the IF putting the IF logic in just one place, some like 8 reuse a IF logic tha is already implemented in the Framework so it prevent from creating a new IF.
Now I challenge you, show me an IF that cannot be eliminated !
Print | posted @ mercoledì 21 aprile 2010 02:58