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.
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: