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.