Lesson 9 of 9 · 22m
Refactoring Away From Patterns: Deleting Design That No Longer Pays
Sometimes the correct refactor is removing factories, interfaces, layers, and events — collapsing indirection so the code tells the truth again.
The hardest lesson in design is that a pattern can be correct in the commit that introduced it and wrong in the system you maintain today. Requirements change. Implementations disappear. The flexibility survives as a tax on every reader while the benefit is gone. Deleting obsolete structure is senior engineering, not vandalism.
Inline what no longer varies
A single implementation behind a mandatory interface often deserves collapse.
// before
interface Clock { now(): Date; }
class SystemClock implements Clock { now() { return new Date(); } }
// after, when tests only need injected time values
function formatDeadline(now: Date, dueAt: Date): string {
return formatDistance(now, dueAt);
}Do not delete the seam blindly. If tests genuinely swap the clock, keep a small function parameter or constructor dependency. The goal is not minimal abstraction at all costs; it is truthful structure.
Collapse forwarding chains
If Controller -> Service -> Manager -> Repository -> Client mostly forwards the same call, some layer is not earning its keep. Inline until each remaining layer has a job:
- controller translates HTTP
- application service coordinates a use case and transaction boundary
- repository translates persistence
- client translates network details
Names matter less than responsibility. A valid architecture can have three layers or six; it cannot have layers whose only job is to satisfy tradition.
Replace unnecessary inheritance with composition
Deep hierarchies often encode one shared convenience method and five accidental couplings. Move behavior into small collaborators or plain functions. Start by copying behavior down to leaf classes, deleting abstract hooks one at a time, then removing empty base classes. It looks repetitive during migration; that is fine. Duplication is easier to see and remove than hidden coupling.
Convert events back into explicit calls when correctness requires order
Event-driven design is seductive because producers stay simple. But if business correctness requires that email is sent after commitment, inventory decremented exactly once, and audit recorded before return, invisible listeners create distributed control flow.
// fragile: correctness depends on registered listeners
events.emit("order.placed", order);
// clearer: required workflow is explicit
await orders.commit(order);
await inventory.reserve(order.lines);
await audit.record("order.placed", order);
await mailer.sendReceipt(order);Events may still be right for nonessential effects. The distinction is whether failure must participate in the transaction or workflow decision.
Remove factories that only construct one thing
If a factory has one branch and no test seam, call the constructor or use a plain function. Fewer types, fewer registrations, less startup magic. Keep factories where construction policy is real: environment, feature flags, vendor selection, lifetime control.
Make deletion safe
Use the same discipline as any refactor: pin behavior first, migrate one consumer at a time, let the compiler find references, and keep movement separate from behavior change. Deleting indirection usually reduces test complexity too, but do not delete boundary tests just because implementation became simpler.
Field notes
- An abstraction with one implementation is not automatically waste; a test seam or dependency boundary may justify it.
- The best time to delete structure is immediately after the variation it supported disappears.
- Code that is slightly concrete and easy to change beats code that is theoretically extensible and practically inert.
Was this lesson helpful?