
Duplication, the Rule of Three, and Wrong Abstractions
Not all duplication is debt. The rule of three, Sandi Metz's argument that duplication beats the wrong abstraction, and the mechanical path for unwinding one.
TL;DR: Not all duplication is debt. The DRY principle from The Pragmatic Programmer is about duplicated knowledge, not merely duplicated text — two similar-looking blocks can encode different rules that happen to look alike today. The rule of three says tolerate the second occurrence and refactor on the third, because two instances are a guess at what varies while three is evidence. Sandi Metz's argument that duplication is far cheaper than the wrong abstraction explains why a premature unification degrades into a parameter-laden mess, and the unwind path — inline back, simplify per caller, let the right boundary emerge — is how to recover from one.
Not all duplication is debt
DRY, as the 20th anniversary edition of The Pragmatic Programmer states it, is a principle about knowledge, not merely text: every piece of knowledge must have a single, unambiguous, authoritative representation within a system. That framing matters more than it looks, because two blocks of code can be textually identical while encoding entirely different knowledge — and knowledge, not character count, is what the principle actually protects.
Call this incidental duplication: code that happens to look the same today but serves different masters, and will diverge for different reasons as each master's requirements change. A validation rule for a sign-up form and a validation rule for an admin-invite form might both check that an email field is non-empty and reject the same three characters — today. But the sign-up rule answers to product requirements about consumer trust, and the admin rule answers to internal security policy. Unify them because they look alike, and a future change to either one has to somehow avoid breaking the other, because they were welded into a single implementation that never should have been one thing.
Essential duplication is the opposite case: the same rule, genuinely repeated, where a fix applied in one location and missed in the other is not a stylistic wrinkle but a live defect waiting to happen. A tax rate calculated two different places, a discount eligibility check copy-pasted into two code paths — these are the same knowledge, stated twice, and every future change to one has to remember to update the other. This is the duplication DRY was written for.
Telling the two apart is the actual skill. Textual similarity is not evidence either way; the question is whether a change to one instance's underlying reason would necessarily require the same change to the other.
The rule of three
A practical default is the rule of three, a guideline that Martin Fowler's Refactoring credits to Don Roberts: the first time you do something, you just do it; the second time you do something similar, you tolerate the duplication; the third time you do something similar, you refactor.
This article's reasoning for the rule is about evidence, not superstition. With one instance, there is nothing to compare. With two instances, an engineer is guessing at the axis of variation — which parts are fixed and which parts will differ as more cases appear — because two data points support almost any story. The third instance is where guessing stops: it either confirms the axis the first two suggested, or it reveals that the real axis of variation was different all along. Abstracting after seeing three real cases produces a shape informed by actual variation instead of a shape that happened to fit the first two examples that came to mind.
The rule of three is a default, not a law, and treating it as one misses the point. A duplicated security check or a duplicated money calculation is a case where two instances may already be one too many, because the cost of a missed update is disproportionate to the cost of a slightly premature abstraction. The rule's real content is not the number three specifically — it is the discipline of waiting for evidence before generalizing, calibrated to how expensive a missed instance would be.
The wrong abstraction
Sandi Metz's argument, stated plainly: duplication is far cheaper than the wrong abstraction. A premature unification that guessed the axis of variation wrong does not fail loudly — it fails by accretion. The shared code is technically correct for its original caller, so instead of questioning whether the abstraction was right, each new caller that does not quite fit gets a new parameter, a new conditional branch, a new flag threaded through the signature.
The accretion signature is recognizable once named: each caller adds one more knob to the shared function, until the function is a conditional labyrinth that serves none of its callers particularly well, and understanding any single call site requires tracing which combination of flags applies to it. What began as a clean abstraction is now harder to read than the original duplicated code would have been, because the duplicated version at least let each caller's logic be read on its own.
Why does this persist instead of getting fixed? Two forces, both quiet. Sunk cost makes the abstraction feel like an asset that has already been paid for — reworking it feels like waste, even though the ongoing cost of every new flag is the real waste accumulating in plain sight. And "reuse" has a surface plausibility that resists scrutiny: the shared function looks efficient, looks like good engineering, right up until someone counts the branches inside it and realizes it is functioning as a tax on every caller rather than an asset any of them benefit from.
Unwinding a wrong abstraction
Recognizing a wrong abstraction is only useful if there is a way out, and the mechanical path, the one Metz herself recommends, is more straightforward than it sounds. Inline the shared code back into every caller. Within each caller, use the parameters being passed to work out which part of the inlined code that caller executes, then delete the bits it does not need; a function was carrying a flag for every caller, but any single caller only needed the branch that applied to it. What remains is a set of copies that look similar but are no longer forced through one shared implementation.
Re-duplication is the honest intermediate state here, not a failure to reach. From those cleaned-up copies, a better-fitting boundary often becomes visible on its own — not guessed at in advance, but observed from what the actual, now-simplified callers have in common. Sometimes no new abstraction emerges at all, and that is fine too; the copies were cheaper than the wrong abstraction, and staying as copies may be the correct final state.
This sequence is behavior-preserving throughout, and it should land as its own commits rather than one large tangled change, as in this illustrative sequence:
1. refactor: inline sharedDiscountLogic() into applySignupDiscount()
- behavior-preserving; existing tests pass unchanged
2. refactor: inline sharedDiscountLogic() into applyRenewalDiscount()
- behavior-preserving; existing tests pass unchanged
3. refactor: remove unused tier and isTrial parameters from the signup copy
- behavior-preserving for this caller; its own tests unchanged
4. refactor: remove unused earlyBird parameter from the renewal copy
- behavior-preserving for this caller; its own tests unchangedInlining, per-caller simplification, and any later re-extraction are each independently reviewable — a reviewer verifying step three only has to confirm that the sign-up path's behavior did not change, not reason about the renewal path at the same time.
Judgment heuristics at PR scale
A working test for whether duplication is incidental or essential: would a change to one copy necessarily apply to the other? If the answer is no — the two blocks would plausibly diverge under a realistic future change — the duplication is incidental, and unifying it welds two futures together that were never meant to travel as one.
Distance changes the calculation, too. Duplication inside the same function, or between two functions in the same file, is cheap to unify and cheap to unwind if the unification turns out wrong — the blast radius is small either way. Duplication across module boundaries is a different proposition: unifying it creates coupling between parts of the system that may otherwise have no reason to know about each other, and that coupling can outlive the convenience it was built for.
When the case is genuinely close, the maintenance question is the tiebreaker: which future engineer is worse off — the one who has to remember to fix the same bug in two places, or the one who has to decode a parameterized abstraction serving five callers with subtly different needs? Neither cost is zero. The judgment is in weighing them honestly for the specific case in front of you, rather than defaulting to either "always extract" or "never extract" as a reflex.