Code Duplication

Updated

Code duplication is the presence of identical or substantially similar source code or logic in multiple locations. In a legacy codebase, it can create several implementations of one rule, so a change made in one place may need to be found, repeated, and verified everywhere else.

Code duplication exists when identical or closely related code appears in multiple locations. The copies may be exact blocks, functions with renamed variables, or separate implementations of the same business rule. The maintenance problem comes from scattered ownership: a future change may reach one copy and miss another.

Forms of duplication

Text matching can find identical or near-identical source, but duplication also exists at the level of behaviour. Two services can calculate the same status through different code. Several controllers can validate the same rule with small variations. Those cases need domain knowledge as well as automated detection.

FormExample shapeMain review question
Exact copySame block in several filesDo all copies need the same change?
Parameterised copySame flow with different constantsWould one named operation express the variation?
Structural copySimilar sequence with renamed typesIs there a stable shared responsibility?
Rule duplicationDifferent code implements one policyWhich implementation is authoritative?

Not every repeated line is harmful. Setup code, explicit mappings, and small independent adapters may be clearer when kept separate. The audit looks for duplication that causes inconsistent behaviour or repeated maintenance.

Why duplication accumulates

Legacy code often lacks an obvious owner for a shared rule. A developer copies working logic because changing the original feels risky, because the modules cannot depend on each other, or because the similarity is discovered only after both versions exist. Over time, each copy can acquire its own exceptions.

This pattern can form part of technical debt, but the existence of copies alone does not measure the cost. Version history shows whether they change together. Tests and incident records show whether they already disagree. Architecture explains whether sharing an implementation would cross a boundary that should remain separate.

How an audit assesses the risk

A legacy codebase audit starts with clone detection and search, then traces the responsibility represented by each match. The review checks:

  • Authority: Which copy, specification, or test defines the intended rule.
  • Change history: Whether maintainers update the copies in the same work.
  • Divergence: Whether differences are intentional or accidental.
  • Boundary: Whether the copies sit inside one module or across independent services.
  • Verification: Whether tests compare equivalent inputs and outcomes.

Duplication inside a code hotspot deserves particular attention because repeated change increases the chance that copies drift apart. Stable copies at a protocol boundary may simply need documentation.

Removing duplication safely

The usual structural response is to identify the shared concept, record its current behaviours, and move it behind one named function, module, service, or policy. That is a refactoring only when observable behaviour remains stable. If the copies already disagree, the team must decide which outcome is correct before consolidating them.

Code smells around long methods or repeated conditionals can help locate duplication, but they do not dictate a shared abstraction. Combining code too early can create a dependency between responsibilities that only look similar today. A reviewable plan states what is genuinely shared, which variations remain explicit, how callers migrate, and which tests prove that the result preserves each required behaviour.