Tabs vs. spaces redux

2008-11-05 22:52:20 -08:00

Alice, who prefers four-space indents, wants this:

Four-space indents, with non-first lines of an Objective-C messages lined up on the colon of each argument.

Bob, who prefers eight-space indents, wants this:

Eight-space indents, with non-first lines of an Objective-C messages lined up on the colon of each argument.

Is it possible for this source code file to be written such that Alice and Bob each see what they want?

Yes, but not yet.

What they need to do is to use tabs only up to the level of indent of the first line, then spaces the rest of the way:

Screenshot demonstrating this with four-space indents.
Screenshot demonstrating this with eight-space indents.

Note that the contents of the file are the same in both of these images; the only change is the indent width. The characters shown in these two images produce the result shown in the earlier two images.

I say “not yet” because no editor currently does this. Xcode, for example, will use on each line as many tabs as it can fit, followed by < tabstop spaces. Other editors won’t line up the colons; they’ll just left-justify all of the non-first lines.

So Alice and Bob can each have the indent width they want; it’s just a pain in the ass for them to do the editing necessary to get it. Therefore, they won’t bother, just as I don’t.

So, consider this my call to the makers of all the editors to auto-indent the right way, as I have just shown it.

Added a few minutes later: Alternatively stated by Christopher Bowns on his blog post.

9 Responses to “Tabs vs. spaces redux”

  1. David Wilson Says:

    What bothers me is that we care about such things in the first place. It is possible for a suitab ly capable editor to render the syntax tree in whatever way pleases the individual user. I think a large part of the tabs/spaces/formatting/K&R/ANSI/etc. debate would quickly go away if we had such smart editors.

  2. Peter Hosey Says:

    David Wilson: I wholeheartedly agree. Basically, the editor should be able to learn the “local” style, display whatever style the user wants to see, and then write the file’s original style out when saving (regardless of the display style).

    So, for one example, if the file uses four-space indents and the user wants eight-space indents, it shows eight-space indents to the user, and converts these to four-space indents when saving.

    The hard part would be handling inconsistent styles intelligently.

  3. Mattias Says:

    I was actually about to write something like this myself a few days ago, but haven’t had the time to do it yet. I guess I’ll just write a short post and link to you and Christopher. And why not file a feature request over at Apple while you’re at it?

  4. Christopher Bowns Says:

    The wackiest thing about “Xcode, for example, will use on each line as many tabs as it can fit, followed by < tabstop spaces. Other editors won’t line up the colons; they’ll just left-justify all of the non-first lines”: TextMate does it the other way around when you hit control-q, as I showed in my post. It’s spaces all the way, presumably because it uses a Ruby snippet to do the text manipulation.

    In an even more ideal world, when Alice or Bob edits the parameter name of, say, “errorDescription:”, because they realized it’s supposed to be “errorText:” instead, the editor auto-eats or auto-inserts spaces before it to keep the semi-colons aligned. The result would resemble a right-justified text, with the colons being the “margin”, but the implementation is far trickier.

  5. gparker Says:

    This scheme is still insufficient. It only works if the elements that require alignment are on lines that share the same indentation scope.

    |~~~if (condition) {~~~// multi-line
    |~~~|~~~code;~~~~~~~~~~// comment
    |~~~|~~~more code;~~~~~// describing the block
    |~~~}// multi-line macro declaration
    #define foo~~~~~~\
    |~~~do {~~~~~~~~~\
    |~~~|~~~code;~~~~\
    |~~~} while (0)
    

    If you change the width of the leading indentation, the alignment of the trailing elements will be mangled.

  6. gparker Says:

    (The preview showed a fixed-width font. Bad weblog software, no biscuit.)

  7. Peter Hosey Says:

    gparker: I went and put a pre element around that for you.

    I don’t think there’s a perfect solution to that problem (also discussed by Christopher Bowns on his post) without a much smarter editor.

  8. Steven Fisher Says:

    What you’re really saying here is very simple:
    Indents should use tabs.
    Alignment should use spaces.
    They’re not the same thing.
    The editor should help you do this.

    I agree on all four points. However, I’ll add that I tried to do this for years in Visual Studio. I haven’t even bothered with Xcode, thanks to all the extra alignment in Objective-C code. And I’d be happy with an editor that just stopped fighting me on it.

  9. Marius Andersen Says:

    Emacs does:

    http://www.emacswiki.org/emacs/IntelligentTabs

Leave a Reply

Do not delete the second sentence.