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.
Top comments (7)
In the modern era tabs are the way to go.
This is an overhang from when editors were simple and didn’t let you change the visual appearance of a tab and all handled them differently.
100% with you on this.
Yours sincerely,
a former space user (who used to use 3 spaces as that worked best for me 😱)
P.s. The only issues with this is I have never worked out how to use tabs in jsfiddle, codepen etc. If someone can enlighten me as to how to make that work I will be happy! ❤️🦄
Here's a post detailing how to change indents in CodePen
Thanks Siddarth, that is really useful! ❤
Welcome!
I'm more of a JSFiddle guy. They have a setting for tabs in their flyout menu. See "Indent with tabs"
Thank you!!!
How on Earth I never spotted that is beyond me (I was trying key combinations not thinking it would be a setting 🤦♂️)...that will save me a load of headaches! ❤
There's also accesibility to think about