5 ms·
In the end, what truly matters is whether the codebase is consistent—either using tabs or spaces throughout I use tabs for code indentation, but spaces for non
by thomassmith65 2y ago
In the end, what truly matters is whether the codebase is consistent—either using tabs or spaces throughout
I use tabs for code indentation, but spaces for non-code indentation (eg: for ascii diagrams within comments).
Anyone who has converted a lot of code, from different projects, from spaces to tabs will have noticed: the vast majority of code with spaces contains a few screwups where a line or two in a 4-spaced file actually contains 3 spaces.
Why that happens, despite editors automatically converting tabs to spaces, is beyond me, but it is a ubiquitous phenomenon. I suspect this is the real reason some people, certainly myself, prefer tabs.
- GuB-42 2y agoI like the "tabs for indentation, spaces for alignment" style, but it is not always easy for formatting tools as there is no simple way to convert spaces to tabs, the tool has to be aware of the syntax somehow, or you need to do some manual tweaks. Screwups like missing or adding a space can happen easily even with auto-indent, a common cause is splitting or merging a line, i.e. changing a space with a newline and vice versa. That space character has a tendency to end up where you don't want it, or conversely, get eaten up. When using tabs, invisible space characters can end up between tabs. In the end, on collaborative projects, I usually settle on 4 space indentation, as it is the most common and from my experience, the least likely for people to screw up.
- edflsafoiewq 2y agoCode with tabs inevitably ends up with X spaces where a tab should be, which goes unnoticed until viewed with a different tab width.
- fire_lake 2y agoCould easily be fixed with CI
- poincaredisk 2y agoJust like incorrect indentation using spaces. I personally use autoformatter in all CI pipelines, and error out for every change. This entirely kills the whole issue of wrong indentation/dangling spaces/accidental tabs/inconsistent formatting, etc.
- thomassmith65 2y agoThat's a valid point, though I think it's evitable (if that's a word) because many editors have visible white space, and it's easier for the eye to catch 4 dots after a long tab than to notice that an indentation is 7 dots instead of 8, etc. It also attracts more attention when using arrow keys to navigate the offending lines.
- samatman 2y agoBingo. And the only solution to this problem, consistent use of a formatter, solves the equivalent problem with spaced indentation just as well. I would prefer to have the spacing version of this problem, personally, because that way I can always see that there's a problem, and can do so without resorting to changing tab widths or making invisible characters visible.
- thomassmith65 2y agoYou also make a good point. I usually code in Sublime, where I have invisible characters hidden in unselected text. At this point, I am used to doing 'select all' to check whitespace, but I can understand how that could annoy.
- crazygringo 2y ago> a few screwups where a line or two in a 4-spaced file actually contains 3 spaces. Why this happens... is beyond me In my experience it's usually from copy-paste, usually because the cursor wasn't at the right position when pasting. The cursor not being at the right position because you deleted some spaces to reduce the level of indentation before pasting, but didn't do the right number. While tab inserts the right number of spaces, delete still deletes spaces. Also occasionally due to a find-replace that accidentally included a leading space, which can be hard to see when the find/replace boxes are in a proportional font.
- norir 2y agoI solve this problem at the language level. Instead of using an external formatter, indentation is enforced by the compiler and requires tabs. Significant indentation is used for multiline functions rather than block delimiters. On a given line, spaces may be used for alignment purposes but only after the first non whitespace token. This is no harder to parse than arbitrary whitespace between tokens and guarantees a uniform format for any valid program in the language. I know not everyone will agree with me, but I think defining whitespace in a language as essentially [ \t\n] between any token is a language design mistake.
- everybodyknows 2y ago> ascii diagrams within comments As an aside, what tools do you use to produce the diagrams?
- thomassmith65 2y agoI don't use any tools for that. I did try an online ascii-drawing tool one afternoon, but the results weren't compact enough to be useful for programming comments. Contrary to the way I worded my comment, my 'diagrams' are typically no more than text with perhaps a box-drawing unicode character here or there. But even drawing a simple tree, tabs can mess up important details.
- ericyd 2y agoOn my team this happens due to individual laziness and I don't think tabs would solve that problem.