Back to Catalog
Code Smells
Object-Orientation Abusers
Switch Statements
Complex switch statements that should be replaced with polymorphism.
Intent & Description
'
π― Intent
Identify switch statements that select behavior based on type or properties, which often indicate missing polymorphism.
π Context
You have complex switch or if-else chains that select behavior based on object types or properties. Adding new types requires modifying existing code.
π‘ Solution
Replace conditionals with polymorphism. Use the Strategy pattern or create subclasses with overridden methods.'
Real-world Use Case
Use when switch statements select behavior based on type and new types are frequently added.
Source
π TL;DR
Switch statements on type often indicate missing polymorphism. Replace with Strategy pattern or inheritance.