Top Qs
Timeline
Chat
Perspective

Duplicate code

Repeated fragment of computer source code From Wikipedia, the free encyclopedia

Remove ads

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.

Remove ads

Cost

Summarize
Perspective

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.

Remove ads

Emergence

Some practices that lead to duplicate code include:

Scrounging
Via copy and paste programming, a section of code is duplicated in the codebase instead of factored it into a reusable function.
Code snippets
Some development tools like LLMs[6] automate the process of inserting code snippets; code that is identical or functionality equivalent.
Coincidence
Similar code may be developed independently although studies suggest that such code is typically not syntactically similar.[7]
Generated
Automatically generated code may be duplicate but this may be done for runtime performance or ease of development.
Remove ads

Fixing

Thumb
Example of duplicate code fix via code replaced by the method

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.

extern int array_a[];
extern int array_b[];
 
int sum_a = 0;

for (int i = 0; i < 4; i++)
   sum_a += array_a[i];

int average_a = sum_a / 4;
 
int sum_b = 0;

for (int i = 0; i < 4; i++)
   sum_b += array_b[i];

int average_b = sum_b / 4;

The two loops can be rewritten as the function:

int calc_average_of_four(int* array) {
   int sum = 0;
   for (int i = 0; i < 4; i++)
       sum += array[i];

   return sum / 4;
}

Using this function eliminates the duplicated code.

extern int array1[];
extern int array2[];

int average1 = calc_average_of_four(array1);
int average2 = 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.

Detecting

A number of algorithms have been proposed to detect duplicate code. For example:

See also

Remove ads

References

Loading related searches...

Wikiwand - on

Seamless Wikipedia browsing. On steroids.

Remove ads