To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* 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
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* 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.

>
>
>
Eternal sunshine of the spotless copy-p…

Eternal sunshine of the spotless copy-paste

Jan 22 2020
Author:

Today let's recall the oldie but goodie article "The Last Line Effect" written more than 5 years ago. Time passes, but nothing changes. There is nothing dreadful in it. Copy-Paste is still cruel and merciless. However, over the years, many new readers have joined our blog who may not be familiar with that article. So now there will be a moment of memories and a few additions.

0708_Eternal_Sunshine_of_the_Spotless_CopyPaste/image1.png

Today I met the mention of the article "Last Line Effect" in the tweet from Jason Turner. As we can see, the Copy-Paste issue is alive and followed by lively discussions. So I decided it was worth dusting off that publication and reminding you of it. I'm sure many people haven't read this old article or have forgotten about it. But it is very interesting and amusing.

Here's the main point. People tend to make mistakes at the end of monotonous operations, as they lose attention. One such tedious thing is writing code with Copy-Paste.

If one has to write a piece of code that consists of similar blocks, people prefer to copy the block and edit it. So I noticed that most likely a mistake will be made in the last block.

This is the pattern with relevant examples that I describe in the article "Last line effect".

Anyway, I wouldn't like to retell the content of this article here. So if you haven't checked it out yet, please, follow the link and read it.

Unfortunately, the article was written at the time when the PVS-Studio analyzer wasn't able to analyze C# and Java projects yet. Therefore, all errors in the article relate to the C and C++ languages.

At this point, I can give similar examples related to other languages. Copy-Paste issue is universal and reveals itself everywhere in the same way. Not to be unfounded, I'll cite one example for C# and Java.

The example of an error we found in the C# AWS SDK for .NET project.

if (
  (this.token == JsonToken.ObjectEnd ||
  this.token == JsonToken.ArrayEnd ||
  this.token == JsonToken.String ||    // <=
  this.token == JsonToken.Boolean ||
  this.token == JsonToken.Double ||
  this.token == JsonToken.Int ||
  this.token == JsonToken.UInt ||
  this.token == JsonToken.Long ||
  this.token == JsonToken.ULong ||
  this.token == JsonToken.Null ||
  this.token == JsonToken.String       // <=
  ))
{
  ....
}

The example of an error we found in the Java Elasticsearch project.

for (int i = 0; i < values.length; i++) {
    if (values[i] == null) continue;
    if (values[i] instanceof String) continue;
    if (values[i] instanceof Text) continue;
    if (values[i] instanceof Long) continue;
    if (values[i] instanceof Integer) continue;
    if (values[i] instanceof Short) continue;
    if (values[i] instanceof Byte) continue;
    if (values[i] instanceof Double) continue;
    if (values[i] instanceof Float) continue;
    if (values[i] instanceof Boolean) continue;   // <=
    if (values[i] instanceof Boolean) continue;   // <=
    throw new IllegalArgumentException(....);
}

As you can see, everything is the same everywhere :). It's not surprising, as typos and failed Copy-Paste problems almost don't depend on the chosen language.

By the way, for those who want to delve deeper into the topic discussed, I offer the following research article, written by several authors: "The last line effect explained".

What shall you do now with this knowledge? Good question. I have three ideas:

  • Be aware of this effect and tell others. Knowing about it, you will become more accurate, when finishing same-type work of writing code. Knowledge is power!
  • So don't be lazy and write functions, template functions or lambda expressions to avoid code duplication. As for macros, don't write them at all. Marcos are evil.
  • Use PVS-Studio regularly. It's great at finding such errors.
Popular related articles
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept