Static Code Analysis and Copy-Paste Bugs

I was writing some fairly complex code recently and of course I generated bugs. But one particular kind of bug got me to thinking about a class of bugs which I’ve never really seen discussed. In my own mind I call these bug copy-paste bugs. When most developers write code, at some point they type code like:

age1Index = GetIndexOfNumericVariable((double)rsi.age1, age1BorderValues);

Then shortly later the dev will need to type something like:

age2Index = GetIndexOfNumericVariable((double)rsi.age2, age2BorderValues);

In other words, the new line is exactly like the old line but has “2” instead of “1”. So instead of retyping the second line from scratch, the dev will save time by copying the first line, pasting, and then change the “1”s to “2”s. The bug I call the copy-paste bug is when a mistake like this is made:

age2Index = GetIndexOfNumericVariable((double)rsi.age1, age2BorderValues);

The new line has two of the “1”s replaced but missed one of the changes. This type of bug is very common. I’ve participated in several studies of effectiveness of different testing techniques which used actual buggy code and the copy-paste bugs were among the most common of all bugs for certain kinds of software. Anyway, a tool which does static code analysis and checks for copy-paste bugs like this might be very useful, but I’ve never heard of such a tool.

This entry was posted in Software Test Automation. Bookmark the permalink.