I have found several instances of bugs like this in dusty corners of our automation code base. Fortunately the person responsible for snippets like these has no longer with the company, but I'd like to warn my coworkers to watch out for it in code reviews.
var NumberList = new List<int>() {1, 2, 3, 11, 4, 5, 6, 7};
bool AllNumbersLessThanTen = true;
foreach (var number in NumberList)
{
if (number >= 10)
AllNumbersLessThanTen = false;
else
AllNumbersLessThanTen = true;
}
In the above snippet AllNumbersLessThanTen
is set to true at the end of the loop because on the last iteration since 7 >= 10
evaluates to false we go to the else statement which assigns AllNumbersLessThanTen
, even though 11 is obviously not less than ten. The resulting boolean flag is invalid. The obvious fix is to delete the else
and associated code block.
Is there a common name or way to describe this type of bug in QA or software development parlance I could use when writing or discussing bugs of this nature?