gofmt: is it really wise to attempt to force a formatting style on users?

544 views
Skip to first unread message

Antoine Chavasse

unread,
Nov 11, 2009, 7:44:48 PM11/11/09
to golang-nuts
I think it's pretty neat that a source code formatting tool is
provided along. It's also a nice thing that the parser/ast/pretty
printer are available as mods.

However, I'm not sure that trying to enforce a formatting style by
denying the ability to configure the formatter (as the FAQ mentions)
is a good idea. It could be good that at least the go/printer package
allows more flexible configuration even if the command-line gofmt tool
doesn't.

I don't think it's so bad that different C/C++ project use different
set of conventions and I doubt that the plans of encouraging everyone
to use the same style by making go/printer non-configurable will
really work. Instead, I expect that many people will do what I intend
to and customize it, even at the cost of pulling go/printer out and
including a tweaked version of it in the formatting tool source tree
(opening braces at end of if/else/etc. instead of on their own lines
drastically reduce readability for me)

Ian Lance Taylor

unread,
Nov 11, 2009, 8:09:29 PM11/11/09
to Antoine Chavasse, golang-nuts
Antoine Chavasse <a.cha...@gmail.com> writes:

> Instead, I expect that many people will do what I intend
> to and customize it, even at the cost of pulling go/printer out and
> including a tweaked version of it in the formatting tool source tree
> (opening braces at end of if/else/etc. instead of on their own lines
> drastically reduce readability for me)

I hope that when you do tweak it, you will do it by adding command
line options to gofmt, so that other people can take advantage of that
as well.

Ian

Russ Cox

unread,
Nov 11, 2009, 8:21:45 PM11/11/09
to Antoine Chavasse, golang-nuts
> I think it's pretty neat that a source code formatting tool is
> provided along. It's also a nice thing that the parser/ast/pretty
> printer are available as mods.
>
> However, I'm not sure that trying to enforce a formatting style by
> denying the ability to configure the formatter (as the FAQ mentions)
> is a good idea. It could be good that at least the go/printer package
> allows more flexible configuration even if the command-line gofmt tool
> doesn't.

No one is or will ever be 100% happy with the output of gofmt,
but it's actually pretty good, and what's more, people adapt
surprisingly quickly to styles that at first seem foreign. Especially
in formatting, style is really just "what you're used to".
Since Go is an entirely new language, it should not be too
hard to get used to a different formatting too.

We hope that people will accept the output of gofmt precisely
because it puts an end to these kinds of style debates. How
many different brace styles are there in C? Too many.

Personally I find it liberating to let gofmt format for me,
because it means I have more neurons available for
attacking interesting programming problems. There are
things I don't like about gofmt's output, but I love not
worrying about them anymore.

But all of this misses what I think is the most exciting thing
about gofmt: the fact that we have a tool that can pick up every
source file in the Go tree, parse it into an internal representation,
and then put the exact same bytes back down. (A large part
of this is due to the amount of work that went into gofmt, and
the rest is because we have agreed to standardize on gofmt's
style.) Once you have such a tool, it becomes very easy to
insert mechanical processing in the middle, between parsing
and printing. So we have all the hard parts of a program
manipulation tool just sitting waiting to be used. I've started
writing similar tools for C before and never quite got the output
to match the input exactly. Agreeing to accept "gofmt style"
is the piece that makes it doable in a finite amount of code.

I hope that people will use gofmt, as is, for their own code.
As I said, it doesn't take more than a couple weeks to get
used to a new coding style, especially if you are in a brand
new language, and there are huge benefits to everyone using
the same style.

We certainly plan to keep formatting all the code in the
Go tree using gofmt. Like Go, it's an experiment.

Russ

a.cha...@gmail.com

unread,
Nov 12, 2009, 3:44:32 AM11/12/09
to Ian Lance Taylor, Antoine Chavasse, golang-nuts
On Nov 12, 2009 2:09am, Ian Lance Taylor <ia...@google.com> wrote:

>
> I hope that when you do tweak it, you will do it by adding command
>
> line options to gofmt, so that other people can take advantage of that
>
> as well.

I wouldn't mind doing that but since it seems contrary to the intended design philosophy of gofmt I'm not sure you guys would accept such a patch. I will probably give a shoot at getting used to the default gofmt formatting anyway before embarking on such a task.

David Smid

unread,
Nov 12, 2009, 8:55:19 AM11/12/09
to golang-nuts
On 12 lis, 09:44, a.chava...@gmail.com wrote:
I prefer left brace on the next line as well thus I tried to implement
it by adding command line option to gofmt :
http://code.google.com/p/go/issues/detail?id=99

I think I fell in love with Go.

David
Reply all
Reply to author
Forward
0 new messages