March 05, 2013, 12:15 PM — A couple of weeks ago I wrote about some of things that I found most frustrating about being a programmer, back when I was a programmer. One of those things was the size of indentation in code. Somewhere along the line, I was trained (or just started on my own; can’t recall) to set the tab key in my editor to four spaces. That’s what I stuck with for years and got used to seeing. When I read someone else’s code that used two or eight character tabs, I found it annoying. It wasn’t a dogmatic thing for me; I didn’t care that much. Four spaces was just my personal preference.
Image credit: REUTERS/Jason Reed
In the programmer community, though, discussions of coding style such as the size of code indentation can quickly turn into a holy war. Some people tend to have very strong opinions on it. It flared up a little bit in the comments some people made on the article; but there are plenty of lengthier (and more heated) discussions on it elsewhere on the web.
Spacing and indentation in code are important, of course, to help organize things and to make it readable. Code is often read by different people, and the preferences (or training) can differ from programmer to programmer. Also, depending on what type of indentation is used, the same code opened on different operating systems can look different (and less legible). It’s not as trivial an issue as it may sound to the non-programmer, and it’s also an argument that will likely never end.
Among the choices are things like: hard tabs (that is, the tab key inserting the actual ASCII tab character) versus soft tabs (replacing the tab character with multiple spaces), how many columns wide shoud the indentation be, how to deal with indentation or spacing within a line (some prefer hard tabs at the start of lines, but soft tabs in the middle of lines), etc. There are pros and cons to each choice; hard tabs use less space in the file, but soft tabs will lead to consistent spacing across operating systems. Two spaces were better than four or eight when trying fit things on 80 character lines. On and on (and on) it can go.
In the aftermath of my blog post, I decided to see what, if any, consensus or, at least, official recommendation, there was regarding indentation and spacing for various programming languages or applications. I took 20 popular languages and platforms and tried to find an official coding style guide for each to see what it recommended for indentation. Here’s what I found:
Recommended indentation style by language/platform
|Language/platform||Tab type||Tab width|
For what it’s worth, 84% of these languages/platforms recommend using soft tabs and the average recommended width is 3.3 columns. Now, of course, organizations will often specify their own coding style guidelines which can differ from these and, ultimately, developers can do whatever they want. So, take from this very unscientific study what you will.
What I will take from it is that by setting my tab to four spaces, I was doing it (almost) right all along. I knew it! Well, technically, I guess I should have set my tabs to 3 spaces, but close enough.
Ok, let’s hear your preferred indentation style. Hard tabs or soft tabs? How many columns wide do you set the tab? Does your style differ depending on the language you’re coding in? Does your organization have its own coding style guide? Discuss (but civilly, please).
Read more of Phil Johnson's #Tech blog and follow the latest IT news at ITworld. Follow Phil on Twitter at @itwphiljohnson. For the latest IT news, analysis and how-tos, follow ITworld on Twitter and Facebook.