Why tabs are better than spaces

⚠️ Warning!

Opinions in this article may differ from your own. Beware of mild generalizations.

Background

I've been writing code for over 20 years in several languages and all types of editors, including: Notepad, Visual Basic, Dreamweaver, TextMate, Coda, Sublime, Xcode, Visual Studio, and others.

For the majority of that time, I've been pretty adamant about 4 spaces.

Why I might be changing my mind

In very recent years, the popularity of a few frameworks has spawned a rise in cases of 2-space indentation. In my opinion, this level of indentation is too low and creates confusion. My assumption is that 2-space indentation was adopted to save horizontal real estate, but I'm not entirely sure why that would be required.

After all, if your code has to be indented to the point it becomes unreadable, you likely have other code-related issues to fix. In fairly broad strokes (⚠️), you have indentation for the following:

namespace
    class
        function
            conditional
                loop
                    logic

There's more than enough room to keep code within an 80 character margin with a 4-gap indentation even at 6 levels.

But why tabs?

I'm not on a mission to stop people from writing 2-gap indentation. If that's what you like, that's what you like.

When I write my code, I want to see 4-gap indentation even if I'm sharing the codebase with 2-gappers. This creates a problem because formatting the file will add/remove spaces on every line to meet indentation levels which causes your git diff to change virtually every line; unreasonable and irresponsible!

Tabs make more sense because each individual tab represents one logical level of indentation.

You may have noticed I changed my terminology in this article to say "2-gap" rather than "2-space" and that's because it's the visual representation of space that matters; not the actual character. Even though spaces require more disk space.

Within your editor, you can change the visual representation of a tab:

You can still get your 2-gap, 4-gap, 8-gap indentation level using tabs while working seamlessly with other developers. If Johnny is using 2-gap tabs, I'm using 4-gap tabs, and Maria is using 8-gap tabs... none of the actual characters change in our commits even though we can all feel comfortable with our preferred indentation sizes.

TL;DR

  • Each tab (\t / 0x09) represents one level of logical indentation, rather than having to compute/reduce spaces as a representation of indentation.

  • Tabs allow multiple developers to use their own indentation preferences without overriding formatting of others.

  • Forcing spaces of your own preference onto the other members of your team is a bit selfish.

21