Article 5XJVC CodeSOD: If We're Good, Or Else

CodeSOD: If We're Good, Or Else

by
Remy Porter
from The Daily WTF on (#5XJVC)

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.

otter-icon.png [Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today! TheDailyWtf?d=yIl2AUoC8zA
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments