|
Duplicate code is a computer programming term for a sequence of source code that occurs more than once in a program. A minimum requirement is usually applied to the quantity of code that must appear in a sequence for it to be considered duplicate rather than coincidentally similar. Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs. ...
Source code (commonly just source or code) is any series of statements written in some human-readable computer programming language. ...
Sequences of duplicate code are sometimes known as clones. The following are some of the ways in which two code sequences can be duplicates of each other: - character for character identical
- character for character identical with white space characters and comments being ignored
- token for token identical
- functionally identical
How duplicates are created
There are a number of reasons why duplicate code may be created, including: - Copy and paste programming, in which a section of code is copied "because it works".
- Functionality that is very similar to that in another part of a program is required and a developer independently writes code that is very similar to what exists elsewhere.
- Plagiarism, where code is simply copied without permission or attribution.
Copy and paste programming is an informal computer programming style that simply copies code from one program to another. ...
Plagiarism is the unauthorized use or close imitation of the language and thoughts of another author and the representation of them as ones own original work. ...
Detecting duplicate code A number of different algorithms have been proposed to detect duplicate code: - Baker's paper
- Swiss work on visual clone detection
- other people
Example of functionally duplicate code Consider the following code snippet for calculating the average of an array of integers It has been suggested that this article or section be merged with Snippet management. ...
In mathematics, an average or central tendency of a set (list) of data refers to a measure of the middle of the data set. ...
This article or section does not cite its references or sources. ...
The integers are commonly denoted by the above symbol. ...
extern int array1[]; extern int array2[]; int sum1 = 0; int sum2 = 0; int average1 = 0; int average2 = 0; for (int i = 0; i < 4; i++) { sum1 += array1[i]; } average1 = sum1/4; for (int i = 0; i < 4; i++) { sum2 += array2[i]; } average2 = sum2/4; The two loops can be rewritten as the single function: int calcAverage (int* Array_of_4) { int sum = 0; for (int i = 0; i < 4; i++) { sum += Array_of_4[i]; } return sum/4; } See also Dont Repeat Yourself (DRY, also known as Once and Only Once or Single Point of Truth (SPOT)) is a process philosophy aimed at reducing duplication, particularly in computing. ...
External links |