Case Statement
Case statements are a sign that a method contains too much knowledge.
Symptoms
- Case statements that check the class of an object.
- Case statements that check a type code.
- Divergent Change caused by changing or adding when clauses.
- Shotgun Surgery⭐ caused by duplicating the case statement.
Solutions
- Replace Type Code with Subclasses if the case statement is checking a type code, such as question_type.
- Replace Conditional with Polymorphism when the case statement is checking the class of an object.
- Use Convention Over Configuration when selecting strategy based on a string name.