4 ms·
So clearly, there are people who favor 2 and people who prefer 4. Solution: Indent with tabs, configure your editor, and everyone is happy. I don't get why peop
by Rockslide 11y ago
So clearly, there are people who favor 2 and people who prefer 4. Solution: Indent with tabs, configure your editor, and everyone is happy. I don't get why people still promote the use of spaces.
- x1798DE 11y agoI generally use spaces (in Python) to make up the difference when aligning. If you switched the tab size a bunch of visually indented code would become a complete mess.
- Etzos 11y agoThe rule when using tabs is: indent with tabs, align with spaces. Tab everything out to the proper indentation level, and then if you want to align something then you use spaces on top of that indentation.
- CJefferson 11y agoThe problem is that when some line inevitable has space-based indent (which is basically impossible to see), then indentation goes crazy. Of course, the same thing can happen with spaces, but it generally easier to fix. You can have a "no tab anywhere" policy, and easily enforce it. You can't have a "no space anywhere", or even a "no spaces at the start of lines", for example: while(1) { if (a == b && b == c && d == e && f == g && h == i) { .... We need to tab-indent the 'if', but do tab+4 spaces the 'f == g'. That's very hard to automatically check and enforce
- Etzos 11y agoI personally turn invisible characters on so mistakes like that are a bit harder to make. Along that line of thinking, the code should probably be reviewed by at least one other person before being officially committed, so one would hope that would be the place to find and fix such errors. Personally I've always felt the benefit of letting everyone set their own indentation level far outweighs the off chance of someone misindenting code (and I've never seen it cause a problem so far, not that it means much).
- d0lph 11y agoI do this all the time, I've never had issues, I tab as far as I can and space the rest. As I said I have no issues with this, but if you really wanted, a quick regex for this would be something like: Not allowed: " +\t" Not allowed: "\t +\t" (this is redundant, but you get the idea)
- Stratoscope 11y agoThat's only a problem if you use column alignment, as in your example. But column alignment isn't necessary in coding. It's quite possible - and easy - to write perfectly readable and maintainable code using indentation only and no column alignment. Defining my terms a bit: By "indentation" I mean whitespace at the beginning of a line without any dependency on character widths. Extra whitespace in the middle of a line is "column alignment", and so is whitespace at the beginning of a line when the number of spaces depends not only on an indentation level, but also on matching other text in the previous line. In your example, the "if" line uses indentation only, but the line beginning with "f == g" uses a mix of indentation (to match the indentation of the "if" line) and column alignment (the extra spaces to line the "f" up with the "a" above). One good way to think about it is to imagine (or actually test) what the code would look like in a proportional font. If it still looks properly formatted, you're probably just using indentation. If the formatting gets messed up (e.g. the "a" and "f" no longer line up), you're using column alignment. Avoiding column alignment has many benefits. If you ban it from your code, so many formatting issues go away completely. I've written about this on HN a few times, so I'll leave the details to one of my previous posts: https://news.ycombinator.com/item?id=10206860 https://news.ycombinator.com/item?id=10206860