Hacker News new | past | comments | ask | show | jobs | submit login

= Why Grown-Ups Don't Use Tabs =

* Joe likes 4-space tabs, I like 2-space tabs, and Jane is old-school with 8-space tabs.

* All goes well until someone aligns something visually, like so:

    void someNiceMethod(
[tab][tab][tab][tab][space][space]int myParam...);

* Now it aligns perfectly on my machine, looks mostly ok on Joe's machine, and is ON MARS on Jane's machine.

Thus one-or-more of three futures happens:

* Someone implements a code re-formatter into version control

* Someone re-aligns the code, starting the process over again.

* Someone calls a meeting and demands we all switch to spaces




You mean something like this don't you?

  [tab]void some nice method(int param a,
  [tab]                      int param b
So long as you only use tabs for indentation, not alignment, tabs look correct on _everyone's_ machine. The real problem with tabs is that (some) people are lazy and do stuff like this:

  [tab]void some nice method(int param a,
  [tab][tab][tab][space][space]int param b
Personally, I think if you work like that you need to learn to be professional and write code that follows whatever coding standard your organization uses. If they use tabs, the second example is _not_ following the coding standard.

So whether to prefer tabs or spaces, to me, depends entirely on how much you can/want to trust your coworkers to follow the coding standards.

Edit:

Or as pointed out below, change your coding style to this:

  [tab]void some nice method(
  [tab][tab]int param a,
  [tab][tab]int param b


No I think grand parent comment means that some developers sometimes add spaces instead of tabs, even if it is by mistake. That will mix everything up.

"Could" happen the other way too, but I think: If you use spaces you typically dont repeatedly hit space to produce a single indent. You use tab which gets translated to spaces. Once the editor is set up for this, you don't accidently insert tabs for indentation.


Wait ... you're saying that you should do a tab, and then a whole bunch of spaces?

What actual benefit does tabbing even get you at that point?


Respecting indent preferences of others while not breaking the formatting of that line-split function call.


This whole post would be so much more productive if "Tabs for indentation, spaces for alignment." was defined upfront for everyone to debate.


Formatting


The Linux codebase uses tabs, and presumably they have this rule about using spaces for alignment. I've seen tons of committed code that doesn't follow this rule, though, and hence Linux source looks like shit on anything besides 8 tab-width.


While this is true they will look correct on everyone's machine, they'll be annoyingly wide in tools that display tabs as 8 spaces (such as less, most online diff displays, etc). But spaces will look the same regardless of tool, editor, anything.


I don't know what online diff do you use, but less let you configure the tab stop with the -x option, which is the point of using tabs: being able to chose what's more readable for you.


The point of using spaces is to not worry about any of this. You need a specific fix for less, another specific fix for other diff tools, etc etc. With spaces, everything just works.


I don't see it as a "fix" but as a configuration. And tabs also just works, if you accept the defaults that are given to you :) but if you don't, then you can change your settings (which is trivial). With spaces you have no option but accept whatever is given to you.


Something tells me we'll just go back and forth on this. Should we discuss something else? Perhaps vim versus emacs? Windows versus Linux? :)


The only reason 2, 4 or 8 is less "readable" to anyone is because they have a mental breakdown when they see a style they don't prefer and can't get past it. Tab is a variable width character, it has no place in text laid out with fixed-width fonts.


Is having a style preference bad? One can freely configure color scheme and font, so why should indentation be restricted?


Those options are unambiguous and never require changes to a plain text file to work. I'm pretty sure it was Ayn Rand who said "A is A, but God only knows what Tab is." It's not always used for indentation, it might be for alignment, even if it's at the beginning of a line. There's no way to make any sane programmatic decisions base on it's presence. It does not belong in the digital world!


Some typefaces have spaces as narrow as 1/5em. Many are around 1/4em.

Spaces in a fixed width typeface will be up to about 5 times as wide as in some proportional width ones.


This is like an "eternal September" - when tabs vs spaces comes up each year, most commenters have never seen one of the rather simple ways of using tabs correctly.


There's actually a solid argument for "tabs for indentation, spaces for alignment", which solves both for preference and vertically aligning stuff:

  [tab]void someNiceMethod(
  [tab][space*20]int arg1
  [tab])
But it basically requires having some visual indicator of what is a tab, which most people don't like.

(edited for line breaks)


I have visual whitespace turned on in all of my text editors. The massive amount of mixed tabs/spaces and trailing whitespace I find in virtually every document with multiple authors leads me to believe that I'm the only person on the planet who actually cares about whitespace at all.


Trailing whitespace drives me batty! The only reason I can come up with for it is sloppiness.


If a system requires thought and care to be used properly then people will inevitably use it improperly. Even skilled, thoughtful people make mistakes or get fatigued and take shortcuts.

It seems to me that using tabs for indentation and spaces for alignment is a fragile technique. It's easy to make a mistake and not even notice. If you don't fix that mistake fast, people start to assume that maybe it's OK to ignore whitespace formatting issues. And so the problem compounds.

I would prefer to use a system that creates a "pit of success" so that even careless people have a hard time doing it wrong. I think sticking to just one whitespace character is better than mixed tabs and spaces, but best of all is using an editor that can automatically format your code.


What I didn't say in my original comment is that I don't actually do this for exactly the reason you describe, and that I think spaces-only is the most robust solution.

That said, all you would need is editors to enforce "any line with spaces before a non-whitespace character must have exactly the same number of tabs as the previous line." Which would not be all that difficult, but currently (afaik) does not exist.


I like how it is in Sublime (2) - it shows whitespace characters on highlight.


That's pretty nice. VSCode has a pretty good option too that shows whitespace except single spaces between non-whitespace characters.


Who aligns like that? It seems to defeat the purpose of putting it on a separate line.

  void someNiceMethod(
  <tab>int myParam...
  );
Hey Presto! You, Joe and Jane all get your preferred size size of tab.

As a bonus, this indentation style works equally well if Jim uses a non-fixedwidth typeface.


Well, as long as everyone always use tabs, that should work out okay. But in practice, of course, it doesn't ever happen that way. Nice thing about a space is that it is always a space, a tab is however many spaces you personally want it to be.


Non-fixed width typeface? That would be grounds for immediate termination.


You'd lose a pretty good employee then:

http://www.stroustrup.com/3rd_notes.pdf

> At first glance, this presentation style will seem ‘‘unnatural’’ to programmers accustomed to seeing code in constant-width fonts. However, proportional-width fonts are generally regarded as better than constant-width fonts for presentation of text. Using a proportional-width font also allows me to present code with fewer illogical line breaks. Furthermore, my experiments show that most people find the new style more readable after a short while

Great book, and I have used proportional fonts occasionally. It's much much better than you might imagine (as long as you are sane and use tabs for indentation). Obviously you can't get "perfect" alignment like you can with fixed-width fonts, but it is certainly nicer to read, so there is a trade-off.


People's opinions on that matter are always very strong and rarely... align (ba dum bump).

All-spaces lets the code look as-intended for everyone, and then their IDEs can let them hit the tab-key and get the look-and-feel they're after. It is the best option that gives the most freedom to everyone whilst also making us all play well together.


Do people actually code with non-monospaced fonts? I've never seen anyone doing that before


When I first started programming I had to share a single terminal with the rest of the school, so I wrote my programs on paper first. I used graph paper so that my handwriting would be monospaced.


> * Joe likes 4-space tabs, I like 2-space tabs, and Jane is old-school with 8-space tabs.

> * Someone calls a meeting and demands we all switch to spaces

They compromise on the average 5 spaces per tab and everybody is dissatisfied.


see, i just think that's why grownups don't do that shit. you've got something that would normally be on one line but it's too long? indent once extra and don't be weird. there's no value in lining it up with the parenthesis.


>All goes well until someone aligns something visually, like so:

I have been programming for most of my life, and I still don't know why people do this. I just don't feel the need to align stuff.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: