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.
Cost
editCode 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.
Emergence
editSome 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 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.[6]
- Generated
- Automatically generated code may be duplicate but this may be done for runtime performance or ease of development.
Fixing
editDuplicate 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
editA number of algorithms have been proposed to detect duplicate code. For example:
- Baker's algorithm.[7]
- Rabin–Karp string search algorithm.
- Using abstract syntax trees.[8]
- Visual clone detection.[9]
- Count matrix clone detection.[10][11]
- Locality-sensitive hashing
- Anti-unification[12]
See also
edit- Abstraction principle (programming)
- Anti-pattern – Solution to a problem that may be commonly used but is generally a bad choice
- Data deduplication – Data processing technique to eliminate duplicate copies of repeating data
- Don't repeat yourself – Principle of software development
- List of tools for static code analysis
- Redundant code
- Rule of three (computer programming) – Refactoring rule of thumb
- Snippet (programming) – Small amount of source code used for productivity
References
edit- ^ Spinellis, Diomidis. "The Bad Code Spotter's Guide". InformIT.com. Retrieved 2008-06-06.
- ^ Li, Hongzhe; Kwon, Hyuckmin; Kwon, Jonghoon; Lee, Heejo (25 April 2016). "CLORIFI: software vulnerability discovery using code clone verification". Concurrency and Computation: Practice and Experience. 28 (6): 1900–1917. doi:10.1002/cpe.3532. S2CID 17363758.
- ^ Arcelli Fontana, Francesca; Zanoni, Marco; Ranchetti, Andrea; Ranchetti, Davide (2013). "Software Clone Detection and Refactoring" (PDF). ISRN Software Engineering. 2013: 1–8. doi:10.1155/2013/129437.
- ^ Kapser, C.; Godfrey, M.W., ""Cloning Considered Harmful" Considered Harmful," 13th Working Conference on Reverse Engineering (WCRE), pp. 19-28, Oct. 2006
- ^ Wagner, Stefan; Abdulkhaleq, Asim; Kaya, Kamer; Paar, Alexander (2016). "On the Relationship of Inconsistent Software Clones and Faults: An Empirical Study". 2016 IEEE 23rd International Conference on Software Analysis, Evolution, and Reengineering (SANER). pp. 79–89. arXiv:1611.08005. doi:10.1109/SANER.2016.94. ISBN 978-1-5090-1855-0. S2CID 3154845.
- ^ Code similarities beyond copy & paste by Elmar Juergens, Florian Deissenboeck, Benjamin Hummel.
- ^ Brenda S. Baker. A Program for Identifying Duplicated Code. Computing Science and Statistics, 24:49–57, 1992.
- ^ Ira D. Baxter, et al. Clone Detection Using Abstract Syntax Trees
- ^ Visual Detection of Duplicated Code Archived 2006-06-29 at the Wayback Machine by Matthias Rieger, Stephane Ducasse.
- ^ Yuan, Y. and Guo, Y. CMCD: Count Matrix Based Code Clone Detection, in 2011 18th Asia-Pacific Software Engineering Conference. IEEE, Dec. 2011, pp. 250–257.
- ^ Chen, X., Wang, A. Y., & Tempero, E. D. (2014). A Replication and Reproduction of Code Clone Detection Studies. In ACSC (pp. 105-114).
- ^ Bulychev, Peter, and Marius Minea. "Duplicate code detection using anti-unification." Proceedings of the Spring/Summer Young Researchers’ Colloquium on Software Engineering. No. 2. Федеральное государственное бюджетное учреждение науки Институт системного программирования Российской академии наук, 2008.