Why I prefer spaces to tabs in code (indentation & alignment)

Indentation is one of the key aspects of coding style, and the issue of using hard tabs or spaces (also referred to as soft tabs) is an ever-ongoing debate in the programming community. Some say it's just a matter of preference and consistency, while others go to great lengths to argue why theirs is the right choice.

My understanding is that there are some basic things that you should know about tabs and spaces before deciding which one's better for indenting your code.

(Note usage: ---> for tabs and .... for spaces.)

Tabs are meant for indentation

The sole purpose of tabs in code is indentation. It has no other general use, unlike spaces. So, why not use something that's actually meant for the purpose?

Tabs can be personalized

A tab is a single character just like a, b, c, etc. In most code editors a tab is equivalent to 4 spaces or colummns by default. Most importantly, any reasonably good editor 1 allows you to configure the width of the tab (also known as tab size). So you can configure your editor to display tabs at the size of 8 characters, if you so like it.

Remember, a tab is still a single character in your code; it's just that your editor configuration decides how big or small it's displayed to you.

But if you choose to use spaces for indentation, and say you prefer 4 spaces like many others do, then it's 4 characters in your code instead of one. The reason why this matters is that people have different preferences; some prefer 4 spaces, some 2, others 8 and so on, and they may find your code to be too compact or too spread out depending upon their indentation preferences.

Tabs allow people to automatically view code in their preferred indentation size.

Maximum line length limit? You need spaces

Coding styles for programming languages often recommend that lines be no longer than a specific number of characters (e.g. 80 character limit is common for Python), one of the reasons being readability.

As tabs are dynamic in nature, it's impossible for developers to conform to a maximum line length limit, unless they configure their editors to the same tab-size. But this takes away tabs' most evangelized feature—personalization.

And then you have no control over tab-size in other places, say, the web. For example, GitHub would still display your indented code with 8-colum tabs.

So the answer? Spaces.

Tabs are easy to select

Say you chose to go with tabs. For some reason you later find spaces superior to tabs, or that tabs are an unnecessary complexity when dealing with coding styles, or whatever. You can easily, manually or use/write a tool, to replace tabs with a specific number of spaces; because tabs have only one meaning in code—they indent.

On the other hand spaces in code may have different meanings. For instance, it's common to use spaces for indentation and alignment. E.g.

int f(int x,
......int y) {
....return g(x,
.............y);
}

As you can see in the sample code above, in lines 2 and 4, spaces are used solely for alignment (after breaking lines for readability). So if your code looks like this in places, you can't simply replace spaces with more/less number of spaces or tabs, and be done with it. You'll break the alignment that you so cherish and your code will look messy.

Spacebar's function is universal

Anywhere you can type text hitting the SPACEBAR key produces a space character. Obvious, right? Not the case with the TAB key. For instance, press the TAB key in a textarea on a web page. You'll notice that, instead of producing a tab character, the control/focus switches from the textarea to the next link or form element on the page; because the browsers chose to use the TAB key for that purpose.

How about tabs for indentation and spaces for alignment?

As in:

int f(int x,
......int y) {
--->return g(x,
--->.........y);
}

Notice that in:

  • Line 2: We are aligning code with the previous line.
  • Line 3: We are indenting code.
  • Line 4: As the code in this line is broken from an indented line itself, we are using tabs for indenting, and spaces for aligning code with the previous line.

This is what people mean when they say, "Use tabs for indentation and spaces for alignment." Some people also mistake this for mixing whitespaces, which it isn't. This is what mixing whitespaces would look like:

int f(int x,
--->..int y) {
--->return g(x,
--->.........y);
}

That is one of the reasons why the practise of using tabs for indentation and spaces for alignment is frowned upon by some—most people who write code just don't get it (they mix up indentation and alignment) and it's easy to get wrong if you don't always keep your head straight.

So, why mix things up when we can simply use spaces for both indentation and alignment?

So, tabs or spaces?

I really like tabs, but consistency is very important for collaboration, even more so than personalization/flexibilty, and it isn't possible without hard and fast rules. And the fact that limiting line length, to keep the code readable and consistently so, isn't possible due to the dynamic nature of tabs, makes it clear that using spaces is the way to go.


  1. Popular editors:

Questions, Feedback & Suggestions