CodeSOD: Duplicate Reports

Date:

Share:

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.


.comment { border: none; }

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

Source link

Subscribe to our magazine

━ more like this

Conservatives Boycott All Forms Of Entertainment

WASHINGTON—Decrying the un-American nature of any activity intended to provide amusement or the slightest bit of diversion, conservatives across the country announced an immediate...

Jess Cartner-Morley’s February style essentials: joyful jumpers, 24-hour earrings and the world’s most flattering tee | Fashion

Let’s get real. Few of us look or feel at our most fabulous in February. It’s been cold and dark for, what, 18 months?...

Prepping for Halloween! – From Head To Toe

It's finally AUTUMN and you know what that means! Cozy sweaters. Hot chocolate. A husband who stops complaining about being overheated (I have a...

676: A Sternly Worded Instruction

Pre-show: Snowpocalypse update Follow-up: atp.fm passkey update Google Private AI Compute Apple 🤝🏻 Google Cloud Removing copy/paste crud (via rauli) Screen on RAM ASUS improves color accuracy for Mac users (via...