Tabs vs. Spaces

I found this article after googling “tabs vs spaces” — and I did that because I was reading an amusing (and apparently very old) treatise on How to Write Unmaintainable Code which seems mostly to be a litany of defects in C, C++, and Java as programming languages. Using tabs instead of spaces is listed as a way of writing unmaintainable code.

Seriously?

I use tabs for indenting, and have never really seen any reason not to. At my last job, there was a rather stern rule to use spaces, only, for indentation, and most of the code was indented by two space increments (I prefer four). I read Jamie Zawinski’s post in an effort to understand where the “anti-tab” folks were coming from.

I just care that two people editing the same file use the same interpretations, and that it’s possible to look at a file and know what interpretation of the TAB character was used, because otherwise it’s just impossible to read.

–Jamie Zawinski

Actually, I don’t really understand his position at all. Having said this, he then goes into technical minutiae about how to get emacs to automatically replace tabs with spaces when you save or somesuch.

Here’s my view. I like to indent stuff fairly clearly (as I said earlier — four spaces). I like to avoid typing more than necessary, and I particularly hate having to precisely type four spaces. I even more particularly hate having to backspace over four spaces. Now, sometimes when I paste code from somewhere else into my own code it will be indented somewhat randomly (e.g. if the person was using spaces for indentation and preferred two or eight space indents). In this case, depending on how fussy I’m feeling, I’ll convert the spaces to tabs or just get it roughly right (so its base indentation level is correct) and leave it alone.

Now there’s a simple test you can apply to either side of a question. What if everyone did things your way? Well, if everyone used tabs then everyone would see code indented just as they liked, code files would be smaller, and removing or adding a level of indentation would always require just one keystroke.

There are cases where tabs may cause misalignment, e.g. one post on the subject showed if you’re breaking out a function’s parameters because it has a lot of them, and you want to line up the second parameter with the first, then using tabs may ruin your alignment (but so does everything else), therefore use spaces — but this problem can be solved by using a less idiotic indentation scheme (indent all the parameters one level further than the function). Any problems caused by inconsistent tab preferences are just as true of spaces when using variable width fonts, which is quite common these days (oddly enough).

Here’s the (simplified) example:

    foo( int i,
         int j
       );

This (potentially) breaks if indented with tabs (or a mixture of tabs and spaces, but no-one is advocating that) and editors with different preferences are used). (It breaks in lots of other situations too, which is why I’d write it this way:

    foo(
        int i,
        int j
    );

or if you’re one of those people:

    foo
    (
        int i,
        int j
    );

(I prefer the first option — but I don’t think the second option is stupid.)

The problems all seem to result from mixing spaces and tabs, which can be solved just as easily by converting leading spaces into tabs when you save as the reverse, and has all the other advantages listed.

I really don’t care that much — in my previous job I cheerfully adhered to the spaces not tabs rule. But I still thought it was stupid.

I have a feeling that the roots of this war lie in the whole emacs, vim, pico thing. Basically tabs are a pain in the butt for users of emacs and vim so they’re convinced it’s tabs and not their idiotic text editors that are the problem.

Post Script

One possible reason for such vehement preferences that just occurred to me is that many popular version control systems aren’t smart enough to ignore whitespace, so if Jill uses tabs and Joe uses spaces a whole bunch of badness will ensue. (Again, in my previous job there was similar angst over Mac classic vs. Windows / DOS vs. Unix / Mac OS X newline standards.) But this doesn’t mean one side is right and the other is wrong, just that for a given situation it may be a good idea to pick one or the other and stick with it.