Replace Type Code with Subclasses

Symptom

A class with a kind field plus methods that switch on it; the agent adding a new kind must find every switch and update each consistently.

Goal

Each kind is a subclass; the agent adds a new kind by adding one class, and the type system tells it what's still missing.

Before the refactoring

class Employee {
type; // 'engineer' | 'manager'
bonus() {
switch (this.type) {
case 'engineer': return this.salary * 0.10;
case 'manager': return this.salary * 0.15 + this.reports.length * 100;
}
}
}

After the refactoring

class Employee {}
class Engineer extends Employee {
bonus() { return this.salary * 0.10; }
}
class Manager extends Employee {
bonus() { return this.salary * 0.15 + this.reports.length * 100; }
}
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure

The agent must enumerate every switch on every addition; the type system can't enforce completeness so the agent verifies by grep.

Tradeoff

If only one or two switches exist on the type code, the subclass hierarchy is over-design; the agent now navigates a class tree for what was a single switch.

Relief

Adding a new kind is mechanical and type-system-enforced; the agent's plan-and-execute loop for new variants is bounded.

Trap

Subclassing for type codes used in only one or two switches creates a hierarchy the agent must navigate for a coordination cost that was already small.