Incomputer programming,duplicate code is multiple occurrences of equivalentsource code in acodebase. A duplicate code fragment is also known as acode clone, and the process of finding clones in source code is calledclone detection. Duplicate code has multipleundesirable aspects.[1]
Whether fragments are classified as duplicate can be subjective. Fragments that are very small – such as a singlestatement – 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 aswhitespace,comments andvariable names. Even fragments that are only functionally equivalent may be classified as duplicate.
Code that includes duplicate functionality is more difficult tomaintain 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 avulnerability 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 aslines of code,cyclomatic complexity, andcoupling. This may lead to shorter compilation time, lowercognitive load, lesshuman 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 theprogramming language provides inadequate or overly complex abstractions, particularly if supported with user interface techniques such assimultaneous 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 theaverage of anarray ofintegers.
externintarray_a[4];externintarray_b[4];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(inta[]){intsum=0;for(inti=0;i<4;i++){sum+=a[i];}returnsum/4;}
Using this function eliminates the duplicated code.
externintarray1[4];externintarray2[4];intaverage1=calc_average_of_four(array1);intaverage2=calc_average_of_four(array2);
Thecompiler mightinline the calls such that the resultingmachine code is identical for both versions. If the function is not inlined, then theadditional 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: