The copy-paste programming method is a widely spread coding anti-pattern (a trap). This method is usually understood as multiple copying (with further editing) of existing code instead of creating general solutions. This programming style often produces excessively large, poorly readable functions that contain a lot of repeating code fragments. Such a code is difficult to comprehend, while fragments repeated many times weaken programmer's attention, which is a source of misprints. If some mistake was already made earlier, it will be multiplicated throughout the code.
These errors are so frequent (especially in large projects) because it is impossible in practice to avoid copying several lines in a row, for example, when calling one method with different (but similar in general) parameters and applying this method to different objects several times in a row. Note that manual check of such a code (code review) is rather inefficient because the human eye simply cannot distinguish differences in 1-2 characters.
It's best to catch copy-paste mistakes at the stage of code writing in automatic mode. For instance, you can use the static analysis methodology. Unlike ordinary manual review, static analysis is usually completely automated and covers the whole project code - even those fragments which are executed seldom and where errors are difficult to detect through dynamic verification methods.
Here are several examples of real errors of the copy-paste pattern in C++ found in popular open-source projects with the PVS-Studio static analyzer.
The Fennec Media Project project. A slip when handling array items.
fhead = '\0'; fhead = '\0'; fhead = '\0'; fhead = '\0';
The four similar lines must have appeared in the program through copying. Then the programmer made a mistake when editing indexes which caused zero to be written into 'fhead ' twice and not be written at all into 'fhead '.
The ReactOS project. Selecting an incorrect object.
HPEN hhi = CreatePen(0, 0, MAKE_PALETTERGB(crHighlight)); HPEN hsh = CreatePen(0, 0, MAKE_PALETTERGB(crShadow)); ... if(fNormal) hOld = SelectObject(hdc, hhi); else hOld = SelectObject(hdc, hhi); ...
The 'hsh' object is not used. It is the 'hhi' object which is used all the time.
More examples of copy-paste errors (and other issues) detected with the help of the static analysis methodology can be found here.
In a more general case, the copy-paste programming is understood as usage (or adaptation) of existing third-party solutions (open-source, for instance), often without understanding their principles. It in its turn leads to heterogeneous coding style of a project, inefficient operation and cluttering of the code.
- Wikipedia. Anti-pattern.
- Wikipedia. Copy and paste programming.
- Wikipedia. Cargo cult programming.
- Andrey Karpov. The Last Line Effect.
- Andrey Karpov. Consequences of using the Copy-Paste method in C++ programming and how to deal with it.