5 ms·
In fact, tabs are better for compatibility: other people might like other indentation sizes. If you keep your stuff consistently with tabs, and use spaces for a
by tomsmeding 9y ago
In fact, tabs are better for compatibility: other people might like other indentation sizes. If you keep your stuff consistently with tabs, and use spaces for alignment instead of having your IDE auto-replace tabs there as well, your code will look great with whatever tab size someone chooses. And extra pro: in dumb editors, you automatically get the right thing.
- joncampbelldev 9y agounless you have anything aligned in columns then all bets are off (something which many code formatters can do automatically for you e.g. lists of bindings)
- scrollaway 9y agoTabs for indentation; spaces for alignment; I don't know why you'd want to insert tabs in the middle of a line. Also, most people who do have several years of experience under their belt will tell you: don't align. Mid-line alignment is a waste of a maintainer's time to have to update alignment blocks every time you add/remove variables... and statement-alignment is unreadable[1]. [1] https://github.com/pennersr/django-allauth/blob/a44134688f5be0a26f5e93b607f8c4e3f5b0782b/allauth/account/views.py#L685-L688 https://github.com/pennersr/django-allauth/blob/a44134688f5b...
- hn_throwaway_99 9y ago> Tabs for indentation; spaces for alignment On a large code base with multiple committers, I've never seen this rule followed consistently. In fact, I think this shows a poor engineering decision: "Let's make a rule where everything looks OK 95% of the time, except when you change tab widths (which is, presumably, why we made the rule in the first place.)"
- tomsmeding 9y agoWell everything looks OK 100% of the time if everyone followed the rule. Sadly, human history tells us that's an invalid assumption to make.
- bpicolo 9y agoForcing people to remember relatively minor things is a poor use of headspace. Enforce a code formatter/linter, make the test suite success dependent on it, and never argue again.
- benibela 9y agoI often run into trouble with IDEs that think they are smart, but aren't. "I see you have a space following a tab, so I will replace all your spaces with a tab, whenever you save the file"
- fao_ 9y agoI don't see how that code is unreadable, but for me it isn't representative of how I would align that block anyway.
- simias 9y agoI do find properly aligned code easier to read in general, I'm not willing to give that up. The crux of the issue is poor tooling. Aligning correctly is a pain because most editors suck terribly at it. For instance neither emacs nor vim align correctly when using tabs by default (they use as many tabs as they can and then pad using spaces, instead of indenting only with tabs and aligning only with spaces). If they did it would be painless to indent and re-indent. When some of the most venerable code editors out there can't even seem to get it right I think there's no point in fighting it. Learn to stop worrying and love the space, I know I did. Or alternatively use a language like go which provides a standard tool to indent everything, make it mandatory pre-commit and never look back.
- scrollaway 9y agoYeah; the correct answer to a good codestyle is good tooling; high quality automated tooling. Things like gofmt and prettier are excellent. yapf and clang-format are also pretty good, but need a lot of set up. But I never found a single editor that handles spaces correctly. I currently use vscode with vim bindings and whenever I deal with a space-indented codebase I want to tear my hair out: I inconsistently have to hit backspace sometimes once, sometimes multiple times to get rid of an indent level in insert mode. And don't get me started with yaml, a really painful experience to edit it.
- tomsmeding 9y agoCan't vscode automatically remove an entire indentation level with a single backspace? Many editors can do that, maybe with a non-default setting, but regardless.
- coldpie 9y ago> Tabs for indentation; spaces for alignment; If you try this, it fails for people who use a different tab size than you, which is the whole supposed point of using tabs. But yes, the correct answer is don't align. Utter waste of time.
- treehau5 9y agoThis entire thread is why I love gofmt so much. I haven't thought about this useless bullshit for years.
- coldpie 9y agoMyself, working on an old open source project, I just don't care. Match the nearby style best you can, use your own style on new code, who cares, get on with your work.
- Gracana 9y ago> If you try this, it fails If it fails, then you have not understood and you are not using your tools correctly. If your alignment depends on tab width, then you are not using tabs for indentation and spaces for alignment, you are using tabs for indentation and alignment and spaces for alignment, which of course breaks when you change your tab width.
- deleted 9y ago[deleted]
- coldpie 9y agoAh, I see what you mean. I've never seen an editor work this way, normally they just swap spaces with tabs, leaving however many spaces can't be swapped. Which is probably why I've never seen tabbed code in the real world that doesn't look like utter garbage.
- pseudalopex 9y agoThis was definitely a problem back in the day, but I think most popular editors do the right thing now. And like you say, alignment is usually a waste of time anyways.
- tomsmeding 9y agoThe alignment in that link is laughable. Please use newlines and an extra indentation level. I believe older versions of Xcode did that by default with objc (maybe still; didn't check lately) if you press enter in the middle of a function argument list. Really annoying.
- peterbonney 9y agoTabs are only better for compatibility if you know that no one who touches the code will ever use spaces.
- IshKebab 9y agoBut the same is true for spaces...
- creepydata 9y agoWhat kind of a dick opens up a tabbed project and starts inserting spaces? Even if you have a preference the other way you play nice with others and stick with the project conventions. The other thing you need to do is stop people from checking in code that doesn't follow project conventions.
- ioquatix 9y agoOh, I've got some good examples :D https://github.com/ioquatix/rack-freeze/issues/2 https://github.com/ioquatix/rack-freeze/issues/2 https://github.com/ioquatix/latinum/pull/1 https://github.com/ioquatix/latinum/pull/1
- kej 9y agoFrom the linked issue: >deliberately have my editor set up to change tab width based on editor width (responsive indentation width) What editor are you using that has this setting?
- ioquatix 9y agoThere are a couple of ways I have this set up. Firstly, I've explored using responsive tab width in https://github.com/ioquatix/jquery-syntax https://github.com/ioquatix/jquery-syntax which is nice when presenting code on a mobile phone. Secondly, I've got hard coded tab width on my desktop with a huge screen vs my laptop with a smaller screen, and this works for all editors, including my primary editors Atom and vim. Finally, I've played around with using responsive CSS within Atom, but due to recent changes in the text rendering my current CSS doesn't appear to be working.
- hn_throwaway_99 9y agoI have never seen this work correctly in practice, where a code base with tabs looks "OK" when opened on an editor with a different tab width. E.g. some coding styles will have subsequent lines line up (like multiline method params), and everything looks horrible with a different tab width. If anyone can post a sizable production code base that uses tabs that DOESN'T look like shit with a different tab width, I'm certainly open to being proven wrong.
- tomsmeding 9y ago> sizeable production code base Here and there's the problem. Too many people with editors that go round replacing N spaces with tabs everywhere to have sane alignment in a code base with >1 developer...
- vvhn 9y agoFreeBSD, Linux ... every UNIX kernel codebase ?
- _IsThisRealLife 9y agoThis feels like a partial proxy question for Unix versus non-Unix development. Unix developers are, in my experience, consistently more interested in configuring their IDEs, which underlies many of the arguments for tabs over spaces.
- hn_throwaway_99 9y agoLinux looks like shit with any tab width besides 8. Simple example: https://github.com/torvalds/linux/blob/master/kernel/livepatch/transition.c https://github.com/torvalds/linux/blob/master/kernel/livepat... Notice there are a couple examples in that code where multiline method calls are meant to line up, and if tabs are not set to 8 they don't line up.
- GuB-42 9y agoThat's the problem with a space-tab mix. I do it myself but it requires a bit of maintenance and I wouldn't trust teammates with it. There will always be some guys who don't understand the idea and mess everything up. In a team environment, now, I wouldn't chose anything but 4 spaces. Speaking from experience. - with anything but 2, 4 or 8 spaces, some guys with incorrectly set up editors will mess everything up. - tabs don't work well for alignment, and as long as you allow the tab character, someone will make a mess. - 2 spaces is not enough to properly distinguish between blocks. - 8 spaces can look very nice if your code is very clean. It won't be the case of your project.