Hacker News new | ask | show | jobs
by dilap 3830 days ago
tabs to indent, spaces to align.

this is what go does, and it works great. take any gofmt-formatted style, set your tab size to anything you like, and it will still look great.

it's baffling and slightly tragic that this style of indentation is not more popular!

2 comments

Mere humans are incapable of doing this right as most don't run with show whitespace (so tabs and spaces look different) & many don't grok the difference between indentation and alignment.

I wonder if gofmt logic can be extended to other languages.

Well, in my opinion doing any sort of aligning-things by hand is pretty tedious. I tend to just use indentation and not worry about alignment in languages that don't have a formatter to do it for me.

E.g. I would write

    struct Foo {
        int bob;
        string alice;
    };
and not worry about lining up variables, whereas gofmt would give you

    type Foo struct {
        bob   int
        alice string
    }
which is fine too but not worth doing by hand IMO.

But if you are, it's not too hard to know where to use spaces and where to use tabs, even w/o show whitespace. But it is true that this is more deeply more than most programmers want to think about indentation. :)

There are more languages than Go and some of them work better with spaces.

Spaces work everywhere.

Nah, it works almost everywhere (ignoring things like nim or make that treat them differently); it's not just a Go thing. Here's a random page I found advocating the style for js and css:

    http://lea.verou.me/2012/01/why-tabs-are-clearly-superior/
And here's what codinghorror had to say about the style:

> This way, in theory at least, the level of indent can be adjusted dynamically without destroying alignment. But I'm more inclined to think of it as combining all the complexity and pitfalls of both approaches, myself.

Which sadly seems to be most people's reaction -- basically, it might be better, but I'll be damned if I'm going to have BOTH tabs and spaces in my files!

Oh well...

Spaces are obviously a workable solution, but there are two really big downsides: (1) it throws away information -- when should I insert or remove a indentation block of spaces? You can make your editor guess, but it won't always guess right, and you won't always be editing inside your properly configured editor. A tab simply says what it means and requires no editor trickiness. (2) The obvious inability to adjust the visual width of the indentation, though personally as long as it's not too crazy I'm happy with anything from 2-8, so this doesn't bother me. (I normally run with tabs at 4.)

It's pretty simple, tabs are clearly superior in theory, by a decent margin. And spaces are superior in practice, by an enormous margin.

Go is making a play to make tab/space mixing work nicely in practice and that's quite interesting to watch.