My theory on why Go's gofmt
has wound up being accepted
In Three Months of Go (from a Haskeller's perspective) (via), Michael Walker makes the following observation in passing:
I do find it a little strange that gofmt has been completely accepted, whereas Python’s significant whitespace (which is there for exactly the same reason: enforcing readable code) has been much more contentious across the programming community.
As it happens, I have a theory about this: I think it's important
that gofmt
only has social force. By this I mean that you can
write Go code in whatever style and indentation you want, and the
Go compiler will accept it (in some styles you'll have to use more
semicolons than in others). This is not the case in Python, where
the language itself flatly insists that you use whitespace in roughly
the correct way. In Go, the only thing 'forcing' you to put your
code through gofmt
is the social expectations of the Go community.
This is a powerful force (especially when people learning Go also
learn 'run your code through gofmt
'), but it is a soft force as
compared to the hard force of Python's language specification, and
so I think people are more accepting of it. Many of the grumpy
reactions to Python's indentation rules seem to be not because the
formatting it imposes is bad but because people reflexively object
to being forced to do it.
(This also means that Go looks more conventional as a programming language; it has explicit block delimiters, for example. I think that people often react to languages that look weird and unconventional.)
There is an important practical side effect of this that is worth
noting, which is that your pre-gofmt
code can be completely sloppy.
You can just slap some code into the file with terrible indentation
or no indentation at all, and gofmt
will fix it all up for you.
This is not the case in Python; because whitespace is part of the
grammar, your Python code must have good indentation from the
start and cannot be fixed later. This
makes it easier to write Go code (and to write it in a wide variety
of editors that don't necessarily have smart indentation support
and so on).
The combination of these two gives the soft force of gofmt
a great
deal of power over the long term. It's quite convenient to be able
to scribble sloppily formatted code down and then have gofmt
make
it all nice for you, but if you do this you must go along with
gofmt
's style choices even if you disagree with some of them.
You can hold out and stick to your own style, but you're doing
things the hard way as well as the socially disapproved way, and
in my personal experience sooner or later it's
not worth fighting Go's city hall any more. The lazy way wins out
and gofmt
notches up another quiet victory.
(It probably also matters that a number of editors have convenient
gofmt
integration. I wouldn't use it as a fixup tool as much as
I do if I had to drop the file from my editor, run gofmt
by hand,
and then reload the now-changed file. And if it was less of a fixup
tool, there would be less soft pressure of 'this is just the easiest
way to fix up code formatting so it looks nice'; I'd be more likely
to format my Go code 'correctly' in my editor to start with.)
|
|