Copy-Paste programming

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[11] = '\0';
fhead[12] = '\0';
fhead[13] = '\0';
fhead[13] = '\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[13] ' twice and not be written at all into 'fhead[14] '.

The ReactOS project. Selecting an incorrect object.

HPEN hhi = CreatePen(0, 0, MAKE_PALETTERGB(crHighlight));
HPEN hsh = CreatePen(0, 0, MAKE_PALETTERGB(crShadow));
  hOld = SelectObject(hdc, hhi);
  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.


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++ and C#

goto PVS-Studio;