Indent with tabs
Some days ago a heated discussion broke out in a tech group I’m in. It was the old debate of tabs vs. spaces. I thought this debate was over, but apparently I was wrong…
I will answer what I found to be the most common arguments within the next paragraphs.
For a short summary from a Rust user click here
It screws up the formatting
No, it doesn’t. We are talking about indentation, not alignment. Take the following examples with a tabwidth of 2, 5, and 8. Why 5? to show that it works even with unconventiuonal values. The formatting does not change if you follow these simple rules:
- tabs must not be preceded by anything but tabs on one line (
^\t*
) - aligning items does not change the indentation level. That’s usually scope.
ladder.up()
.up()
.down()
.showStep();
ladder.up()
.up()
.down()
.showStep();
ladder.up()
.up()
.down()
.showStep();
If you happen to find an example where this does not work and changes what your code looks like (aside from the space before the beginning of the line), please send a snippet to me. I am most curious, as I have yet to see one.
Where it doesn’t work
I have recently found an example for something that does not work with various tab-sizes: ASCII-Art. If you are wondering “the what now?” Take the following example:
for i := 0; i < 20; i++ { // this comment
doStuff() // covers multiple lines
} // and that is an exceptionally dumb thing to do
for i := 0; i < 20; i++ { // this comment
doStuff() // covers multiple lines
} // and that is an exceptionally dumb thing to do
for i := 0; i < 20; i++ { // this comment
doStuff() // covers multiple lines
} // and that is an exceptionally dumb thing to do
While most programmers probably agree, that this is a rather dumb way to comment stuff, it’s nonetheless one that does not work across multiple tab sizes.
Does this outweigh the accessibility gains? Probably not.
The code looks different than on my screen
Granted, what’s your point? Not everyone wants the code to look as it does on your screen, as a matter of fact: sometimes it shouldn’t. Therefore tabs. Everyone should choose whether they want the reduced eye movement of 2 spaces, or the clear structure of 8 spaces. The choice of tabwidth is not a matter of people choosing their tabsize because they think it’s just awfully trendy. If you don’t care; leave it at what most editors consider the default nowadays: 4.
To each their own
An often overlooked part of the tabs vs. spaces debate is accessibility. Every brain works differently and you may have different needs depending on what you’re working with. To me 8 space tabs with a 80 character line length are a great way to “visually lint” my code. If I notice that my remaining line after indentation is only 30 characters, that’s a pretty good sign that my code can use some refactoring. In HTML a tabwidth of 8 is just overkill, as nesting is simply part of the structure, and we don’t really have the option to put it in a function or something similar.
This is obviously my personal taste, but it’s hard to argue with “every programmer needs their own environment”. If you constantly have to work against your tool, instead of with it; your efficiency will take a deep dive.
Aligning is hard to do
We are programmers. There are tools that do it for you. And even better: with these you can work in spaces and just create a git filter that formats all files according to agreed-upon standards.
Why would you though? I have yet to see anyone struggling with tabs who did not use “features” like JetBrains ironically named “Smart Tabs”. Follow the indent with tabs, align with spaces mantra and you’ll be fine.
Conclusion
Tabs are just superior when it comes to accessibility and therefore a better default. Do what you want on your monitor, but try to help the guys who are not as lucky as you are. Nobody is changing their tab-value just for the sake of it, imposing spaces for this reason is not only unreasonable, it may ultimately hinder development if a person with special needs is hired.
But that intern will mess it up
Allow me to add some words from experience here:
- Get a formatter. Really. Now.
- Set it to tabs and K&R braces (because we didn’t have enough war in one post)
- Run it automatically on save (most editors have this feature; if your’s does not: try upgrading from notepad)
- You’re done. Use whatever you want and the formatter makes your code nice to look at.
Further reading
- A prime example of crappy spaces-handling: https://teddit.net/r/javascript/comments/c8drjo/nobody_talks_about_the_real_reason_to_use_tabs/esr3s9r/
- Tabs for accessibility: https://teddit.net/r/javascript/comments/c8drjo/nobody_talks_about_the_real_reason_to_use_tabs/
- https://www.kernel.org/doc/html/v4.10/process/coding-style.html
- https://www.i-programmer.info/news/99-professional/10049-tabs-or-spaces-one-billion-files-later-an-answer.html
- https://www.kirsle.net/tabs-vs-spaces