Glossary/Code Duplication
Technical Debt & Code Quality
1 min read
Share:

What is Code Duplication?

TL;DR

Code duplication occurs when identical or near-identical code blocks exist in multiple locations within a codebase.

Code duplication occurs when identical or near-identical code blocks exist in multiple locations within a codebase. Also known as copy-paste programming or WET (Write Everything Twice) code, duplication is one of the most common code smells and a significant driver of maintenance costs.

Duplicated code creates problems: when a bug is found in one copy, all copies need to be fixed separately. When behavior needs to change, every copy must be updated. When testing, each copy needs its own tests. The DRY principle (Don't Repeat Yourself) addresses this directly.

Tools like jscpd, PMD, and SonarQube can detect duplicated code blocks automatically. The ideal target is <5% duplication across the codebase. Above 10% duplication indicates systematic copy-paste patterns that need refactoring.

Not all duplication is bad. Sometimes two pieces of code are similar by coincidence but serve different domains. Premature abstraction of such code creates worse problems than the original duplication.

Why It Matters

Code duplication is a direct multiplier of maintenance cost. Every duplicated block multiplies the cost of every future change by the number of copies. Reducing duplication from 15% to 5% can reduce maintenance hours by 20-30%.

Frequently Asked Questions

How much code duplication is acceptable?

Below 5% is healthy. 5-10% is common but should be reduced. Above 10% indicates systematic copy-paste patterns that need refactoring.

How do you reduce code duplication?

Extract duplicated blocks into shared functions or modules. Use static analysis tools to identify duplicates. Be cautious of premature abstraction — only combine code that changes for the same reasons.

Related Terms

Need Expert Help?

Richard Ewing is a Product Economist and AI Capital Auditor. He helps companies translate technical complexity into financial clarity.

Book Advisory Call →