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:
- Smaller code size
- That’s what they are used for
- Custom indentation setting possible
- Requires multiple button presses
And to all of these I have to say: “Really? Why does it matter?” or “WTF? Are you serious?”
- Minify your code (like css or js) in production, if needed.
- But they cause more problems than they help.
- Why would you need this? Just pick a good default.
- Check your tooling, nobody needs to press spaces nowadays.
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 😉
Code language: PHP (php)
# .editorconfig [*] end_of_line = lf insert_final_newline = true charset = utf-8 indent_style = space indent_size = 4 trim_trailing_whitespace = true