CodeSOD: If We’re Good, Or Else

There are some coding practices which likely arise from a good intent, but sometimes feel misplaced. For example, the "only one return from a function" tends to make code more complex and harder to read, instead of easier, ever if it theoretically makes debugging easier. I can at least understand the intent and reasoning behind it, even if I don't like it.

Danny D's former co-worker had their own little rules. Specifically, sometimes when you write an if statement, you definitely need an else. Other times, you don't. That's confusing, because you have to make a choice. Instead of making a choice, just always use an else. Always.

if (!(tf.getType() == matchingTimeframe.getType() && StringUtils.equals(tf.getParameter(), matchingTimeframe.getParameter()))) { .... } else { // we are still good }

Of course, you need to include a comment to explain what condition the else represents. On every if-statement in the application. Even- no especially the ones with a somewhat confusing to read negation test, like we see here. We are still good. We are still good.

[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.