Hacker News new | ask | show | jobs
by skybrian 1588 days ago
It's odd that nobody followed Go's formatter in letting developers break lines themselves and mostly fixing indentation and spacing. I thought they made good choices.
2 comments

Honestly the only grievance I have with Go's formatter is that it doesn't automatically break lines. I'd be a big fan of "if two programs parse to the same AST, they should format the same" and if that's too aggressive perhaps allow for `// go:nofmt` annotations or something. In whatever case, `gofmt` gets at least 95% right.
Nah, I can totally understand why they decided to stay away from this can of worms. First, what max line width do you choose? Second, where do you break a line if it's too long? I think gofmt gets the balance exactly right: makes source code easier to read by providing a unified formatting style, but doesn't get in your way more than necessary.
> First, what max line width do you choose?

The whole point of an opinionated formatter is to have opinions about these sorts of things.

> Second, where do you break a line if it's too long?

It depends on the context. Yeah, writing the algorithm to make these decisions is a little complex, but it's also well-understood.

> doesn't get in your way more than necessary

What is "necessary"? It seems like you're trying to say "it makes decisions on the things I think it should make decisions on" which is fine, but it's not like choosing between `struct {` and `struct{` is objectively more critical than line wrapping.

I believe one reason they chose it is so that automatic reformats like renaming a variable don't cause reformats to more lines than necessary.

Keeping lines reasonably short is nice but doesn't need to be done strictly. It can wait until someone edits the code.

> what max line width do you choose?

80

> where do you break a line if it's too long

Wherever a keyword or name ends, but does not exceed 80.

Gofmt has made opinionated decisions about everything, why stop at line breaks?

When I'm writing Python at Google, and get yet another error because my Python or Markdown line exceeds 80 characters, and read the fights on the mailing lists about changing the limit, I think Go was created because it was easier to create a whole new language than get the line length increased for Python.
Why do you get errors rather than auto-formatting (in the editor or in CI) and moving on with your day? I would have thought Google would have this sorted already?
I know! Usually the editor autoformats on save and while typing, but there are some edge cases where the regular incremental formatter fails but it's rare enough that I don't reflexively hit the "format all files" button and then get caught out by pre-submit tests.
IME 80 is terribly short in almost any language.
I like 80 because I can have three vertical windows open on my monitor and see everything without side scrolling.
I'm in this camp. Why do we still waste brain cells on this problem? Just copy it