CodeSOD: Expanded Conditionals

A common anti-pattern is the "expanded conditional". You know the drill:

if (a == b && b != c) { return true; } else { return false; }

This could be condensed into a single return statement. It's annoying, and stupid, and frequently part of a WTF, but never a WTF in-and-of-itself.

Except for this one, found by Timo.

// check if the IDs match public bool CheckIfIdMatch(int currentProductId, int foundProductId) { // if the product matches with the target if (currentProductId == foundProductId) { // return true return true; } // else, return false return false; }

This is a perfect block of bad code. It takes a simple task (an equality comparison between integers) and stretches it out to a whole block of code with an expanded conditional. The comments are useless restatements of the code itself. And of course, it's such a "useful" method, it's invoked in all sorts of places throughout the code.

[Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.