Tabs Versus Spaces
Recently, I was talking with Will Farrington about that age-old debate, whether to use tabs or spaces to indent code. As a religious war, it would seem that there can be no resolution; the opposing factions are doomed to disagree1. However, I believe that despite the religious nature of the issue, I have found a resolution.
For the uninitiate, a “hard tab” is the character that appears when you press the Tab key in most programs. When writing code, indentation is used to make the structure of the code more clear. For example:
def print_each(list) # The inside of a function is indented for entry in list # The inside of a loop within the function is indented again puts entry end end
There’s considerable debate over whether it’s preferable to use one tab for each level of indentation or several spaces. The arguments both for spaces and for tabs have already been covered more than amply, so I won’t bother going over them.
This is one of the oldest and deepest of the religious wars that programmers love to engage in, just below Emacs versus Vim. Just like any of these wars, neither side is clearly better, and which side you’re on depends more on what you’re familiar with than an unbiased consideration of the pros and cons of each side. I prefer spaces to tabs, largely because my third-quarter Software Design and Development instructor had us use spaces. Do I prefer them because of the good reasons she offered for using them, or because I just got used to them? It’s hard to say for sure2.
The tabs vs. spaces war does have one difference that sets it apart from most other religious wars, though. It’s a war about style. In issues of style, there is one rule that always holds true. This rule can be applied to any sort of style, be it programming, writing, or anything else. It can be applied objectively, and it never fails. It’s also very simple: be consistent. Inconsistent style is always wrong.
I read an interesting blog post to a similar effect a few days ago. For the life of me I can’t find the link - if you know what I’m talking about, link it in the comments and I’ll edit it in. The author argued that, for any given stylistic choice, neither option could possibly do more harm than inconsistent application of both options. This is definitely the case here: consistent indentation style is far more important than having tabs or having spaces, regardless of which you prefer.
This is most obviously true for a single project; if you have tabs in one file and spaces in another, or worse a mixture in a single file, you will be smote from on high. However, it also applies to the set of all code. It’s better if all code, at least for a given language, follows a single, consistent style. Many editors can only set the “tab” key to produce spaces at a global-level; they can’t change it based on filetype, let alone set it for individual files. UI is broken; integrating code becomes (more of) a pain.
Interestingly, it seems that a substantial majority of coders has chosen spaces over tabs. Ruby has settled on two spaces; Python explicitly says that four spaces is preferable. According to Google Code Search, there are 2.4 times as many documents that have lines beginning with spaces than there are documents that contain any tab characters at all3.
To sum this up:
- The best way to make a stylistic choice is to pick the most common option.
- More people use spaces than tabs for indentation.
The conclusion is clear. In the interest of consistency, always indent with spaces.
1 Incidentally, I did agree with Will, but we discussed it nonetheless.
2 I do find the arguments for spaces more convincing than those for tabs, but I admit that this could be confirmation bias.
3 I searched for ^ + and \t
for spaces and tabs, respectively.
About Me
Crazy Days and Almost Haml 1.7


