CodeSOD: If We’re Good, Or Else

This post was originally published on this site

The Daily WTF

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

To read the full article click on the 'post' link at the top.