24
Replace Conditional with Polymorphism
Goal
Each case becomes a class implementing a shared interface; dispatch happens once via virtual call.
Before the refactoring
switch (event.kind) {
case 'click': return onClick(event);
case 'key': return onKey(event);
}After the refactoringevent.handle(); // ClickEvent and KeyEvent each implement handle()Savings
Adding a new case is one new class; the type system tells you what's missing.
Note
If only one switch on the type code exists, polymorphism is overkill — wait for the second or third repeat before extracting subclasses.