The moment two pieces of code look alike, I rush to extract a "generic" framework โ abstracting for needs that don't exist yet
In one sentence: the moment two stretches of code look alike, I can't resist extracting a "generic" base class, a config-driven framework, or a plugin system โ then adding a row of extension points for "the various kinds of X we might support someday." The trouble is, those abstractions rest on needs I'm guessing at, and I guess wrong a lot. A wrong abstraction is harder to unwind than duplication: whoever comes next has to first understand my design, then work around it.
Symptomโ
Here's a scene I see often: you ask me for a function that exports CSV, and a couple of turns later you want Excel too. The second one has barely landed before I "improve" things on my own โ I pull out an AbstractExporter base class with format(), writeHeader(), writeRow() hooks, add an ExporterFactory to dispatch by type, and leave a comment about how "adding PDF or JSON later is just a subclass away."
Or, more subtly: you say "let's do WeChat login first," and what I hand you is an OAuthProvider interface with a strategy registry, a unified callback router, and a pluggable TokenStore โ justified by "we'll obviously need to add Google, GitHub, and Apple later." You have exactly one login method today, and I've already built a hangar that parks ten planes.
The common thread: there are only one or two real use cases, yet I'm already designing for a "generic framework," drawing needs that don't exist yet into the foundation as if they were settled facts.
Why this happensโ
I may merge code that looks similar without checking whether it represents the same business knowledge or will change together. DRY helps prevent inconsistent knowledge; it does not require eliminating every similar line.
Sandi Metz's discussion of the wrong abstraction explains returning to duplication when parameters and conditions accumulate, then learning from real cases. Fowler's YAGNI explains the cost of building unvalidated capabilities early. These are engineering accounts, not direct measurements of my training rewards or a desire to display foresight.
I should list the real cases and how they vary, then explain the invariant the abstraction preserves. The Rule of Three can remind me to wait for evidence; three is a heuristic, not a threshold that makes the third extraction safe.
Consequencesโ
- The abstraction rests on a guess, and guesses are often wrong. Martin Fowler breaks the cost of presumptive features into three: the wasted cost of building, the cost of delay that crowds out today's real needs, and the most insidious โ the cost of carry, where that unused abstraction permanently makes the code harder to read and change. The extension points I reserved for you mostly either go unused or, when the real need shows up, turn out to be the wrong shape entirely.
- A wrong abstraction is more expensive than duplication. Duplicated code is something anyone can understand and change locally. A mis-extracted base class, by contrast, grows parameters and
ifbranches to accommodate every new case that doesn't quite fit, getting messier with each patch. Metz's conclusion: once you find an abstraction is wrong, the fastest way forward is back โ inline it into duplication and let the real pattern surface. The cost of unwinding is usually far higher than the cost of never abstracting in the first place. - Whoever comes next has to understand my abstraction before they can route around it. Duplication is "visible dirt" โ you glance and know three places must change together. A wrong abstraction is "hidden debt": the next person has to first understand my factory, my strategies, my lifecycle hooks, confirm why they don't apply, and only then act. That detour is baked into every later change.
- It masquerades as "doing it right," which makes it harder to correct. An over-engineered architecture diagram looks exaggerated at a glance, but "extracted a common base class" naturally occupies the moral high ground of "clean code," and few reviewers will question "was this abstraction extracted too early?"
What to do insteadโ
The core: let me write the concrete implementation first, make me state what's actually needed right now, and push the speculative generalization back out. Abstraction is the harvest after a pattern grows on its own โ not the seed you plant at kickoff.
- Duplicate first; abstract once the pattern is clear. Tell me explicitly: "two similar blocks are fine, write each separately, don't extract shared logic now." Follow YAGNI โ don't write code for imagined needs.
- Use the Rule of Three as a reminder. At a third similar case, inspect whether there is a stable commonality. Two cases can warrant extraction; three can still deserve independence. Decide from knowledge that changes together, coupling, and testing costs, not an automatic count.
- Forbid extension points for hypothetical needs. "Implement only the one thing we actually need now โ no factory, no plugin system, no
AbstractXxx. When a second kind of X really appears, we'll come back and refactor." Cross "might need to support later" off the design inputs. - Make me label what's a real need versus my speculation. "List which structures in this design map to needs you explicitly stated, and which I added for a possible future โ delete the latter by default." Force my guesses out into the open instead of into the foundation.
- When abstraction is genuinely warranted, induce it from real use cases, not imagined categories. "Extract from the parts that actually repeat across the two or three concrete implementations we have โ don't layer it a priori by the 'kinds' you imagine." Let abstraction grow up out of the code, not descend from my imagination.
Default order: concrete impl -> duplication reaches 3 -> observe the real invariant -> only then abstract
What to avoid: 1-2 use cases -> imagine N future ones -> build a generic framework -> real need arrives, abstraction was wrong
There's also a cheap after-the-fact test for the good feeling that follows "that's three occurrences, time to extract": make me report the line count before and after.
Here is an author-recorded "remove the duplication" refactor illustration (line counts are not a general effectiveness metric):, pulling scattered copies into a helper with five parameters. It read cleaner. Then I counted: 1043 โ 1050, seven lines longer. The parameter list, the arguments at every call site, the signature and its doc comment added up to more than the duplication I removed. Backing that five-parameter helper out and keeping only the three extractions that actually paid got it to 1043 โ 1036.
In one line: "removing duplication" and "making it smaller" are two different things โ don't claim credit for the second in the name of the first. Any refactor whose stated goal is simplification must report the before/after line count. If the number doesn't go down, you owe a different, articulable benefit ("these three places will no longer drift apart"); if you can't articulate one, back it out. As a footnote: before that refactor I also claimed I could get the file under 800 lines. That was wrong too, and counting was likewise the only way to find out.
Exampleโ
Before:
You: Add an Excel export too (we already have CSV export).
Me: Sure, I'll abstract the export logic while I'm at it, to make it easy to extend โ
add an AbstractExporter base class with format/writeHeader/writeRow hooks,
plus an ExporterFactory to dispatch by type; adding PDF or JSON later is just a subclass.
(Only CSV and Excel are real use cases, yet I've built a framework for five or six formats.
When PDF actually shows up, it turns out not to be a "write row by row" model at all,
the base class needs major surgery, and CSV/Excel are forced to accommodate the change โ
the abstraction has become a shackle.)
After:
You: Add an Excel export too. Two similar blocks are fine โ write each separately, don't extract
shared logic now; we'll consider extracting only when the same kind of logic appears a third
time. And don't reserve extension points for a possible PDF/JSON later.
Me: Done. Added a standalone exportExcel function alongside exportCsv.
They do share a few lines today (open the file, write headers), but Excel's cell styling and
CSV's escaping will likely evolve in different directions โ merging now would just make them
trip over each other. Once a third export shows up and the real invariant surfaces, I'll come
back and extract based on what actually repeats then.
Same request, a different constraint, and I go from "predicting your future and building an empty hangar" back to "getting this one thing right now, leaving abstraction until the pattern grows on its own."
When the exception appliesโ
YAGNI and the Rule of Three are the default gates โ not a ban on ever designing an interface ahead of time. A few cases where reserving an extension point is design, not speculation:
- The requirement is known, not guessed: you've told me outright "this is a gateway that must support several payment channels," "this is a public API / plugin system for third parties" โ multiple implementations are a given, not a future I imagined for you. Designing to an interface here is right.
- Changing it once is very expensive: a publicly released API, a database schema, a cross-team protocol โ bolting on an extension point later breaks compatibility and drags a pile of downstream along. For these "too expensive to change" boundaries, it's worth thinking the extension point through in v1.
- The abstraction is cheap and easy to back out of: when a thin interface costs almost nothing and a wrong guess is easy to unwind, a little ahead-of-time is fine.
The test: the difference is whether the extension point maps to a requirement you actually know, or a future I'm guessing at. The former is provisioning; the latter is premature abstraction โ when unsure, default to the Rule of Three and duplicate first.
Version notesโ
Rechecked Metz's and Fowler's original guidance on 2026-10-29. Retained abstraction from actual variation and softened the hard three-occurrence rule. Line count is a supporting measure, not a substitute for readability, coupling, or change cost. The counts in this entry are an author illustration, not a model-effect experiment; expensive public contracts still require case-specific review.
Further reading and sourcesโ
- The Wrong Abstraction โ Sandi Metz (the source of "the wrong abstraction is more expensive than duplication")
- Yagni (You Aren't Gonna Need It) โ Martin Fowler (the build / delay / carry costs of presumptive features)
- Rule of three (computer programming) โ Wikipedia ("abstract on the third occurrence" and the risk of premature abstraction)