IF you must

I have a lot of gripes in life. Most of them relate to driving. Thankfully as a mostly work from home developer I only need to drive into the office once a week. During that drive to Park City I consistently contend with slow drivers in the passing lane, which annoys me almost as much as a people who think using their turn signal is optional. This post isn’t about driving though, its about a bigger gripe: people who do IF statements wrong. Let’s look at the common types of badly written if statements that make me want to fire developers.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
– Martin Fowler, 2008.

The Linophobic Programmer

This level of insanity exists. I once saw 30 lines of crap like this. For one, you’re doing it wrong if you need that many IF statements, try using a switch statement, its kinda awesome. Regardless my eyes didn’t quite bleed, but I did tense up and feel “the rage” within. No one is giving you a gold medal for reducing the amount of lines in your code by failing to use brackets. PHP is C-style language, use brackets and make your code more readable for the love of god. It’s times like this where I wish I developed in Python since you’d get a parse error with shit like this. Is this really so bad:

The Horizontal Fan Boy

Because we all love scrolling horizontally right? I mean that’s how we wrote papers in school right? Thats how we all love reading web pages right? With fucking horizontal scroll bars, right?!?! There comes a time when you have to take a deep breathe and just break this into multiple lines….but not like this:

The Drunken If

Oh thanks for not forcing me to use my horizontal scroll bar, but I still can’t read your shitty code.  I swear if I ever saw this code given to me in an interview setting I would end the interview real quick. I don’t care if this shit could pass a Turing Test, you just failed a readability test big time.

Readability is important

The code listed above is actually a form of technical debt. When a new developer comes in it takes him/her that much longer to figure out what’s going. Hell it probably takes the original developer twice as long to figure it out. There is nothing wrong with nested if statements when used to improve readability. Example:

Now sure I created more lines of code, but any developer can come in and understand and if its still a little to complex guess what?

Oh wow, we’ll broken out code is easier to add comments too! What a compelling reason to write readable code. And please spare me your pleas for performance. Increasing the amount of IF statements for readability is unnoticeable to the human eye and likely doesn’t add even 1 cycle to a processor. Spend a few minutes verifying proper indexes within your databases or tackling one inefficient query if you are actually so concerned with performance.

Perhaps you thought that “getting it working” was the first order of business for a professional developer. I hope by now, however, that this book has disabused you of that idea. The functionality that you create today has a good chance of changing in the next release, but the readability of your code will have a profound effect on all the changes that will ever be made.
– Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship