4 ms·
I prefer Gofmt's strategy/philosophy, which is to mostly let the developer control breaking, and only format around it. Gofmt will format everthing strictly, bu
by atombender 7y ago
I prefer Gofmt's strategy/philosophy, which is to mostly let the developer control breaking, and only format around it. Gofmt will format everthing strictly, but will leave decisions about "layout" to you.
This is a wiser design because the formatter can't know what the best layout is, and a formatter really ought to only format something where there's is an unequivocally, universally correct way of formatting something.
As an example, sometimes table-driven test are better written compactly, sometimes better verbosely. As a naive example:
for _, c := range []testcase{
{input: 1, expect: 10},
{input: 2, expect: 20},
{input: 3, expect: 30},
} {
assert.Equal(t, c.expect, someFuncToBeTested(c.input))
}
If the formatter starts splitting each testcase entry up over several lines, like so:
{
input: 1,
expect: 10,
},
...then you potentially lose readability. In other cases, you have more complicated structs that might fit on one line, but deserve to be formatted across multiple lines.
This is something Prettier doesn't always do correctly, and with Prettier, you don't have a choice. Opinionated is good when there is just one answer, but not when there's a range of possibly answers.
- triyambakam 7y agoYou've perfectly explained my daily frustration with Prettier. I am sad a few times a day about this for a project I'm on at this time.
- bryik 7y agoWell, there is //prettier-ignore https://prettier.io/docs/en/ignore.html#javascript https://prettier.io/docs/en/ignore.html#javascript
- ss3000 7y agoI personally really enjoy the automatic line breaking behavior of prettier as it frees me from having to make this entire class of purely stylistic decisions but still have code formatted in a reasonably legible manner. But I also have wished on multiple occasions that prettier was just an opinionated preset on top of a much more configurable core, which would make it less of a taboo to add options for what are obviously massively divisive formatting preferences. Case in point, all the most popular issues in the repo are some form of people asking for options or changes to the current behavior: https://github.com/prettier/prettier/issues?q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc https://github.com/prettier/prettier/issues?q=is%3Aissue+is%... And let's face it, that whole idea of a 1-size-fits all JS code formatting tool to end all formatting debates? That ship has sailed a long time ago. The debate just moved to what prettier options to use: https://prettier.io/docs/en/options.html https://prettier.io/docs/en/options.html I think the real value of prettier these days is that we can debate as a team once, decide on an option, and then have that decision be enforced automatically moving forward. A configurable core + opinionated defaults can serve that use case just as well.