I have always loved tabs, but the general consensus and insistence that Spaces are the Way(tm) has made me switch begrudgingly because I want to be a good team player.
I love that I can set tabs to a certain size and it all just works. Nowadays I have to use an editor that already has spaces in mind just to edit. I used to be able to edit code in Notepad, but with some codebases using 2 spaces, or 4 spaces... I've even seen some with 8 (!!).. I have to use an editor that understands spaces = tabs (VS Code, for example).
There are days when I want to switch sizes, just because of my little neurodivergent brain.
I'd love to hear why spaces became the dominant form of indenting code. Does anyone know the history?
> I'd love to hear why spaces became the dominant form of indenting code. Does anyone know the history?
My take is it's because when people use tabs, they don't only use it for "regular" code indentation, which remains consistent when different people configure their editor for a different visual tab width. They also use it for things intended to line up, like this:
void actions() {
int bat = (get_bat_thing()
+ the_other_thing());
int turn = (get_turn_thing()
+ the_other_thing());
return bat + turn;
}
Obviously that'a a made up example to illustrate. Line 2, 4 and 6 are "regular" indentation, and if it's done with a tab, you can change displayed tab width to taste, and the code will still look consistent.
But the lines 3 and 5 are intended to match up with the expression, and that breaks when someone has used tabs (followed by a few spaces).
Changing displayed tab width makes lines like that misaligned and ugly. Even worse, it does so in an inconsistent way in different places in the code. If the example was written with 4-wide tabs (matching the regular indent on line 2), then you display it with 2-wide tabs, it will change the line 3 and 5 indentation inconsistently:
void actions() {
int bat = (get_bat_thing()
+ the_other_thing());
int turn = (get_turn_thing()
+ the_other_thing());
return bat + turn;
}
After the person using an editor configured with 2-wide tabs edits the file and lines up their code, it is then indented inconsistently for the person using 4-wide tabs. They might not even realise. If a team does this for long, different parts of the code end up inconsistent in different ways from each other; a messy repo.
This messy repo problem is avoided by mandating spaces.
On the face of it, this seems to take away your personal flexibility to change visual indentation locally. But this isn't really true. In principle, your editor could change how it displays the code, as if those spaces were really the equivalent tabs, and then use a different displayed tab width, distinguishing between regular and expression indentation in the editor to keep various alignments.
I can see that in the examples you have given, where tabs might not be desirable. I dislike doing the manual alignment of code across multiple lines, anyways, so maybe that is why I haven't really come across it in my own code. If it doesn't fit in a single line neatly, I try to rewrite it so that it does... but I can understand this isn't always possible.
The implication in the article and your comment is that tabs let you switch indentation level without any negative consequences. But that’s clearly false.
A lot of code will use space to align code over multiple lines. If you only use tabs, the alignment will be broken in other developers editors if they don’t have the same tab indentation.
The author complains about visual noise with 2 level indentation. But misaligned code can become a complete unreadable mess. You end up being forced to set your tab indentation to the same value as the original author anyway.
So the logical conclusion then may be to use tabs for indentation and spaces for alignment. That would work in theory, but handling that correctly in an editor is nearly impossible.
So then you end up with the conclusion that spaces is the only way that doesn’t have any serious downsides. It’s far from ideal, but i don’t think the purported benefits that tabs have of letting you change indentation later works in practice anyway.
I’ve experienced this personally in a huge code base with multiple authors where some earlier code used tabs. It was always a 50% chance that code with tabs looked like hot garbage as the author had a different tab level than yours. After enforcing using only spaces, at least the code looks fairly organised most of the time.
I don't understand what you mean. Is this an IDE thing? Do you mean the IDE/editor is auto-wrapping long lines (visually)? If not, why would manually indented lines not also use tabs if tabs were the standard?
Yes that works. Until a variable name changes which could cause the alignment to cross a tab boundary so then instead of just fixing the alignment with spaces you have to convert existing spaces to tabs and realign.
It's just more work and to do it reliably requires making tabs visible in the editor though, which is another mark against tabs for many, I'd imagine.
I suppose there may be Vim/Emacs realignment macros that take care of it but then we're heading toward the other classic argument.
I was reading in an app (Materialistic) and none of that spacing rendered so I was confused. But I see it on the web.
Personally, I really hate code formatted like this. I don't really think it's any easier to read, and I find it obnoxious to write/maintain unless the editor does it for you. Just my opinion.
I have ADHD and letting ppl define how much indent they have equals to not having a standard. Some will choose 3 and you will curse them for life. 4 spaces is the only way, rest sbould be burnt in hell.
Did you read the article? This is the point, or rather the inverse. Why do you need the code to look the same on your machine as on mine? So what if I prefer wider indents? Tabs allow flexibility of viewing experience while keeping the code itself consistent.
It matters in that the code you write looks great for you, but misaligned for everyone else.
If we are coding as a team, the code needs to be easily readable by everyone, not just by your IDE -- and without having to change my IDE to suit each developer's personal preference of tab size
I honestly don't understand what you mean by "without having to change my IDE ...".
Because that's exactly it -- you set your IDE to make tabs the width that is easy to read for you. I do the same. We both get readable-to-us code without imposing our visual preference on each other.
I don't understand the downside of this approach. Am I just missing something obvious? Honestly asking.
No, Betamax was worse for most uses than VHS. The picture quality of Betamax was better at roughly the same tape speed, but the cassettes held about half the amount of tape. Slowing down the tape to improve capacity made the quality advantage go away.
Use an autoformatter with formatting rules defined and committed with the code so your team never has to argue about code style again. It shouldn't even be possible for that to come up in code review since it should be an automated check.
25 comments
[ 3.0 ms ] story [ 85.8 ms ] threadI love that I can set tabs to a certain size and it all just works. Nowadays I have to use an editor that already has spaces in mind just to edit. I used to be able to edit code in Notepad, but with some codebases using 2 spaces, or 4 spaces... I've even seen some with 8 (!!).. I have to use an editor that understands spaces = tabs (VS Code, for example).
There are days when I want to switch sizes, just because of my little neurodivergent brain.
I'd love to hear why spaces became the dominant form of indenting code. Does anyone know the history?
My take is it's because when people use tabs, they don't only use it for "regular" code indentation, which remains consistent when different people configure their editor for a different visual tab width. They also use it for things intended to line up, like this:
Obviously that'a a made up example to illustrate. Line 2, 4 and 6 are "regular" indentation, and if it's done with a tab, you can change displayed tab width to taste, and the code will still look consistent.But the lines 3 and 5 are intended to match up with the expression, and that breaks when someone has used tabs (followed by a few spaces).
Changing displayed tab width makes lines like that misaligned and ugly. Even worse, it does so in an inconsistent way in different places in the code. If the example was written with 4-wide tabs (matching the regular indent on line 2), then you display it with 2-wide tabs, it will change the line 3 and 5 indentation inconsistently:
After the person using an editor configured with 2-wide tabs edits the file and lines up their code, it is then indented inconsistently for the person using 4-wide tabs. They might not even realise. If a team does this for long, different parts of the code end up inconsistent in different ways from each other; a messy repo.This messy repo problem is avoided by mandating spaces.
On the face of it, this seems to take away your personal flexibility to change visual indentation locally. But this isn't really true. In principle, your editor could change how it displays the code, as if those spaces were really the equivalent tabs, and then use a different displayed tab width, distinguishing between regular and expression indentation in the editor to keep various alignments.
A lot of code will use space to align code over multiple lines. If you only use tabs, the alignment will be broken in other developers editors if they don’t have the same tab indentation.
The author complains about visual noise with 2 level indentation. But misaligned code can become a complete unreadable mess. You end up being forced to set your tab indentation to the same value as the original author anyway.
So the logical conclusion then may be to use tabs for indentation and spaces for alignment. That would work in theory, but handling that correctly in an editor is nearly impossible.
So then you end up with the conclusion that spaces is the only way that doesn’t have any serious downsides. It’s far from ideal, but i don’t think the purported benefits that tabs have of letting you change indentation later works in practice anyway.
I’ve experienced this personally in a huge code base with multiple authors where some earlier code used tabs. It was always a 50% chance that code with tabs looked like hot garbage as the author had a different tab level than yours. After enforcing using only spaces, at least the code looks fairly organised most of the time.
It's just more work and to do it reliably requires making tabs visible in the editor though, which is another mark against tabs for many, I'd imagine.
I suppose there may be Vim/Emacs realignment macros that take care of it but then we're heading toward the other classic argument.
Here's what it should look like (if you really want to go full-alignment):
Personally, I really hate code formatted like this. I don't really think it's any easier to read, and I find it obnoxious to write/maintain unless the editor does it for you. Just my opinion.
https://www.jwz.org/doc/tabs-vs-spaces.html
Monospace spaces are consistent across every machine
While the fight over "how many spaces" can be tricky, at least the code looks consistent once a standard is found
It matters in that the code you write looks great for you, but misaligned for everyone else.
If we are coding as a team, the code needs to be easily readable by everyone, not just by your IDE -- and without having to change my IDE to suit each developer's personal preference of tab size
Because that's exactly it -- you set your IDE to make tabs the width that is easy to read for you. I do the same. We both get readable-to-us code without imposing our visual preference on each other.
I don't understand the downside of this approach. Am I just missing something obvious? Honestly asking.
There is added value to collectively agreeing on one format. I think that added value can easily outperform the technical merits of a specific format.
No, Betamax was worse for most uses than VHS. The picture quality of Betamax was better at roughly the same tape speed, but the cassettes held about half the amount of tape. Slowing down the tape to improve capacity made the quality advantage go away.
colon newline, space space space space space space space space if x equal equal 5 colon newline, ...