In computer programming, duplicate code is multiple occurrences of equivalent source code in a codebase. A duplicate code fragment is also known as a code clone, and the process of finding clones in source code is called clone detection. Duplicate code has multiple undesirable aspects. [1]
Whether fragments are classified as duplicate can be subjective. Fragments that are very small – such as a single statement – are probably not classified as duplicate. Additionally, fragments that is not exactly the same text might be considered duplicate code if they match except for less important aspects such as whitespace, comments and variable names. Even fragments that are only functionally equivalent may be classified as duplicate.
Code that includes duplicate functionality is more difficult to maintain because if it needs updating, there is risk that only some of the duplicates will be updated; leaving the others as-is. When code with a vulnerability is duplicated, the vulnerability exists in the duplicate even after it is fixed in one copy. [2] Refactoring to eliminate duplicate code can improve many software metrics, such as lines of code, cyclomatic complexity, and coupling. This may lead to shorter compilation time, lower cognitive load, less human error, and fewer forgotten or overlooked pieces of code.
However, not all code duplication can be refactored. [3] Clones may be the most effective solution if the programming language provides inadequate or overly complex abstractions, particularly if supported with user interface techniques such as simultaneous editing. Furthermore, the risk of breaking code when refactoring may outweigh maintenance benefit. [4] A study by Wagner, Abdulkhaleq, and Kaya concluded that while additional work must be done to keep duplicates in sync, if the programmers involved are aware of the duplicate code there weren't significantly more faults caused than in unduplicated code. [5] [ disputed – discuss ]
Another cost is memory size as duplicate code requires memory to store each copy.
Some practices that lead to duplicate code include:
Duplicate code is most commonly eliminated by moving the code to a function and replacing each duplicate with a call to that function.
For example, the following code calculates the average of an array of integers.
externintarray_a[];externintarray_b[];intsum_a=0;for(inti=0;i<4;i++)sum_a+=array_a[i];intaverage_a=sum_a/4;intsum_b=0;for(inti=0;i<4;i++)sum_b+=array_b[i];intaverage_b=sum_b/4;
The two loops can be rewritten as the function:
intcalc_average_of_four(int*array){intsum=0;for(inti=0;i<4;i++)sum+=array[i];returnsum/4;}
Using this function eliminates the duplicated code.
externintarray1[];externintarray2[];intaverage1=calc_average_of_four(array1);intaverage2=calc_average_of_four(array2);
The compiler might inline the calls such that the resulting machine code is identical for both versions. If the function is not inlined, then the additional overhead of the function calls will take longer to run by a relatively small amount.
A number of algorithms have been proposed to detect duplicate code. For example: