Applies duplication judgment to real code: knowledge versus coincidental duplication, the rule of three, extracting safely under tests, and undoing a wrong abstraction by inlining it back.
First I'd diff the three copies and ask whether they encode the same business knowledge or just look alike. If finance owns one discount rule and all three must change together when it changes, that's real duplication: pin each copy with tests, decide which differences are bugs, and extract one DiscountPolicy. If they belong to different products with different owners that happen to match today, that's coincidental similarity, and merging them couples things that will drift.
If an earlier extraction has grown boolean flags and if (caller is X) branches, it's the wrong abstraction: inline it back into each caller, delete dead branches, and re-extract only what's genuinely shared. The trade-off: duplication costs edits in several places; the wrong abstraction costs every caller.
"I wouldn't extract on sight. First I'd put the three copies side by side and diff them, because the small differences are the interesting part. Then I'd ask who owns the rule. If it's one discount policy that finance defines and all three handlers must change together when the policy changes, that's duplicated knowledge and it should live in one place. If one is retail pricing, one is a wholesale contract and one is a staff discount, they look alike today but have different owners and will diverge, so merging them creates coupling, not reuse.
When it is real duplication, I do it safely: characterisation tests around each handler first, then I decide for each difference whether it's a bug or a deliberate variation. Bugs get unified, often after checking with the product owner, because a rounding difference in one copy might be what customers are already being charged. Real variations become explicit parameters or a small strategy, not hidden flags. Then I extract with the IDE refactoring and point all three callers at it, one PR at a time.
The mistake I see is the opposite one, sticking with a shared abstraction after it's gone wrong. You can tell because every new caller adds a flag:
CalculateDiscount(order, isWholesale, skipLoyalty, useLegacyRounding). At that point the fastest way forward is backwards: inline it into each caller, delete the branches each caller doesn't use, and re-extract the part that's genuinely common. Duplication is far cheaper than the wrong abstraction."
| Question | Real duplication | Coincidental similarity |
|---|---|---|
| Same business rule? | Yes, one policy | No, different rules that match today |
| Same owner / stakeholder? | Yes | Different teams or customers |
| Must change together? | Always | Rarely or never |
| Action | Extract one implementation | Leave separate, maybe name the difference |
| Step | What I do |
|---|---|
| 1. Diff | Compare copies line by line; list every difference |
| 2. Classify | Each difference: bug, deliberate variation, or noise |
| 3. Ask the owner | Confirm which behaviour is correct where money is involved |
| 4. Pin | Characterisation tests for each caller's current behaviour |
| 5. Extract | IDE Extract Method/Class, variations as explicit parameters or strategies |
| 6. Migrate | One caller per PR, tests green each time |
| Sign | Example |
|---|---|
| Boolean or mode parameters per caller | isWholesale, legacyMode |
| Caller checks inside shared code | if (source == OrderSource.Marketplace) |
| Most callers use a fraction of it | Five options, each caller sets one |
| Fear of changing it | Every change needs all teams to review |
In an order platform, a shared PriceCalculator had grown seven boolean parameters across four channels and caused two pricing incidents in a quarter. Inlining it into the four callers removed [~40%] of its branches as dead per channel; the re-extracted common core was 60 lines, and the next channel-specific promotion shipped in [1] day without touching the other channels.
Checks that the candidate applies DRY with judgment on real code: telling knowledge duplication from coincidence, merging safely, and recovering from a wrong abstraction.
Green flags
Red flags
The original Pragmatic Programmer definition: "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." It is about knowledge (a business rule, a schema, a configuration value), not about identical-looking lines. Two identical if statements encoding different rules are not a DRY violation; one rule expressed in a C# class and again in a SQL view is, even though the text differs completely.
Retail discount ─┐
├─► SharedDiscount() ◄── change for retail
Staff discount ─┘ breaks staff
Merging code with different reasons to change creates coupling: the two callers can no longer evolve independently. This is the single responsibility principle viewed from the other side.
With two copies you often can't see which parts are essential and which are accidental. A third use case reveals the real shape of the abstraction. So tolerate two, extract on three, unless the duplicated thing is a clear, stable business rule (like a GST rate) that should be centralised immediately.
Sandi Metz described the cycle: someone extracts shared code; a new requirement is almost the same, so they add a parameter; repeat. Each step is locally sensible, but the abstraction ends up as a switchboard of flags.
v1: Calculate(order)
v2: Calculate(order, isWholesale)
v3: Calculate(order, isWholesale, skipLoyalty)
v4: Calculate(order, isWholesale, skipLoyalty, legacyRounding)
The fix is to go backwards: inline, simplify each caller, re-extract.
Across microservices or bounded contexts, a shared library for business logic couples deployments and release cycles. It is often better to duplicate the logic (or the DTO) and let each service own its copy. Share infrastructure (logging, auth plumbing), not domain rules.
Three handlers with a copied discount rule, and the extraction once it's confirmed to be one business rule.
// Before: the same loyalty discount copied into three handlers.
public sealed class PlaceWebOrderHandler
{
public decimal Discount(Order o) =>
o.Customer.LoyaltyYears >= 3 ? Math.Round(o.Subtotal * 0.05m, 2) : 0m;
}
public sealed class PlacePhoneOrderHandler
{
public decimal Discount(Order o) =>
o.Customer.LoyaltyYears >= 3 ? Math.Round(o.Subtotal * 0.05m, 2) : 0m;
}
public sealed class PlaceMarketplaceOrderHandler
{
// The difference: no loyalty check, so every marketplace order gets 5% off.
// Bug or deliberate marketplace promotion? Only the product owner can say.
public decimal Discount(Order o) => Math.Round(o.Subtotal * 0.05m, 2);
}
// After: one policy owned by pricing; the product owner confirmed the missing check was a bug.
// Rounding stays Math.Round's default (MidpointRounding.ToEven) so no customer's discount changes;
// switching to AwayFromZero would be a separate, deliberate behaviour change.
namespace Shop.Pricing;
public sealed class LoyaltyDiscountPolicy
{
private const int QualifyingYears = 3;
private const decimal Rate = 0.05m;
public decimal DiscountFor(Order order) =>
order.Customer.LoyaltyYears >= QualifyingYears
? Math.Round(order.Subtotal * Rate, 2)
: 0m;
}
public sealed record Customer(int LoyaltyYears);
public sealed record Order(Customer Customer, decimal Subtotal);
// Program.cs
// builder.Services.AddSingleton<LoyaltyDiscountPolicy>();
// A wrong abstraction to recognise and undo: flags per caller.
public decimal CalculateDiscount(Order order, bool isWholesale, bool skipLoyalty, bool legacyRounding)
{
if (isWholesale) return order.Subtotal * 0.12m; // only wholesale uses this
if (skipLoyalty) return 0m; // only staff orders
var d = order.Subtotal * 0.05m;
return legacyRounding ? Math.Round(d, 2) : Math.Round(d, 2, MidpointRounding.AwayFromZero);
}
// Fix: inline into each caller, delete the branches it never takes, re-extract only LoyaltyDiscountPolicy.
Seniors judge duplication by change, not by text. The useful question is: when this rule changes, how many places must change, and will people know? If the answer is 'three, and nobody will remember', extract. If the answer is 'they change independently', leave them. The rule of three is a good default because two examples rarely reveal which parts vary.
The other senior skill is being willing to go backwards. Inlining a wrong abstraction feels like regression, but it is usually the fastest route to a correct design, and the IDE's Inline Method refactoring makes it safe.
| Option | When it fits | Cost | When not to |
|---|---|---|---|
| Extract now | Clear, stable rule (tax rate, rounding policy) | Small | Different owners or reasons to change |
| Wait for the third copy | Shape of the abstraction is unclear | Temporary duplication | Rule is safety- or money-critical and already diverging |
| Parameterise | One real variation point | One parameter | Variations keep multiplying |
| Strategy per variant | Several real variants with different rules | More types | Only one variant exists |
| Inline and re-extract | Abstraction grown flags and caller checks | A larger PR or several | Abstraction is healthy |
DRY (from The Pragmatic Programmer) says every piece of knowledge should have one authoritative representation in the system. It is about rules, not text: the GST rate, the definition of an overdue invoice or a validation rule should live in one place. Two blocks of code that look the same but change for different reasons are accidental duplication, and merging them couples unrelated features. I remove true duplication of business knowledge quickly, and I tolerate similar-looking code until a real shared concept appears, usually around the third repetition.
Duplication is acceptable when the copies are coincidentally similar but represent different knowledge, so they may change independently. Merging them creates the wrong abstraction: a shared helper that gains a flag for each caller. I also accept duplication across bounded contexts and microservices, where a shared model would couple teams and deployments, and in tests, where readable, self-contained cases beat clever shared setup. In review I justify it with three facts: who owns each copy, whether they change for the same reason, and what coupling a shared version would add.
How to tell it (STAR)
**S:** Our [four] sales channels shared one price calculator that had grown [seven] flags, and changes for one channel kept breaking another. **T:** After the [second] incident I was asked to make pricing safe to change. **A:** I pinned each channel's behaviour with snapshot tests, then inlined the calculator into each channel, removed the branches each one never used, and re-extracted only the genuinely shared GST and loyalty rules, with an ADR naming the owner. **R:** We had [no] cross-channel pricing incidents in the next [two quarters], and a channel promotion that used to take [a week] of reviews shipped in [a day].
Last updated 23 Nov 2026.