TABs vs Spaces. The end of the debate.
When writing source code, indenting is very important. Having a neat and clean programming style, let alone a precise and uniform one, is probably one of the most important keys when attaching example source code with a job application. I was myself asked to show some of my source code in my last two interviews. Nobody ever asked me to show any running program that I had made, though. Wonder why? A lot can be understood about the author just by glancing quickly at some source code.
Indenting makes the source code easier to read for us human beings, whereas the compiler doesn’t really care (except for some languages, where indentation applies as a syntax element). Even if you’re not a programmer, you can see the differences here:
Compiler friendly

Badly indented

Properly indented

There is, I guess, no question that the last one, labelled as “Properly indented”, is the most readable. Problem arise, though, when people start wondering what they should use as indenting character. Some prefer TABs, other prefer blank spaces. A TAB, the key on the left of the Q of most Qwerty keyboards, is a single character that a text editor can represent whatever way it wants. This is usually customizable by the user, of course, so she can decide that a TAB will be shown as 8 spaces, or 4, or 2.
You can hear all the time someone claiming, in turn, that TABs are evil or that spaces are evil, but the truth is that none is wrong, as long as you can indent.
I’ll use, as an example, a piece of source code taken from the ext3 module of the Linux kernel. The Linux programming guidelines recommend using TABs for indenting, and that they should be 8 spaces wide. Let’s have a look at some code.
8-space TAB

4-space TAB

2-space TAB

As you can see, the original intent of the author, was to have the variable names aligned. But that alignment gets screwed up as soon as a reader has a different space-size for her TABs. What’s wrong there? Let’s use a very useful Vim tip: the :set list command.
:set list

