embedded software boot camp

Whither white space?

Tuesday, October 20th, 2009 by Nigel Jones

I was looking over some code I wrote a year ago in preparation for making some minor enhancements, when I noticed that in one place I had two blank lines between functions, instead of my coding standard mandated one line. I immediately and instinctively corrected it – as is my norm. However having done so, I paused to consider what I’d just done – and why.

On the one hand, this was a clear violation of my coding standard – and so it must be corrected. However, the violation wasn’t doing any harm, per se, and correcting it came at a cost – namely that someone (probably me) browsing the version control system at a later date will see that the file has been touched – and may choose to investigate what was changed – only to find out that it was a simple white space correction. (I appreciate that version control systems can be set up to ignore white space. I choose to not use that option).

Now I suspect that readers of this blog will be divided. Some will think I was quite right to eliminate the extra line, whereas others are thinking – doesn’t this guy have better things to do in life? Which brings me to my point!

Some people are completely anal retentive when it comes to white space. They are very careful on indentation, alignment of comments, use of blank lines and so on. I fall squarely into this category – as does Jean Labrosse of MicroOS II fame. Others could not care less about white space. They will arbitrarily have 6 blank lines between two functions,and then no lines between the next two functions. Their comments are usually aligned all over the place, and they rarely use space between e.g. the elements of a for loop statement. Finally, there’s the third (and largest group) who fall somewhere in between these two extremes.

Now I look at a lot of code, andย  having done so, I think I can make a sweeping generalization, which I’ll call the “Nigel Jones white space principle”. Succinctly put, it states:

White space discipline is highly correlated with coding discipline.

That is, those who are careless about white space are often careless about a lot of other things. The converse seems to apply. As a result, when I look at code, literally the first thing I note is how well disciplined was the author in the use of white space. If the code is cleanly and consistently laid out, then I get a good first impression, and the chances are the code will be first rate.

Now I am unsure which is the cause and which is the effect here. In other words, does white space discipline lead to more disciplined code overall, or is it the other way around? Regardless, if your code looks like a mess, then I’d humbly suggest that you literally clean up your act – your career will thank you!


6 Responses to “Whither white space?”

  1. Gauthier says:

    That is so true, I'll send a link to my fellow programmers. As for which is the cause or the effect (which I suppose you meant by "affect"), I believe both are effects? Good spacing and code quality are both effects of a common cause, discipline.

  2. Nigel Jones says:

    I hate it when I make a stupid mistake like that – a lack of discipline I suppose! Anyway, corrected.

  3. Gauthier says:

    I knew you would take it hard. Which is rather a good sign.

  4. david collier says:

    If you aren't disciplined on ws, it will be harder to read the code.Even if it doesn't affect the number of bugs per square furlong, it makes them harder to spot.Some may say that someone who fusses over making the code look good will also fuss over making it work well, I couldn't possibly comment ๐Ÿ™‚

  5. Alan Bowens says:

    Amen to everything above. Consistent spacing, along with correctly spelt and grammatically correct comments, may not matter to the compiler, but they're indicative of a comforting attention to detail. I suspect there's a strong correlation between such artefacts and the quality of the code base.

  6. GregK says:

    If not that correction Nigel was close to win "obfuscate ansi c code" competition this year.see previous winner's code:http://www.ioccc.org/2000/dhyang.c(just joking ๐Ÿ™‚ )

Leave a Reply to Gauthier