skipnothing/Design Patterns

Anti-Patterns & When Not to Use

A three-line switch on payment type grew into an interface plus four classes across four files, though nothing varies yet that the switch couldn't hold. You paid the cost of flexibility the code never spends.

  • ▸A pattern's indirection is billed the moment you add it; the flexibility pays back only if a second case arrives.
  • ▸Two copies of similar code are cheaper to keep than one abstraction bent with a flag per almost-fitting case.
  • ▸When an abstraction is wrong, the fast repair runs backward: inline it into every caller and rebuild from the duplication.
01

What Every Pattern Costs

A getDiscount(type) function is six lines: a switch over three customer types, each returning a rate. It works, it reads top to bottom, and a new reader understands it in one pass.

Refactor it "properly" and you get a Discount interface, three classes implementing it, and a lookup that picks one by type. Now five files hold what six lines held. To answer "what rate does a student get?" a reader opens the interface, finds the implementation, and reads one method, three hops instead of one glance.

That is the trade every pattern makes, and it is worth naming exactly. The indirection, the extra interface, the extra classes, the extra files to trace, is a cost you pay the moment you add the structure, whether or not anything ever uses it. The flexibility, the room to add a fourth rate without touching the other three, is a benefit you collect only if a fourth rate actually arrives. Cost is unconditional and immediate; benefit is conditional and deferred.

The extra structure also makes the whole thing harder to change and debug later, more moving parts to hold in your head on every edit. So when the variation never comes, the three classes are not neutral. They are pure overhead sitting between every reader and the six lines of logic.

A pattern applied where its problem is not present has a name: an anti-pattern. Not broken code, not ugly code, a sensible, well-known structure whose drawbacks now outweigh its benefits, because the benefit half never arrived.

COST NOW, BENEFIT MAYBE

Keep going, sign up to unlock the rest

4 more parts in this topic, plus 18+ more topics in Design Patterns.

Sign up, it's freeSee the full Design Patterns
Patterns in Practice0/4#18 Iterator