Friday, June 13, 2008

Why is Tabs vs Spaces a "Holy War"?

Ok so this has always bugged me: Why is Tabs vs Spaces a "Holy War"? I don't understand it.

My personality is such that I can normally see both sides of an argument. So, when I look at this issue and I don't see the other side I wonder if maybe there is just a miscommunication rather than major differences.


So lets set out some base assumptions:

1. Regardless of whether you use tabs or spaces to indent, use spaces to align so that alignment doesn't change with tab width. If you believe that you should use tabs to align, then you are a fool or lazy.

2. If you are not using a fixed-width font, then you are a complete moron or a newbie. (some people argue tabs for alignment because it works better for variable width font)


So, the vast majority of us can agree to use spaces to align and for godsake use a fixed width font.

Ok now that that is taken care of, on to the important part: Indentation. Everyone has different preferences as to what is more readable as far as indent width. If spaces are used the only way to adjust the indentation to a comfortable width for you personally is to modify the document. This is not acceptable for anyone sharing code: white space diffs, change battles, etc.

If you use tabs, you can set your editor to any indent size...

Where is the argument?

EDIT (2009.9.15):
So I have finally uncovered a somewhat valid argument against tabs:


Comments that are aligned across multiple indentation levels.

When using tabs they can become mis-aligned if spaces are used to align them, and then the document is opened with a different tab width.


So does the whole argument boil down to that? This never comes up in tabs/spaces holy-war debates (that I've seen). But now it seems to me that the crux of the whole thing is the valuation between the {benefit of parameterized indentation level} vs the {cost of lost formatting in cross indentation level comments.}

Now if only we could do this for Palestine and Israel...


EDIT (2009.10.22):
I found another (minor)advantage of tabs recently now that I've been using spaces (for compliance) at work: Keeping commented-out code aligned.


    int x;
//  int y;   // after commenting out with tabs
//    int z; // after commenting out with spaces

3 comments:

Catskul said...
This comment has been removed by the author.
blag said...

Comments aligned across multiple indentation levels don't happen very often, and if they do, I'd bet it'd be "bad usage".

I hate when editors are too dumb to erase all the spaces that form a single indentation (this happens too often). And if you're 'on the road' and don't have your favourite editor available, then you won't like tolerating this for very long. Luckily most editors are sane: tab is tab. (Also sucky when they don't have smart-indent and you have to reindent every new line - easier with tabs.)

Also nice about using tabs is that you can temporarily comment out code without messing up its indentation (but putting the // before the last tab, requires tab-width >=3).

Not that this matters too much, but tabs also save hard-drive space.

Once you're aware of the 'war', you can write code that can happily be converted to spaces unambigiously, but I don't think the same is true of space-indented text.

Also, being able to change the indentation level can help with readability: you can increase the indentation for segments of code that are highly indented and then have more hope of being able to figure out which closing curly brace is for the for loop ten pages up (without doing the ridiculous 'point and scroll back down' trick).

Lastly, the whole premise behind using spaces - to control appearance - is flawed. Yeah, yeah, 'readability', ... but solving the problem by using spaces is so primitive (and in ways counter-productive) it's asking to get flamed.

There. The war is over. :)

Catskul said...

@blag: Honestly I think a lot of the argument against tabs comes from two places:
* Emacs users
* People who learned on spaces

The Emacs users have some sort of super-special-emacs feature that breaks with tabs for some reason. And blame the tabs instead of Emacs.

And the people who started with spaces just think tabs are bad because they've come across files indented half with tabs half with spaces. But that doesn't argue for either, just that one become "standard".

Post a Comment

No flamebait allowed.