3 ms·
Your 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 in
by agrajag 9y ago
Your 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; }