This way, we can actually see the TABs, as “>——-”. Of course there will be less dashes if part of the TAB area is occupied by some text. So, can you see what’s wrong with that? The author of that source code is using TABs not only for indenting, but also for aligning! That way his alignment gets messed up when somebody uses a different TAB size. The solution of this problem is to simply just use what ever you want for indenting, but use spaces for aligning. Indenting must only be that left margin that you give to some lines, but it’s not to be confused with alignment. If the author of that source code had used TABs at the beginning of the lines, but just blank spaces between the type and the name of the variables, his code would be as he meant it whatever indenting style one’s editor would use.
So, in the end, it doesn’t matter whether you use TABs or space, for indenting, as long as you use just spaces for aligning.
Useful Vim/Emacs tip
I like spaces, and add the following to the end of all of my source files:
/*
Local Variables:
mode:c++
c-basic-offset:2
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
c-tabs-mode:nil
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=2:tabstop=8:softtabstop=2
This way, if the reader uses Vim or Emacs (and maybe also gedit), her settings will be temporarily overridden by mine, so, if she’s going to change my code, there are little chances that she’ll mess up my indenting.
The :set line options I use are the following:
set listchars=tab:>-,eol:$,trail:.,extends:#
It helps me to also spot trailing spaces. I recommend everybody to use the :set list, as it will prevent you to accidentally mess up other’s indentation.
Related posts:
15 Comments »
RSS feed for comments on this post. TrackBack URI



But, if you are using a non fixed font to program -yes that kind of perverts exists- using spaces to align is not correct! You should use tabs!
You can not win the debate. Use whatever you want. I’m a tab boy, but it seems the space boys are more than us, so if you consider to share code you should use spaces.
Comment by educhana — May 14, 2007 @ 5:26 pm
@ educhana, comment #1
Hi,
you see, the point is not being a tab or a spaces boy. The point is indenting, rather than aligning, as people often do. I don’t care if you use tabs or spaces, as long as you use them to indent. To align, just use spaces.
As for the non fixed fonts when programming, let the fools rot. I mean seriously, you can’t be a serious programmer if you non-fixed font!
Anyway, thanks for the comment.
Comment by Salvatore Iovene — May 14, 2007 @ 5:36 pm
@educhana: Umm, if you’re using tabs for alignment and you switch from one proportional font to another, it MAY still break your alignment.
Elastic tabs need to be standardized!
http://nickgravgaard.com/elastictabstops/
Comment by Darren Embry — May 14, 2007 @ 9:52 pm
To align, just use spaces.As for the non fixed fonts when programming, let the fools rot.
Why are you “aligning” something after the first non-space character? You’re just asking for trouble, especially with non-fixed fonts. Plus what automatic code formatters understand this aligning?
Just put the type, space, and then variable name.
Comment by BlogReader — May 15, 2007 @ 5:46 am
@BlogReader, comment #4
I’m not aligning, that piece of code comes from the linux kernel, and that style is used a lot. Non-fixed fonts… who uses them for programming, and why didn’t anybody tell them that it’s a bad habit?
Comment by Salvatore Iovene — May 15, 2007 @ 9:19 am
“… his code would be as he meant it whatever indenting style one’s editor would use.”
The above is only true if the aligned text does not span different levels of indentation. This is the real reason why tabs are evil (for some users, in some situations).
For those of us who think that visual presentation of the source can be important to understanding its semantic content, spaces and fixed with fonts are the way to go.
Cheers,
Nick.
Comment by Nick Westgate — May 15, 2007 @ 9:24 am
I’m a text editor developer. And nothing is more repugnant to me than seeing people use spaces for indentation. Apart from the fact that it makes all text processing operations difficult, the most annoying thing about spaces is that it disobeys the indentation preferences of users. I’ve rewriting in detail about this on my blog.
http://mystilleef.blogspot.com/2006/11/indentation-with-spaces-considered.html
PLEASE use tabs for indentation. For usability and accessibility purposes. If you don’t care about these or you don’t care about the preferences of people reading your code, then use spaces.
Cheers
Comment by mystilleef — May 15, 2007 @ 12:45 pm
@mystilleef, comment #7
Hi. The purpose of this article wasn’t to promote spaces over tabs. I’m ok with both. The only problem is that tabs must not be used for aligning, like in the variables of the struct mentioned in the article.
Comment by Salvatore Iovene — May 15, 2007 @ 1:02 pm
I’m a tab person myself. But spaces has won out. Conform or die.
Long live spaces.
Comment by jc — May 15, 2007 @ 10:04 pm
I was thinking of writing an article on that … but there, you’ve said it all.
Many coders I see around me still don’t get the difference between the uses of tab and space.
Any people advocating the use of one over the other, or pointing out a fantasized shortcoming of the “tab method” (including Nick Westgate) have still not got it and are a waste of office space ! ;oP
Comment by Francois Hill — August 7, 2007 @ 8:41 pm
I totally agree with the aricles view on using spaces for alignment, and have an Emacs question: Since I want to use tabs for indentation (so people can view it as they want) and spaces for my alignment, it would be great if the auto-indent (pressing ) functionality used tabs, and pressing C-q produced spaces. But the variable ‘indent-tabs-mode’ that switches spaces/tabs-behaviour, unfortunately changes it for BOTH situations so that I can’t mix the styles as I want. What do to, what to do…?
Comment by Stefan Persson — August 16, 2007 @ 2:52 pm
Nick Westgate (#6), you totally missed the point. The article explicitly states that tabs should not be used for alignment. YOU draw the conclusion that tabs are therefore evil, and spaces should be used, disregarding the articles statement that for indenting tabs vs. spaces is a useless discussion nobody can win.
So to conclude, once again, don’t use tabs for aligment. For indenting use tabs or spaces. Period.
Comment by Mike — June 6, 2008 @ 10:42 am
Mike (#16), you got the point completely.
Comment by Salvatore Iovene — June 6, 2008 @ 10:58 am
Rimsky went legate left buy cytotec meat steamed estivities.
Comment by Pwhndvve — August 9, 2008 @ 7:40 pm
I like using tabs for indentation because it allows multiple editing the same code to use whatever scale of indentation that they want, assuming they’re using an editor that allows them to set the width of tabs. It just seems more friendly, since you don’t forge everyone reading your code into the same indentation depth as you use.
Obviously, I use spaces for alignment.
Comment by Jeremy Banks — September 20, 2008 @ 4:33 am