4 ms·
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,
by throwaway894345 5y ago
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.
- rob74 5y agoNah, 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.
- perryizgr8 5y ago> 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?
- coryrc 5y agoWhen 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.
- throwaway894345 5y agoWhy 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?
- coryrc 5y agoI 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.
- radlad 5y agoIME 80 is terribly short in almost any language.
- perryizgr8 5y agoI like 80 because I can have three vertical windows open on my monitor and see everything without side scrolling.
- throwaway894345 5y ago> 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.
- skybrian 5y agoI 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.