CodeSOD: Exceptional Flags

Something I see in a lot of code, and generally dislike, is this pattern:

if (debug) { print("Some debugging message"); }

Obviously, the "right" answer here is to just use a logging framework and control the mode globally. Still, it's not uncommon to see these sorts of quick-and-dirty branches. I don't like them, but in many cases, they're not worth fighting over.

I bring this up because Drenab's submission, I believe, started with the same kind of intent. It's just, like so much bad code, absolutely misguided.

boolean flag = false; if (flag) { throw new Exception(); }

Clearly, the flag is meant as a compile time switch. Whoever wrote this wanted to stop normal execution at this point during debugging- perhaps not literal debugging, with an attached debugger, but some sort of debugging.

And you know what? While I don't like this- really don't like this- I can absolutely see writing this code to quickly inspect a problem I'm having a hard time replicating. What I can't see is including it in a commit. This is garbage code I don't intend to ever actually let anyone else see.

What I absolutely wouldn't do is chuck this snippet into a bunch of places in my codebase, which is what happened here. These blocks were spammed all over the place, and flipping flag would cause it to throw a generic exception.

[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.