4 ms·
The issue with tabs as indent level is that there are two reasons to indent. The first is for logical scoping, where tabs do indeed make sense. An example would
by rocqua 7y ago
The issue with tabs as indent level is that there are two reasons to indent. The first is for logical scoping, where tabs do indeed make sense. An example would be
int main(void){
indentedStatement()
}
However, the second is for aligning multi-line statements. Something like:
int main(void){
foo(bar,
baz,
54
);
}
Here, the indenting on arguments baz and 54 really should be aligned with the argument bar. If we really want to combine both use-cases that involves a mix of tabs and spaces. Tabs fist for getting the number of nested scopes, with optionally spaces to align text.
Meanwhile, mixing tabs and spaces is almost universally seen as bad. This makes sense because it is not visible where the tabs end and spaces start. This can easily lead to inconsistencies that are invisible to a writer, but are horrible for a reader with a different tab representation.
If you believe that indenting like this is a bad coding style, how do you suggest those statements should be formatted?
- NeutronStar 7y agoIt's really not as hard as you make it sound, use tab for indentation, and spaces for alignment int main(void){ [tab]foo(bar, [tab][spaces]baz, [tab][spaces]54 [tab]); }
- Thiez 7y agoPut `bar` on its own line, then use tabs to indent all the arguments. foo( bar, baz, 54 ); Optionally the closing `);` could follow the last argument.
- colanderman 7y agoThe solution is to mix tabs and spaces; most editors can be configured to clearly show tabs. Indent with tabs, then align with spaces. Never place a tab after a space. If you mess up, no big problem, someone else will see it and fix it. People screw up space-alignment all the time anyway. Or, better still, don't visually align stuff. It's annoying as hell to maintain unless you use an IDE, and has a tendency to bunch stuff up toward the right margin when function names grow long. Just start the argument list indented by one tab stop on the next line. More visually consistent too.
- timw4mail 7y agoIt's way better than alignment with spaces. Simple rules for indentation of multi-line statements have less diff noise and subjectivity than aligning with spaces. The only exception is Lisp...because it's lisp.
- dTal 7y agoYou're quite right, and I address that here: https://news.ycombinator.com/item?id=20381842 https://news.ycombinator.com/item?id=20381842 tl;dr don't do that because it's ugly to conflate two purposes of indentation in this way, but if you absolutely must conflate them then do so explicitly with tabs and spaces, as you describe, and use an editor which makes the difference visually apparent. This actually boils down to an argument in favor of tabs, because the additional semantic information allows the editor to present you more visual cues to work with.