Defect Indicators: A largely similar or identical section of code appears in two or more places in the program or set of programs.
Risks: Having to modify identical code in multiple places:
- increases the likelihood of making slightly different modifications under the mistaken assumption that you have made identical ones,
- increases the time needed to make changes,
- increases maintenance costs in direct proportion to the number of times the code has been copied/pasted.
- If you see a block of code that looks familiar, backtrack through the already assessed code looking for similar blocks. If the blocks could be implemented as a (parameterised) subroutine, mark the second and subsequent blocks.
Remedy: Isolate a single copy of the code into a separate program (e.g., method, function, subroutine) and reuse it by calling it from the places in which it was used.
- Why You Should Use Routines, Routinely (Steve McConnell), 1998.
Don't Repeat Yourself (DRY):
The DRY principle: Don't Repeat Yourself,
Repetitive code indicates poor design:
Copy and Paste is a design error.