dual screen, programming, coding

Tabs vs. Spaces – Why Spaces are better than Tabs

I recently had a long discussion with a friend of mine about Tabs vs. Spaces – as in this TV series. I thought that this was no longer a big deal in 2021, but it seems that we still need to convince people to use spaces over tabs.

A quick Google search revealed more articles about preferring tabs over spaces. However, my personal impression while working on several open source projects and in my daily work was that no one (serious) ever used tabs.

This still seems to be the case, as and projects that use spaces make more money than projects using tabs. Yaml only supports spaces and Python strongly recommends using spaces over tabs. If you’re still not convinced, here are my top 2 reasons for using spaces over tabs.

Reason 1 – Tab indentation is editor specific

Some people argue this as a positive feature of tabs, while it is the exact opposite. Andrey Tarantsov wrote and excellent blog post about why tab indentation causes trouble when mixed with spaces.

You will always run into such issues after some time. The reason for this is certainly that it is always easier to type a space than a tab. Thus tabs and spaces get mixed up more often, although tab is the preferred indentation method. This leads to:

  • Ugly code on Github (and other platforms)
  • Error-prone code for programming languages that rely on code indentation
  • It is time wasting

Reason 2 – Tabs are non-printable characters

This is a huge issue when searching within code. Imagine you are looking for a variable named width but you also have frame-width and other variables inside your code. One way to search the former one is to prepend a space to the search. But that doesn’t (always) work with tabs. You either have to copy and paste it from somewhere else, or escape it using \t. This is super annoying in grep and most editors.

You will always have trouble typing a tab because the tab is often used to navigate an application.

But, but, but…

Some excuses I hear over and over again are the following:

  1. Smaller code size
  2. That’s what they are used for
  3. Custom indentation setting possible
  4. Requires multiple button presses

And to all of these I have to say: “Really? Why does it matter?” or “WTF? Are you serious?”

  1. Minify your code (like css or js) in production, if needed.
  2. But they cause more problems than they help.
  3. Why would you need this? Just pick a good default.
  4. Check your tooling, nobody needs to press spaces nowadays.

Final note

There are many more side effects you will encounter when using tabs. Such as pasting space indented code into a project using tabs, printing tabs in low-level software like serial terminals, etc… It is just a matter of time when got to that point. In Germany we say “Lernen durch Schmerz” – “Learning through pain”. Oh, and by the way… I personally use mostly 4 spaces, sometimes 2 😉

# .editorconfig
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = trueCode language: PHP (php)
Buy Me A Coffee

2 thoughts on “Tabs vs. Spaces – Why Spaces are better than Tabs”

  1. Reason 1: the link 404’s
    Reason 2:
    – add the space character *after* the word to the search term.
    (or even the relevant character after the word, like “:” or “=” if there isn’t a space immediately after)
    – Minify your code: tabs still provide smaller source file size for the repository.
    – But they cause more problems than they help: do they really?
    – Why would you need this?: why not?
    – Check your tooling, nobody needs to press spaces nowadays: that is really not a valid point, who uses that as an argument agains spaces is dumb, but then again… I’ve seen more pro-spaces people use this as a “moot argument” vs actual pro-tab people using it as an argument.

Leave a Comment

Your email address will not be published. Required fields are marked *