3 ms·
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
by deafpolygon 3y ago
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?
- 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.
- audunw 3y agoThe 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.
- redhale 3y agoI 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?
- ajuc 3y agoSome people indent in a way that breaks with tabs. Like: object1.field1 = value1; longObject2.field3 = value2; longObject23.longField1 = value3; or if ((firstArg == 1 && x == 3) || (firstArg == sthElse && x == 4) || (thirdArg == 3 && fourthArg == 2) ) { ... }
- cyclotron3k 3y agoYou do the indenting with tabs and the layout with spaces. Then it doesn't break
- lttlrck 3y agoYes 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.
- cyclotron3k 3y agoI think we're talking about different things. The rule is: nothing should appear to the left of your tabs - use spaces everywhere else. Here's what it should look like (if you really want to go full-alignment): class Demo { \tfunction thing() { \t\tlet height = 123; \t\tlet width = 456; \t\tlet title = "Foo"; \t\tlet description = "Foo bar baz"; \t} }
- deafpolygon 3y agoI just don't indent code like that, but I understand now that others might - and in that case, tab alignment isn't always desirable.
- redhale 3y agoI 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.
- empthought 3y agoJamie Zawinski was an influential programming author 25 years ago. https://www.jwz.org/doc/tabs-vs-spaces.html https://www.jwz.org/doc/tabs-vs-spaces.html