Code Smell 36 — Switch/case/elseif/else/if statements
First programming lesson: Control structures. Senior developer lesson: avoid them.
- Too many decisions together
- Duplicated code
- Violation of Open/Closed Principle.
- A new condition should not change the main algorithm.
- Create hierarchies/compose objects following Open closed principle.
- Use State pattern to model transitions.
- Use Strategy Pattern/Method Object to choose for branches.
- Discrete Values
- State transition
- Algorithm choice.
Since there are valid cases for If/else usages, we should not pull the plug and forbid these instructions. We can put a ratio of if statements/other statements as a warning instead.
Code Smell 12 — Null
Programmers use Null as different flags. It can hint an absence, an undefined value, en error etc.
How to Get Rid of Annoying IFs Forever
Why the first instruction we learn to program should be the last to use.
If debugging is the process of removing software bugs, then programming must be the process of putting them in.
This article is part of the CodeSmell Series.