by inanutshellus · 9y ago · 0 comments
* 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
[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"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.
What actual benefit does tabbing even get you at that point?
Spaces in a fixed width typeface will be up to about 5 times as wide as in some proportional width ones.
[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)
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.
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.
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.
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.
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.
> * Someone calls a meeting and demands we all switch to spaces
They compromise on the average 5 spaces per tab and everybody is dissatisfied.
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.