Today's anonymous submitter sends us a short snippet. They found this because they were going through code committed by an expensive third-party contractor, trying to track down a bug: every report in the database kept getting duplicated for some reason.

This code has been in production for over a decade, bugs and all:

if (reportStatuses.indexOf(newStatus > -1))
{
    // add report to database
}

This is server-side JavaScript running in NodeJS. The mistake here is easy to make, it's a simple transposition error. But it's also easy to catch. Any sort of testing at all would find it.

The specific problem, if you haven't spotted it, is where the comparison operator happens: we're passing newStatus > -1 into indexOf as a parameter: this is a boolean value. Now, neither true nor false are in the reportStatuses array, so indexOf returns -1. But -1 is a truthy value, so the condition evaluates to true, adding the report to the database, even if it's already there.

Our submitter writes:

How has no one noticed this? How is the company still in business? How does the world not come down crashing around us more every day?

How is the world not crashing down? Have you looked outside, recently? Tis the season to quote Clark Griswold:

Worse? How could things get any worse? Take a look around here, Ellen. We're at the threshold of hell.

[Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.