4 ms·
Using tabs has the opposite of your intended effect: If there is any text which is vertically aligned beyond the first indent, then this alignment will break fo
by tbabb 9y ago
Using tabs has the opposite of your intended effect: If there is any text which is vertically aligned beyond the first indent, then this alignment will break for anyone who doesn't have your tab width preference. They're then forced to change their editor settings to view your file in a non-broken way. I get way more pissed about someone else "forcing their preferences on me" when this happens than when I run into a tidy, 2-space-indented file.
If you configure your editor to drop spaces when you hit 'tab', then the file is guaranteed to look the same for everyone, and stops being context-dependent-- a bad thing for plain text, IMO.
- agrajag 9y agoYour issue is why you see people say "tabs for indentation, spaces for alignment." If you're trying to vertically align text, use tabs to keep it at the same indentation as the thing you're aligning to, and use spaces to adjust to vertical alignment. This will keep the vertical alignment regardless of the preferred width of the tab.
- tbabb 9y agoSo for long lines to move an argument over to align with its predecessor on the previous line, like int f() { int someVariableName = myFunctionCall(expr(5), // <--- line A somethingElse(9), // <--- line B a_long * expression + involving(multiple.subExprs)); int d = somethingElse; // <-- line C } ...you're saying line A is indented with tabs, and line B with spaces? Because that will misalign if your tab setting isn't the same. Or are you saying that both A and B are indented with spaces? Then what about line C, which will misalign with A and B if your tab stop is a different width? Or do we indent the entire body of f() with spaces, and then in that case you're basically using spaces everywhere, which the good idea that sparked this whole thing?
- deleted 9y ago[deleted]
- jarvic 9y agoYou indent line A with tabs. You use tabs to line the start of line B up with the start of line A (indentation) and then use spaces to move it the rest of the way to where you want it (alignment).
- knodi123 9y agoAnd as an aside, it's worth pointing out that most editors will handle the initial tab part automatically, so it's not like you have to do mental math to think "okay, this many tabs, and now switch to spaces". Just at the end of line A, you hit enter and start hammering the space bar.
- theseoafs 9y agoA is indented with a tab. B is indented with a tab, plus the requisite number of spaces to line up with the above line. C is indented with a tab.
- abecedarius 9y agoDoes anyone actually do that? I've never noticed that style in any code I've read, but maybe that just means it works.
- theseoafs 9y agoI don't think anyone manually counts out their tabs and spaces, but I think every major text editor has a mode or plugin to make this automatic.
- pera 9y agoI think this is what parent meant: 1 int f() { 2 >---int someVariableName = myFunctionCall(expr(5), 3 >--- somethingElse(9), 4 >--- a_long * expression + involving(multiple.subExprs)); 5 >---int d = somethingElse; 6 } Tabs only for indentation (lines 2 to 5), spaces for alignment (lines 3 and 4). This is easy to achieve if you use Vim with Smart Tabs for instance (https://vim.sourceforge.io/scripts/script.php?script_id=231 https://vim.sourceforge.io/scripts/script.php?script_id=231).
- simonbw 9y agoI think he's saying use tabs on lines A, B, and C up until the indentation level (the "int"). Then on line B use spaces for all the whitespace from there until the text is aligned. /*t*/int f() { /*tabs*/int someVariableName = myFunctionCall(expr(5), // <--- line A /*tabs*//*--------------spaces--------------*/somethingElse(9), // <--- line B /*tabs*//*--------------spaces--------------*/a_long * expression + involving(multiple.subExprs)); /*tabs*/int d = somethingElse; // <-- line C /*t*/}
- Sammi 9y agoThere's no reason to do this silly mix of tabs and spaces. Just move all parameters to a newline have have sane consistency everywhere: int f() { <tab> someVariableName = myFunctionCall( <tab><tab> expr(5), <tab><tab> somethingElse(9), <tab><tab> a_long * expression + involving(multiple.subExprs) <tab> ); <tab> d = somethingElse; }
- dvdgsng 9y agoIndent with tabs. Align with spaces.
- slowmovintarget 9y agoIndent with the tab key, let the editor replace it with spaces. Soft tabs FTW. (F#, where indentation matters, and Clojure using Parinfer where indentation matters to get the form placement correct both make tab characters horrible to use.)
- gcb0 9y agovertical align serves no purpose. why would anyone ever considers it important? silly js devs and their var block...
- betenoire 9y agoIndentation is literally a form of vertical alignment. And unless white space is significant in your chosen PL, all forms indentation/spacing serves no purpose. What does you code look like?
- gcb0 9y agoyou know i meant vertical align on things that are not control blocks, such as assignment values and such. but nice try ;)
- tdeck 9y agoI always see this brought and wonder, what are people vertically alogning, and why? I generally see vertical alignment as an anti-pattern that makes it harder to change code.
- xaedes 9y ago"that makes it harder to change code". Hm,... that could actually be a feature! Adding inertia. Not that I would use that, but it makes me think..^^
- GavinAnderegg 9y agoThis is another style thing, I think. When writing Objective-C I always use spaces because lining things up is idiomatic to the way the language is written. Same with Python which has well-codified style rules for many multi-line argument lists and the like. However, beyond that, I don't tend to line up assignments or anything else that has me doing indenting with spaces AND tabs, so (at least with my code) this doesn't end up being an issue. If I was working on a project where there was a strong preference for indenting assignments, or any other case like this, I would use spaces throughout.