3 ms·
You obviously don't work with a team, or you work with a team that also doesn't have opinions about style. That's probably not a great thing. The tab-vs-spaces
by Jemaclus 11y ago
You obviously don't work with a team, or you work with a team that also doesn't have opinions about style. That's probably not a great thing. The tab-vs-spaces debate is very real, as well as things like whether hanging commas or trailing spaces are allowed, or whether you put one-if-blocks on one line or if you have to split them up into multiple lines, or whether the opening bracket goes on the same line or the next line, or whether... I could go on. All of these are things people tend to have preferences for, so when you work with multiple people, having a consistent style is a huge benefit to reading other people's code.
Imagine a simple CSS example: Some people like to do one line per element, while others like one line per style:
```
a { font-weight: bold; color: blue; text-decoration: none; }
```
vs
```
a {
font-weight: bold;
color: blue;
text-decoration: none;
}
```
That's a pretty trivial example, and for many people, both are acceptable. But when you get into large projects with hundreds or thousands of styles, or you have to code review that one change that was made off-screen on a really long line of CSS, one of those styles is going to be more conducive to code reviews and readability. Stylistic concerns are very real, and gofmt largely makes those go away.
Like kyrra said, you may not be opinionated on the matter, but other people are, and gofmt, et al, completely remove that concern from the table.
- Jemaclus 11y agoerk, that totally screwed up my code example... sorry. :)
- spion 11y agoLike I said, other code reformatting tools exist, the team lead picks the tool and tool settings, and that should be the end of it.
- Jemaclus 11y agoYes, this is true, but you're overlooking a few of the things I mentioned earlier. Gofmt is extremely opinionated -- you can't customize it. Most other tools involve customizing to fit your own style conventions. Gofmt doesn't allow that. Gofmt also ships with Go, so it's a built-in language tool, not a third-party tool. That's a huge benefit right out the gate, meaning that everyone's code will look the same. You don't really appreciate how nice this is until it's actually true Finally, gofmt is dead simple to use. `gofmt <file>`. Boom. Done. Compare with, say, PHPCS, which is like: `phpcs --standards=<standards-file-path> --autofix=true --exclude=<exclude path>` and so on. Gofmt is just stupid simple to use. Yes, you're right that other tools exist, but gofmt is the one that really makes it click for developers, and they think "Man, THIS is how code formatting should work." And then we take it over to the other languages, to C or Python or Javascript or PHP, and we introduce tools that are similar to gofmt. That's why gofmt in particular has "changed my life" (hyperbole, sure, but the point remains that I code completely differently now). Without gofmt, it's doubtful I'd ever adopt code formatting tools to the point that I have today.
- sacado2 11y agoI have already used one such tool for C++ in one of my teams (aastyle ? Something like that). It's only the firs step. Then you have to wonder/discuss: what actual style to use? There are many C/C++ styles out there (stroustrup, linux, gnu, OTBS, etc.), so you have to discuss about that. Now, when you have chosen one, you have to discuss about minor fixes: "ok, so OTBS is cool, but what about line lengths? Should we choose 80 cols? That's silly, why not 120?" etc. All in all, you end up taking a whole hour (even more) about this issue. With go, that doesn't happen. You call gofmt and you're done. Another side effect: when you get external source code (open source, code written by another team, etc.), you know it will be formatted exactly the same way as yours.
- spion 11y agoI'm afraid that may be your fault. What style you pick doesn't matter much (consistency does though) so you should just pick one. It really baffles me that anyone could spend so much time on something like that. I'm sure there are lots of more exciting (or more pressing) problems to solve on the projects you're working on...