You are on page 1of 6

To get a trial key

fill out the form below

Why do we ask to use Business Email?

Team License (standard version)

Enterprise License (extended version)

Submit

Didn't get our email?

* By clicking this button you agree to our  Privacy Policy  statement

Request our prices

Why do we ask to use Business Email?

New License

License Renewal

 I am a reseller
--Select currency--

Submit
Didn't get our email?

* By clicking this button you agree to our  Privacy Policy  statement

Ask us your question

Why do we ask to use Business Email?

Please attach your files here. The total size limit is 20MB. The .exe and .i files are not
supported.

Submit

Didn't get our email?

* By clicking this button you agree to our  Privacy Policy  statement

To get the licence for your open-source project, please fill out this form

Why do we ask to use Business Email?


Get a license

Didn't get our email?

* By clicking this button you agree to our  Privacy Policy  statement

To get the licence for your open-source project, please fill out this form

Why do we ask to use Business Email?

Get a license

Didn't get our email?

* By clicking this button you agree to our  Privacy Policy  statement

I am interested to try it on the platforms:

Windows
Linux

macOS

Why do we ask to use Business Email?

Submit form

Didn't get our email?

* By clicking this button you agree to our  Privacy Policy  statement


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.

Download

Analyzer

Documentation

What's new in 7.14

Prices

For clients

Publications

About us

Home
>

Posts

>

Terminology

>

Copy-Paste programming

Aug 14 2012

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

 Wikipedia. Anti-pattern.

 Wikipedia. Copy and paste programming.

 Wikipedia. Cargo cult programming.

 Andrey Karpov. The Last Line Effect.

 Andrey Karpov. Consequences of using the 

You might also like