Mark Needham

Thoughts on Software Development

If you use an 'if' you deserve to suffer

with 3 comments

One of the things I dislike the most when coding is writing if statements. and while I don't believe that if should be completely abolished from our toolkit, I think the anti if campaign started about a year ago is going along the right lines.

While there is certainly value in using an if statement as a guard block it usually feels that we have missed an abstraction if we are using it elsewhere.

Given my dislike of the construct, when I have to use it I like to being very explicit about the scope in which it is applicable. I think this approach stems from being caught out back in my university days and trying to debug something along the lines of:

if(thisHappens())
	doThis();
	doThat();

The code was much messier than that but I spent ages trying to work out why the correct behaviour wasn't happening. Eventually I realised that 'doThat()' was being called every time regardless of the value returned by 'thisHappens()'.

Ever since then I have written if statements like so:

if(thisHappens()) {
	doThis();
	doThat();
}

It doesn't look as nice but with one glance at the code I can see exactly what is happening. I definitely prefer to have this advantage although I do appreciate that if there is only one statement following the if statement then YAGNI might be applicable.

I prefer to take the more conservative approach – once bitten, twice shy.

So what is the title of this post all about?

Often when pair programming there is some discussion over which approach is better and in one such session last week a colleague of mine, who happened to favour the more verbose approach, came up with a phrase along those lines when I asked why they favoured that approach. If you're going to use an if statement then you're just going to have to put up with the eye sore those extra curly braces create in your code!

Being in favour of this approach already I really like the idea and while maybe not quite as scientific as describing the technical reasons for doing so, it is perhaps more effective.

Written by Mark Needham

October 21st, 2008 at 7:19 am

Posted in Coding

Tagged with ,

3 Responses to 'If you use an 'if' you deserve to suffer'

Subscribe to comments with RSS or TrackBack to 'If you use an 'if' you deserve to suffer'.

  1. Use braces. Developers love leaving them out, but I find that it constantly results in developers forgetting them when adding a second condition. While the loss in time is often small, say a few minutes to figure it out on average, those situations can be avoided by just putting them in. Every little bit helps, and can add up to a lot.

    Also, I feel like you didn't pursue the concept of removing if's from a codebase entirely. As mentioned by the blog in the link you mentioned, if's that decide based on type are a clear smell for some polymorphism.

    On the other hand, there is a line where a simple if statement introduces less complexity than a pattern to get around it. I've worked with developers that when they see an if statement, they say "Hey, you can avoid that with a strategy pattern." While that may make sense in some cases, in others, it's going to result in the creation of a new object hierarchy, and only one code location that uses this forced polymorphic approach. Sometimes, you just can't beat a simple if statement.

    Kris Kemper

    21 Oct 08 at 12:57 pm

  2. …until you use a 'real' language like python or ruby that is, were brackets are frowned upon. Your first example is correct in python, because the indentation level is identical. ahhh…..languages that a pretty as well as smart.

  3. [...] If you use an ‘if’ you deserve to suffer at Mark Needham [...]

Leave a Reply