Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
Copy-Paste programming

Copy-Paste programming

Aug 14 2012

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));
...
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.

References

Popular related articles


Comments (0)

Next comments next comments
close comment form