Tabs Vs Spaces for indentation in Coding

Posted by: Alex on May 27, 2015

This is one of the most trivial areas of coding that people argue over, often bringing up very nitty, and picky points as a point of reasoning.

There is annoyingly, good reason to argue though. The indentation method you choose for your coding can have very surprising knock on effects in the future, so which one should you really use?

And before you even start reading, if you currently have no preference or opinion, I’m going to give you the answer.
For coding indentation, you should use:

Spaces – with 2 indents

Spaces with 2 indents are the go to answer that are generally most widely accepted and used, especially by big companies (Facebook / Stripe / Twitter API’s etc), so if you’re just starting out with coding, or have never even given it consideration before, 2 indented space’s are the go to method – this is the most commonly used setup, and unfortunately it’s better to just fit with the norm, unless you have strong opinions otherwise, but it’s generally these pointless arguments that just fragment the situation even further.
However, it’s important you use the same method as the rest of your development team (or try and get them to change ;)), consistency is the real best answer. So if that means using tabs, then so be it.

But why?
The way in which you indent your code may seem trivial, and for the most part it usually is. But it’s not until everything goes wrong that you really wished you had chosen the correct method and stuck with it.

Not all operating systems, code editors/IDE’s treat code indentation the same way. Tabs are the most common problem, some code editors will treat each tab indent as very small, where as others will treat it like a huge block. This leads to not all code indentation looking the same across multiple developers systems. With spaces you do not have this translation problem, a space, is always just 1 space.

The reason most companies/API’s/libraries etc choose 2 indented spaces is:

  • Consistency across development environments
  • Clean readability
  • Humanly scannable nested code – that doesn’t waste too much space
  • Ease of changing 2 indents (spaces) to 4 indents (spaces) etc, within most IDE’s (Sublime Text 2 and 3 etc)

The most important thing is that all of your development team choose an indentation method, indent size and stick with it. However, I generally urge towards spaces for indentation, as that’s the most common indentation method.
Not only that, use 2 indented space also, as most libraries/API’s you may import/use in your projects will generally use this spaced indenting method. So if you’re using tab indentation, you will have to convert the imported libraries to tabs.

If you’re a Sublime Text user you can add these rules to your Preferences.sublime-settings file to ensure tabs get converted to spaces and also use 2 spaces as a default indentation level:

"tab_size": 2,
"translate_tabs_to_spaces": true

Got opinions of your own? Add them in the comments or let me know on Twitter: @guwiidigital