Duplicate Code: What It Costs and How to Fix It
Identical or near-identical code blocks that appear in multiple locations, requiring synchronized maintenance.
What It Is
Duplicate Code is the most commonly detected smell and one of the most costly. It occurs when the same logic exists in two or more places. The duplication may be exact (copy-paste) or structural (same algorithm with different variable names). When a bug is found in duplicated code, it must be fixed in every copy. When a requirement changes, every copy must be updated. The cost scales with the number of duplicates and the frequency of changes.
Why It Costs Money
Bug fixes must be applied to every copy. When a developer fixes a bug in one copy and misses another, the bug persists in production. A bug reported later in the missed copy looks new but was already fixed once.
Requirements changes multiply effort linearly. If a validation rule exists in 5 copies and the rule changes, the developer must find and update all 5. This is not 5x the typing; it is 5x the testing, 5x the code review, and 5x the risk of inconsistency.
Codebase size inflates artificially. Duplicated code increases the surface area that must be read, understood, and maintained. A 100,000-line codebase with 20% duplication has 20,000 lines that exist only to create maintenance burden.
Specific Cost Mechanisms
- ●Unfixed-duplicate bugs: each missed copy has to be diagnosed again when the 'fixed' bug reappears
- ●Change multiplication: each change to duplicated logic requires N updates, N test runs, N review cycles
- ●Reading burden: developers waste time reading code they have already seen, wondering if the copies are identical or subtly different
Estimated Annual Cost
Cost of one instance a year by team size, from our model at $180K fully-loaded cost per engineer. Low and high use the bottom and top of the High inputs; mid is halfway. See full methodology.
| Team Size | Low | Mid | High |
|---|---|---|---|
| 3 devs | $3K | $7K | $11K |
| 5 devs | $4K | $10K | $16K |
| 8 devs | $6K | $15K | $24K |
| 10 devs | $7K | $19K | $30K |
| 20 devs | $14K | $35K | $57K |
These are model outputs, not measurements. Every input is on the cost model page, and the same model produces the figures on the 22 smells page. Put in your own team and cost in the smell profile calculator.
How to Detect It
Specific rules and thresholds for automated detection. See full tool comparison.
Detects blocks of 10+ duplicated lines (configurable)
Detects both exact and structural duplication
Language-agnostic duplicate block detection
Standalone tool supporting 150+ languages
Refactoring Patterns
Proven techniques to eliminate this smell. See all refactoring patterns.
Extract Method
Duplicated blocks within the same class
Extract Class/Module
Duplicated logic appears across different classes
Form Template Method
Subclasses contain similar algorithms with slight variations