17 ms·
I'm going to use this as fodder next time I get into an argument with someone about why spaces are better than tabs when writing code. Or maybe I've just been
by peterkelly 10y ago
I'm going to use this as fodder next time I get into an argument with someone about why spaces are better than tabs when writing code.
Or maybe I've just been watching too much silicon valley...
- jasonm23 10y agoTabs are the devils puss is a more convincing argument for the religiously motivated Tabs user. They don't respond to reason. Obviously. /s
- drewrv 10y agoI thought that was the most unrealistic thing this season: a supposedly genius programmer who loves tabs and hates spaces.
- scrollaway 10y agoPoe's law and all, I hope your comment isn't serious. The whole point was to highlight Richard's "neurotic" side. (It failed to convey it for me when his girlfriend started audibly indenting by hitting the spacebar 8 times... I'd go nuts, who wouldn't?) Also, plenty of geniuses use tabs :) the Linux kernel included. It obviously has no relation. (Seriously though, tabs are a matter of accessibility. https://leclan.ch/tabs https://leclan.ch/tabs)
- yoavm 10y agoThis is interesting. Why do we use spaces anyway? I mean, really - what are the advantages spaces have over tabs? (I use spaces only because I try to follow common coding styles, like PEP 8, but I never gave it enough thought)
- th0br0 10y agoTab width can vary between editors, i.e. do a "set shiftwidth=8" in vim, and it'll be 8 spaces. Spaces are more constant.r
- kasparsklavins 10y agoIsn't that the whole point of tabs? So everyone can have their own width preference without going mad with git merges.
- clappski 10y agoI thought that was a reason _for_ tabs, so everybody can have their own indent size.
- yoavm 10y agoI have to admit I always thought of that as an advantage of tabs, since they allow the viewer to set how much to indent the code, and doesn't force anyone to adapt to someone else's preferred style. I like it when a tab = 4 spaces, but you like it when a tab = 2 spaces. Cool, just set your editor accordingly.
- slavik81 10y agoUnfortunately, it also means that if the viewer hasn't or can't set the tab width, it may look awful. GitHub was using 8 spaces per tab by default, and my code looked ridiculous. Now GitHub checks .editorconfig for a default tab width, but it didn't used to.
- Stratoscope 10y agoIt's certainly some progress that GitHub now follows .editorconfig! But now that they have the ability to render tabs with different numbers of spaces, I wish they could have changed the default to four spaces while they were at it. Aside from the Linux kernel, not too many people really like eight-column tabs. I've always omitted the indent_size and tab_width settings from my .editorconfig files that specified tabs, because I wanted to let people view the code in their preferred default indentation size instead of one I specified. (As I've mentioned too many times in this thread, I follow an indentation-only style with no column alignment, so you can view the code at any tab width without breaking the formatting.) Now, just to make GitHub give a reasonable code listing, I'm having to add indent_size=4 to all my .editorconfig files. That's not a terrible thing, but I really would prefer to leave the choice up to the viewer instead of forcing 4-character tabs.
- bhaak 10y agoThe reason is probably similar to why there's now top posting in emails. The support in editors is abysmal and doesn't work out of the box correctly for tabs (e.g. invisible whitespace). I've never seen an argument for spaces that would hold iff all editors would have sane defaults for programmers. Of course, it's also bad that some programmers can't configure their tools correctly, but that is a complete different issue. The best of both worlds would be "tabs for indentation, spaces for alignment" but as it is now, you only need one programmer with wrong settings to screw that up, so that's why most projects have settled on the most conservative setting.
- Stratoscope 10y agoThere's an even more conservative setting, which is "don't use alignment at all, use indentation only." If you do that, all of these issues go away. You can use tabs or spaces for indentation, and it won't affect your formatting. You can reformat the code to a different number of spaces, or switch from spaces to tabs or vice versa, and your code will still be perfectly formatted. A good way to learn and practice this style is to switch your editor to a proportional font. Now you won't be able to use column alignment, and without even working at it you'll naturally start to explore an alignment-free style. I've had many programmers react in shock when they see I write code in a proportional font, but when you don't use alignment it stops mattering what font you use.
- scrollaway 10y agoOne of the main drivers for spaces is alignment, which you should never attempt to do with tabs (except in contexts which normalize tabstops, which generally excludes programming). Aligning within a line can and should be done with spaces. There's good reasons against alignment in such cases -- for example, it creates git blame/git diff noise when reformatting due to having to update alignment on a bunch of other lines just because you introduced a variable one character longer than the others. Irrelevant, though; such alignment is perfectly fine to do with spaces in a context where you indent with tabs. The other type of alignment is beginning-of-line alignment, which is indeed not doable with spaces. Some people recommend doing such alignment with spaces despite the tab indent (cf. http://dmitryfrank.com/articles/indent_with_tabs_align_with_spaces http://dmitryfrank.com/articles/indent_with_tabs_align_with_...). It's a valid argument but I feel it's too much of an aberration to be taken seriously. I personally find such alignment generally repulsive anyway. Take a look at the following code and tell me which method looks better: http://sprunge.us/ZFHJ http://sprunge.us/ZFHJ Too many times have I seen such function calls where the space between the parenthese and the soft wrap leaves <10 characters to work with. And here we go with one small argument per line, every argument preceded by 70+ spaces. I know programmers who could fit video games in the bytes used for just a single function call like that.
- Stratoscope 10y agoIndeed, column alignment is just about the only reason to prefer spaces for indentation. If you use column alignment, then indenting with spaces avoids the question of "tabs for indentation, spaces for alignment." But I'm with you: that kind of column alignment is harmful to maintainability, and I find it much less readable too. Some time ago I posted a few examples of column alignment gone bad, from the Servo source code: https://news.ycombinator.com/item?id=9469713 https://news.ycombinator.com/item?id=9469713 and also discussed alignment here: https://news.ycombinator.com/item?id=9469713 https://news.ycombinator.com/item?id=9469713 Once you give up column alignment, there's no longer any real reason to prefer spaces, and you can gain the benefits of using tabs. As a side benefit, when you give up alignment, your code becomes just as readable in a proportional font as a monospaced font. More details in my previous comments linked above, so I won't rehash them here. :-)
- Freak_NL 10y agoThe common argument is that with spaces the indent is the same in not only every editor, but also every tool used, such as cvs, diff, patch, build tools, web viewers (e.g., for your cvs tool, issue tracker, and/or code review), shell tools (e.g., less and cat), et etcetera. With tabs you need to configure each and every tool you use to use the right indent (2? 4? 8?), with spaces, every tool just works, and copy/pasting a section of code to or from an issue or an e-mail or a chat tool just works. It is a matter of code typography. When more than one developer is working on a code base, it helps if every one sees the same outline, because with varying amounts of indenting one persons preference aligns all parameters in a long important method call, whilst any other indenting breaks (part of) that format — code may look fine for one person, but jumbled for another with a different indent preference. With editor support better than ever, using spaces isn't something you think about when coding; you just hit tab to indent and it inserts the number of spaces configured. I like Python's PEP 8 in that respect. The language itself (and the syntax checking tools such as pyflakes) simply states the sensible default you aught to be using.
- scrollaway 10y ago> With editor support better than ever, using spaces isn't something you think about when coding; you just hit tab to indent and it inserts the number of spaces configured You mean like those times I would have to configure my editor so it inserts the exact amount of spaces required by the project I'm working on, for the project maintainers prefer 4-width to 2-width or 8-width? :) You talk about typography but this is exactly what I underline in my article: Your preference is not everybody else's and I personally find 2-space code absolutely unreadable... while some companies adopt it as their internal codestyle. My eyesight has been deteriorating for the past 10 years and I've had to increase my font and zoom sizes as well as line spacing every now and then. Compact 2-space code truly is a strain on the eye for me. If you're using tabs, you don't have to care about my eyesight, just like you don't care about my font size.
- Stratoscope 10y agoIf you're interested in another view, see scrollaway's code example linked above, along with my other comment in this thread that links to a couple of my previous discussions of the topic. In short, the real issue with tabs is that you can't reliably use them for column alignment. But many of us have concluded that column alignment is a harmful practice, preferring indentation instead to show the structure of our code. And once you give up column alignment, you can use tabs for indentation, and it just doesn't matter what tab size the person reading your code prefers. It will read just the same in any tab size and any font, even proportional fonts. It's really quite liberating to stop using column alignment. :-)
- yAnonymous 10y agoTabs don't work very well when you use whitespace for alignment, for example in multi-line statements. Change the tab size and it's all off... And seeing as 80 characters is still the normal line limit, because of window splitting, it does happen quite often.
- Stratoscope 10y agoThe problem there isn't tabs, it's column alignment. If you stop using alignment and use only indentation for your multiline statements, these issues go away. Your code will be just as readable with any tab size, and even in a proportional font. And your line lengths get much shorter in the process. For specific examples, see the other comments in this thread from scrollaway and myself, in particular the Servo source code I linked to.
- deleted 10y ago[deleted]
- yAnonymous 10y agoSo you're trying to dictate a coding style to make up for tabs' weaknesses? That's like Apple's reaction to bad reception on phones: "You're holding it wrong". No, thanks, I'd rather use spaces that don't require any workarounds.
- Stratoscope 10y ago> So you're trying to dictate a coding style to make up for tabs' weaknesses? Not in the slightest. The disadvantages of column alignment have nothing to do with whether you use tabs or spaces. I don't recall for sure, but I think I was using spaces for indentation at the time that I stopped using column alignment. Column alignment has the same problems if you indent with spaces: it leads to excessive line length, it's unfriendly toward source control diffs, it's fiddly to maintain, it mandates the use of a monospaced font, and I find that it harms readability compared to the alternative of using indentation. Take a look at the Servo code examples I linked to in another comment. Are the column-aligned versions a better way to format code than the indented versions? Here's another way to look at it. If the column-aligned way of writing a multiline function call or expression is such a good thing, why don't we also format blocks of statements this way: if( foo == bar ) { if( baz == moo ) { console.log( "yes" ); this.that = true; } else { console.log( "no" ); this.that = false; } } instead of: if( foo == bar ) { if( baz == moo ) { console.log( "yes" ); this.that = true; } else { console.log( "no" ); this.that = false; } } It is probably obvious that I prefer the latter, as would most developers. So why not apply the same formatting principle to expressions that we apply to statements?
- andrewfong 10y agoHow do tab advocates deal with max column widths? Many projects require that lines not exceed 80 chars, but I'm not sure how'd you establish that kind of limit with variable length tabs.
- minitech 10y agoPersonally: by not enforcing maximum widths. Having a fixed limit makes for ugly workarounds and looks horrid if you happen to be viewing at a narrower width. Just wrap code where it makes sense to do so.
- mikegerwitz 10y agoI think that depends partially on the verbosity of the programming language (e.g. type declarations, casting, templating) which eat up a lot of columns, and verbosity of coding style. In the latter case, if your variable names are `name_of_person' rather than simply `name' (which should be obvious by context), you're also going to have problems. The first one might be a legitimate issue. The second issue, I've noticed, I've never had a problem with, even in heavily indented lisp: - If your variable names are too long, they must not be obvious from context, and your function/method/block is doing too much. Refactor. - Consequently, if the line is too long, perhaps it's doing a little too much; short lines with limited logic help with code comprehension the same way short methods do. Splitting code into multiple lines generously allows you to clearly indicate how parts relate to one-another. My line width is 76 characters max to allow for 3 columns for line numbering + one space after it---if line numbering requires more columns that that, maybe your file's too big as well. Some will disagree on that last point, but I consider that in the same light as I do line length and function size. For OOP especially where there's one class per file, it's certainly a code smell if you have a long file.
- girvo 10y agoEditorConfig or various other configuration setups allow your editor/git/whatever to read a tab as if it is "n" spaces, where that is somewhat configurable. Using EditorConfig, this allows consistency across tooling, and solves this problem neatly (at least in this particular use-case, YMMV in other places where this isn't possible or desirable).
- efaref 10y agoTabs make no sense to me as they're clearly control codes. I maintain that if tabs are permitted, so are other ASCII control codes, like vertical tab, form feed (which is actually used in GNU code), and bell. int foo(void) { <TAB>/* <BEL><BEL><BEL> This bit is really important. */ <TAB>... } <VT> int blah(void) { <TAB>... } <EOT> <EM> :-)
- Asooka 10y agoWhy not go all the way? Use Start of Header before your require/#include/import/etc. then Start of Text when your code proper starts. Replace ifs with ENQ, thens with ACK and elses with NAK. You can build an entire language where all the keywords are just ASCII control codes. That will free up all the punctuation for other purposes, and you could name your variables if, for, while, etc.
- Stratoscope 10y agoCareful, my friend. Strawmen are highly flammable. ;-) But I like your sense of humor, so you definitely earned an upvote from me - a flaming tab advocate! (And I learned to program on a ASR33 Teletype - so I do miss those old control codes...)
- Symbiote 10y agoI like the idea :-) The GNU coding standards request form-feed (\f or ^L) to separate code into logical pages. https://www.gnu.org/prep/standards/standards.html#index-control_002dL https://www.gnu.org/prep/standards/standards.html#index-cont...
- akerro 10y agoThis is interesting. Why do we use spaces anyway? I mean, really - what are the advantages spaces have over tabs? Apparently nested JS runs faster with spaces.
- Shorel 10y agoAFAIR It is because a 30 years old rant in Usenet that defended spaces over tabs, but all their arguments were biased by the fact it was about LISP. It's impossible to use anything but spaces in LISP without going mad, and the editors like emacs do a fantastic job formatting the s-expressions. So spaces are very superior to tabs for some languages. For languages with a syntax similar to C, it doesn't matter, the rant was taken out of context, and the rest is history. Whatever you use, make tabs and spaces visibles in your editor and never, ever, mix them up for indentation.
- onion2k 10y agoI've never quite understood why lots of IDEs don't convert a file to your preferred variation of tabs or spaces when you open a file, and then convert it back again when you save it. On disk the indentation could be one thing while in the editor it's something else. That way everyone's happy.
- mhomde 10y agoI think the whole notion of programming language as pure text and even using "files" as a unit is veering towards obsoletion. We do it that way because that's the way it's always been done, but I can see plenty of cases where an IDE that uses a more hybrid graphical approach would be beneficial, and we wouldn't have arguments like this :)
- tmzt 10y agoI feel like WASM could be a first step in that, a binary AST/graph-like format with a preferred textual representation. It even has a bidirectional codec.
- Klathmon 10y agoWhile I agree in some senses, i'm terrified of this idea in others. A business-basic style language i work with (second time i've talked about it in a few days... it's weird) saves it's files in a strange not-compiled-but-not-plaintext format that only it can read. That means you can't use your VCS of choice, you can't use your favorite editor, you can't even grep through the codebase to find where a variable name is used. Technically you could write something to get it working in a more "normal" editor, but that is a path I don't want to run down. Ignoring encoding, text is about as accessible as it gets. You can always layer more tooling on top of it, but you can't take it away from a non-text representation easily. Personally I like the way that the go team handles it. Source files are "plaintext" (again, ignoring encodings for the sake of discussion), but there is one (and only one) canonical format for the layout of the code, and a tool which will apply it. Then like you said, you can have IDEs which can layer more visual representations on top of that if needed, and there is a chance that some people could program without actually seeing the "plaintext" source code.
- js2 10y agoIf the tab people would reliably use tab for leading indent and space for further alignment, I'd be 100% on board. But what happens in practice is that the file ends up with a mish-mash of tabs and spaces throughout and the file is only legible if you set your tab stop to a specific setting, and sometimes not even then because different authors have used different tab stops for both indentation and alignment. At which point the tabs buy you nothing. So just use spaces and be done with it. Or go fmt. :-)
- __s 10y agoAs a tab person, I don't line up code. Each line may decide to unindent, indent, or maintain indentation Here's some Befunge interpreters in a few different languages: https://github.com/serprex/Befunge https://github.com/serprex/Befunge
- sethish 10y agoIn an argument of tabs vs spaces. Someone always says both. You are that person.
- minitech 10y agoBut the argument really is both vs. spaces. Anyone using tabs for alignment is doing it wrong.
- d0lph 10y agoI think it would not be hard to create a git hook that checked and fixed inconsistent tabbing. As someone who tabs, 4 is a very common standard, and consistency has never been a problem.
- frandroid 10y agoIn our project, we use ESlint to check for this kind of stuff. For a while we had it forcibly fix indentation and minor bugs but some devs didn't like it so now it just warns you. I thought it was amazing.
- 10y ago
- Confiks 10y ago> It failed to convey it for me when his girlfriend started audibly indenting by hitting the spacebar 8 times... I'd go nuts, who wouldn't? I remember when I got into coding Runescape autoclick bots when I was 14, in an arcane Delphi editor / standard library called SCAR, it was the insane convention to not use any indenting at all. Indeed you had to manually hit the spacebar again and again to get some proper indenting. Also, the spacebar key on my keyboard was noisy.
- dzhiurgis 10y agoAutorune was better though :) Attack(0) would attack yourself in wilderness, leaving bystanders confused.
- d0lph 10y agoIf you're interested, that community is still alive and pretty active as SIMBA [1]. Not hard to find very smart people with very poor coding standards. [1] https://villavu.com/forum/ https://villavu.com/forum/
- sevensor 10y ago> hitting the spacebar 8 times I used to do this before I switched from vi to vim. I used 2-space indents to save wear and tear on my thumb.
- cyphar 10y ago> I thought that was the most unrealistic thing this season: a supposedly genius programmer who loves tabs and hates spaces. I've never met a programmer that didn't have some view of code style. Even the really clever ones consider their code to be a form of art, so what you use to write it does matter. It's all pretentious, but I actually thought it was a very accurate portrayal of the trivial things that programmers make a big deal out of.
- donny2015 10y agoJust because of this I knew the whole show was BS.
- exadeci 10y agoYou just don't get it, it's not supposed to depict programmers in the Silicon Valley. It's a comedy, that makes fun of the stuff happening in the Silicon Valley by strongly exaggerating it. On a side note, geniuses are know to have unusual quirks.
- onetwotree 10y agoShots fired!
- 6t6t6t6 10y agoA supposed genius programmer who can have an argument for such a silly question. Besides, why people don't use linters?
- brod 10y agothe only reason to use spaces over tabs is if your tab key broke and you can't map it to a double-tap space (yeah, I did that once). /thread
- Bromskloss 10y ago> map it to a double-tap space How do you do that?
- asQuirreL 10y agoIn vim, adding the following to your .vimrc should do it: inoremap <Space><Space> <Tab>
- nerdponx 10y agoOr just `set expandtab`...
- asQuirreL 10y agoThat would be more like inoremap <Tab> <Space><Space> The request was: when I press two spaces, I want a tab character to be inputted. I agree that if you want `n` spaces when the tab key is pressed, you should use `expandtab` and `shiftwidth`.
- nerdponx 10y agoAh, quick/lazy reading. Sorry. Of course now my question is why in the seven hells would you want that mapping??
- Bromskloss 10y agoThe premise was that the tab key was broken.
- joebergeron 10y agoDoesn't everyone have their IDE turn tab characters into some number of spaces by default? Easiest way to deal with this.
- deleted 10y ago[deleted]