Indentation and alignment, tabs and spaces

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:

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.

Some examples:


Indent with tabs, align in lines with spaces. Items will remain aligned on all editors, tabs width rendering is up to user/editor.


Mark indentation with tabs, then align with previous line with spaces. The alignment is preserved across editors and users, although they can have different tab width rendering.


Even if rendering would be similar locally with tabs or spaces, use tabs for indent, and spaces for alignment. Then the alignment remains portable on all editors and user settings, independently of tabs width rendering.

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.

  1. #1 by Gauthier on March 16, 2017 - 9:46 am

    What tabs for indent break is max column requirement. If you have a column limit at 80 chars, and Bob writes code with tabs at width 2 to fulfils the column limit, Alice won’t be happy when she opens Bob’s code with her tabs at 4 and chars pop up outside her window.

    • #2 by Mickael Istria on March 16, 2017 - 3:49 pm

      Ok, although in many cases, the max column requirement isn’t necessary. This max column requirement is legacy of old school terminals, there are not many project that do (nor should) care about it nowadays, as there are good tools and code is more and more focused on human readability.

  2. #3 by Lucio Menci on June 5, 2017 - 12:02 pm

    How you setted the formatter profile? If I set tab policy as Spaces only, the formatting will use spaces to indent lines. If I set tab policy as mixed or Tabs only the formatting will use tabs to align in lines. I’d like that the auto format should indet as your sudgestion, but it seems that it can’t do it. Do you think a next update on Eclipse should permit that?

    Thank you.

    • #4 by Mickael Istria on June 12, 2017 - 1:20 pm

      I have the following formatting settings to help me achieve this kind of whitespace management:
      * Show whitespaces characters (that really allows to show how the code is indented and to take better decisions accordingly)
      * Leave tab as tab (don’t replace with spaces)
      * don’t enable any automatic whitespace substitution
      As I said, there is no automatic way to deal with it, as it’s hard for a code analyzer to understand whether you’re indenting or aligning; those settings simply allow to be more careful about it and avoid counter-productive automation of whitespaces.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: