4 ms·
> 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'
by jlokier 3y ago
> 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.
- deafpolygon 3y agoI 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.