It’s been a while I posted something here! If I’m doing it, it’s for a very worthy reason: there are currently too many people who are wrong, who think that tabs are better than spaces, or that spaces are better than tabs… I hope I can do something about it.
Here are some recent posts on this topic:
- The current state of things: https://medium.com/@hoffa/400-000-github-repositories-1-billion-files-14-terabytes-of-code-spaces-or-tabs-7cfe0b5dd7fd#.np1l4cbth shows indentation with spaces is more popular for most programming languages.
- A pro-spaces blog focusing only on alignment and forgetting about indentation and user preferences or needs: http://javarevisited.blogspot.fr/2016/09/why-use-spaces-over-tabs-for.html
Did you happen to wonder why there are tabs and spaces and why both are still used? There is a reason beyond all that: they serve different and compatible purposes!
Tabs are not a character per se, it’s a model of indentation, which can be rendered differently according to user preferences and editors (2, 4, 8 spaces; it’s up to you. For example, I use a 2-spaces wide tab in vim for XML, and a 4-spaces wide tab for the same in Eclipse IDE).
Tabs are not meant to align, they’re meant to conceptually highlight a “level” of a line in the file. This level can be imagined as an abstract column, with a width that’s not controlled by the author of the code. Tabs are used to group a block of successive lines with the same level together and to show them as more or less “deep” than some other lines, they appear on a same column independently of the tab width.
Tabs only make sense -and are perfect- at the beginning of a line, for indentation only. Their variable width rendering make them very bad to use for spacing and formatting content inside a line.
Spaces are for alignment. There size is exactly 1 character. If you have content with same level of indentation, then you can use spaces to align pieces of code or even parts of lines together.
However, spaces suck for indentation. They cannot be set by user to something more compact or wider (some editors have hacks to render spaces with different size than one character, but that simply highlights how the idea of indenting with spaces is wrong as some want them to behave just like tabs). They obviously consume more space in storage, and may even contribute to energy consumption and global warming more than tabs do. The only argument of spaces for indentation is that code looks the same on all editors and for all user. But it’s exactly a counter-argument, as we see above, customizing indentation rendering can be useful.
But once your line of code starts, after your indentation, then spaces become the only good whitespace to use until the next line! You can use it to align code, or to make a clear separation between 2 parts of the same line.
Now, what about mixing them? A drawing become viral on Twitter on this topic highlighting that people mixing them can upset the pro-tabs or pro-spaces. Well, it’s really a pity, because you should use tabs for indentation, and spaces for alignment, even on the same line!
This is exactly the purpose of those 2 character: the first one is an abstract whitespace and rendered freely, the other is concrete and mapped to an actual and immutable width of 1 character. They can and should be used together, each one for their specific use-cases.
And those are actually the default settings of Eclipse IDE! It indents with tabs, and allows to align with spaces; and in case you already used spaces after the indentation to align you code, it just shows the same indentation and alignment if you create a new line just after.
If you consider the definition of a tab and a space, and of indentation and alignment, Eclipse IDE defaults are simply the settings that are the more natural and efficient, the best ones, rather than the most popular ones.