Knowledge
Search knowledge... ⌘K
Knowledge · Anti-Patterns
Smell Repeated Switches
Code smell indicating repeated switch/case logic across the codebase (updated terminology from Switch Statements)
Tags
refactoring-fowlercode-qualitymaintainabilitycode-smell2nd
Impact
In the first edition, any switch statement was suspect. With languages supporting switch on strings and first-class functions, the blanket advice is no longer valid. The problem with repeated switches is that whenever you add a clause, you must find all the switches and update them. Polymorphism provides an elegant way to handle this by ensuring additions only require a new class.
Symptoms
- In the first edition, any switch statement was suspect. With languages supporting switch on strings and first-class functions, the blanket advice is no longer valid. The problem with repeated switches is that whenever you add a clause, you must find all the switches and update them. Polymorphism provides an elegant way to handle this by ensuring additions only require a new class.
Source
Martin Fowler — Refactoring: Improving the Design of Existing Code, 2nd