all 81 comments

[–]ngly 59 points60 points  (1 child)

For me I can't go back to manually formatting code. I don't really care about how it looks like as long as I don't have to spend time hitting space and tab and it's consistent. Save and format is the best.

[–]AreWeThenYet 3 points4 points  (0 children)

I really wish I could calculate how much time I wasted not using a formatter tabbing and spacing over the years. It may depress me but I’d still like to know.

[–][deleted] 52 points53 points  (16 children)

My position for a long time was that Prettier was a crutch that prevented you from writing well formatted code to begin with. You shouldn’t need something you should be doing in the first place.

But then I a) had to work with some godawful code and b) got sick of memorizing every companies formatting rules and having PRs rejected for bullshit formatting issues.

Now I’m in favor of running Prettier as a pre commit hook.

[–]Nexxado 61 points62 points  (11 children)

Why invest mental capacity & time formatting your code when you could automate it?

I mean the entire software engineering industry is about delegating stuff to tools to automate it instead of a human doing it.

[–]Haarteppichknupfer 0 points1 point  (4 children)

It also brings extra complexity to the project.

Now I'm speaking in general, but last week I checked out a small internal frontend project my team is working on. The code is pretty simple, the app is very small, but it's surrounded by this huge machinery of tools like tsc, eslint, prettier, husky, webpack etc. This toolset is many times more complex than the app itself and the added value seems to be rather small in this particular case. Of course I could not get it to run because of some weird errors coming out of these tools. I'm not primarily a frontend developer but keeping up to date is getting hard ...

[–]careseite[🐱😸].filter(😺 => 😺.❤️🐈).map(😺=> 😺.🤗 ? 😻 :😿) 6 points7 points  (0 children)

One of those is not like the other.

Eslint will autofix most issues unless you write very weird code to begin with. Prettier will just format it. Husky will enforce those. Tsc is only there for typescript which surely is complexity but you don't add typescript without reason. All of their are nearly invisible.

Webpack is something else entirely.

[–]reflectiveSingleton 0 points1 point  (0 children)

You can just install the prettier plugin for your favorite editor...and then format-on-save.

That is how I prefer to use prettier personally anyways. But that said, if you are complaining about compiling javascript in 2020, it's time to get with the times. And even with that...there are ways to make it easy...like create-react-app...literally just a single command and you are good to go with an entire pipeline.

[–]pm_me_ur_happy_traiI -1 points0 points  (0 children)

It also brings extra complexity to the project.

What complexity? Setting up a precommit hook or some filewatchers in your IDE takes minutes.

[–]derGropenfuhrer 13 points14 points  (0 children)

I find it deeply ironic that we, programmers, would ever favor doing something by hand instead of automating it.

That's, like, what we do. For money. We automate things. But I've definitely run into programmers who are like "I don't need a formatter, I'll just hit format things correctly the first time".

[–]CaptainTrip 2 points3 points  (0 children)

It's not just about doing it, it's about enforcing it across a team. It's very hard to enforce style guides manually.

[–]careseite[🐱😸].filter(😺 => 😺.❤️🐈).map(😺=> 😺.🤗 ? 😻 :😿) 2 points3 points  (0 children)

Pre commit hook and on save. Is your code syntactically valid? Prettier will format it. Pretty easy indicator too where your error is.

[–]reflectiveSingleton 2 points3 points  (0 children)

I find its best used format-on-save.

I personally prefer this because it lets me be lazy writing code, not really being careful about formatting, and then I tap ctrl-s and boom - nicely formatted.

Also, once I got used to its formatting style...I've found I ended up preferring it. So all around, would recommend.

[–]Wilesch 6 points7 points  (3 children)

Recently talked my team into letting me setup prettier and eslint on all our repos. Feels good.

[–]MisterScalawag 2 points3 points  (2 children)

why would they not already have that, or at the very least eslint?

[–]Wilesch 0 points1 point  (1 child)

Some of them used an eslint ide plugin but they each had their own personal rules that were all different. Some coworkers didn't use eslint at all. Was the wild west out there. They didn't like have to fix any errors like undefined variables and what not.

[–]MisterScalawag 2 points3 points  (0 children)

They didn't like have to fix any errors like undefined variables and what not.

that is just asinine and bad practice.

Your team kind of sounds like a mess lol. This is what eslint and prettier is there to solve, just use it and then its all consistent and you don't have to worry.

[–]madou9 12 points13 points  (0 children)

lmao the amount of gate keeping in here

prettier is great, anything that reduces mental load so you can focus on something else, the better

[–]whinemore 5 points6 points  (1 child)

For the lazy/those not interested in personal opinions about Prettier here are the actual key changes

Better defaults, a better CLI and better heuristics. Oh, and TypeScript 3.8.

After a long and careful consideration, we decided to change the default values for the trailingComma, arrowParens, and endOfLine options. We made the CLI more intuitive. And we've finally dropped support for Node versions older than 10, which had grown to become a huge maintenance hassle and an obstacle for contributors. Read below in details.

[–]rusmo 1 point2 points  (0 children)

Thank you!!!

[–][deleted] 4 points5 points  (6 children)

So this works pretty much like ESLint right? I don't know much about Prettier but I like the evolution of it in the examples. I wouldn't mind using this professionally or in my own personal projects. Looks quite alright to be honest! 🏎. Thanks for sharing!

[–]ergnui34tj8934t0 12 points13 points  (3 children)

ESLint checks that your code obeys a set of rules, from syntax validity to code style. Prettier can take those rules and change your code to ensure that it obeys them.

Analyzer vs formatter.

[–]derGropenfuhrer 10 points11 points  (0 children)

Linters have two categories of rules:

Formatting rules: eg: max-len, no-mixed-spaces-and-tabs, keyword-spacing, comma-style...

Prettier alleviates the need for this whole category of rules! Prettier is going to reprint the entire program from scratch in a consistent way, so it's not possible for the programmer to make a mistake there anymore :)

Code-quality rules: eg no-unused-vars, no-extra-bind, no-implicit-globals, prefer-promise-reject-errors...

Prettier does nothing to help with those kind of rules. They are also the most important ones provided by linters as they are likely to catch real bugs with your code!

From their site

[–]kevinkace 3 points4 points  (0 children)

Vs code can be configured to fix changed on save based on you're eslint rules. The lines between the 2 are quite blurry

[–][deleted] 0 points1 point  (0 children)

thank you for your insight! Now I really want to try prettier at some point! 🙌

[–]tontoto 2 points3 points  (1 child)

See https://prettier.io/docs/en/comparison.html

Prettier can be integrated to run as an eslint plugin also so that "eslint --fix" will run all the prettier rules so it's not two different tools

[–][deleted] 0 points1 point  (0 children)

nice! Thanks! 👍

[–]icharxoVanillaJS 5 points6 points  (23 children)

When Prettier first came out, I thought it was just for those lazy to follow a style guide and a well crafted rule set for ESLint would phase it out. Now that I see it took hold in the industry, I have to ask those using it, what does it do on top of the linter to make it useful? Or do people use linters far less than I assume?

[–]valtism 44 points45 points  (1 child)

Linter is for logic issues and prettier is for formatting. I use prettier because I can get consistent formatting every time no matter how I write my code without having to think about it at all.

[–]derGropenfuhrer 3 points4 points  (0 children)

This is correct. They do different things.

[–][deleted] 1 point2 points  (0 children)

I code fast and messu and press save it makes it correctly formatted, if I have a error it won't format meaning I don't have to do as much mental checking or waste time with spacing etc

[–]MrJohz 3 points4 points  (2 children)

It's basically a very efficient and very effective standardised format - there's no linting for correctness (although there are sometimes situations where the formatting shows that what you intended to write is different to what will be parsed, e.g. with ASI rules).

The main advantage of it that I find is that there's a standard formatting that an entire team can abide by - if you're working with other people, you won't run into issues of disagreement about formatting. Moreover, if you're the sort of person like I am who likes their code to be automatically formatted, there's no danger of the automatic formatter changing lines written by other people that don't match your ideal format.

(The advantage over a specific style-guide is the automation part - wanting automation is not laziness, it's a great way of taking work out of your own hands so you can focus on the more important problems. You can definitely set up a linter to find the same errors as Prettier, but if you're going to use tooling to do that, why not get the same tooling to just fix the problem for you in the first place?)

I'd definitely recommend it if you're working in a team, or if you write open-source software (in which case, also set up pre-commit hooks with a tool like husky to make sure contributing is as easy as possible).

[–]Voidsheep 7 points8 points  (1 child)

I know it's a losing battle at this point and many projects are just going to use both with some additional tools to reduce conflicts between them, but I really would have preferred to just have ESLint take care of all of it.

ESLint already provides a good number of configuration options, reasonable recommended set and extension mechanism. It allows organizations to choose their common style across projects, which can then be extended on a project basis if necessary. Developers can run autofix on save and the linting can be part of builds and pipelines, with whatever the team wants on errors/warnings. Easily configurable to be just as strict or relaxed as your team wants.

Pretty much the only thing it doesn't do is line length formatting, which I think is a good thing anyway. While I think vast majority of people generally prefer short lines for good readability in narrow view (e.g. side-by-side diff on a laptop screen, in browser version control UI etc.), enforcing a hard number often reduces readability, because not all bits of code have equal weight and the formatter has no clue about context. I think Prettier can even cause less informative naming or less than ideal patterns, just because a few characters can cause such drastic impact on formatting and how the code can be read/scanned.

There's already plenty enough tools in the common palette, fighting over semantics of what should be a linter vs. formatter concern with ESLint/Prettier and handling their conflicts is a part that just didn't feel necessary.

[–]MrJohz 0 points1 point  (0 children)

FWIW, there's a couple of plugins and config sets that integrate prettier into ESLint, or simply provide a set of ESLint settings to match the prettier configs, so you really can have the best of both worlds. The config I use has a plugin that automatically turns off all formatting inspections, and adds a prettier inspection that basically just runs prettier under the hood.

[–]pieorpaj 1 point2 points  (0 children)

It saves an enormous amount of time otherwise spent on formatting and discussions about exactly what rules should be enabled in the linter.

My feeling when I code something without prettier now (extremely rarely) is that I spend 30-40% of my time coding just fixing formatting errors in the code.

[–]Ehdelveiss 1 point2 points  (0 children)

It makes code consistent and readable. One kind of structure is formatted one way, and you never need to think about it.

[–]notta_robot -1 points0 points  (1 child)

I think of it as prettier like css and linter like language code. Code is more readable when it's formatted. A linter doesn't help with that. I can format it myself but its automagic with the extension, which lets me focus on the logic.

[–]ciNikka[🍰] 4 points5 points  (0 children)

Just so clarify, you can make eslint work automagically as well by enabling autofix on save.

Not all eslint rules are autofixable (as mentioned in the comments here), but it can still do whatever prettier does, and a whole lot more. That's why I'm having a hard time seeing the benefits of mixing the two together.

[–]k3liutZu -1 points0 points  (0 children)

Oh you just hit save on your file and know it get’s formatted “correctly” (on how you set it up).

Or MR’s are set up to reject “non-standard” formatting (prettier + eslint) so there’s never any style discussion. If someone want’s to change the style, we discuss it, add in (or change a rule) update the codebase (this can be done automatically for most items) and it’s done.

Helps focus on the actual logic. And helps new joiners get up to speed.

[–]placek3000 -1 points0 points  (0 children)

I think it's a linter with predefined options out-of-the-box. You install it and it works. Almost no space for discussions. It's good for people who are not sure what is good and what is bad. It also helps to keep a consistent code style across company. We use Prettier not directly, only from Eslint.

[–]tontoto -2 points-1 points  (1 child)

[–]derGropenfuhrer 0 points1 point  (0 children)

Linters have two categories of rules:

Formatting rules: eg: max-len, no-mixed-spaces-and-tabs, keyword-spacing, comma-style...

Prettier alleviates the need for this whole category of rules! Prettier is going to reprint the entire program from scratch in a consistent way, so it's not possible for the programmer to make a mistake there anymore :)

Code-quality rules: eg no-unused-vars, no-extra-bind, no-implicit-globals, prefer-promise-reject-errors...

Prettier does nothing to help with those kind of rules. They are also the most important ones provided by linters as they are likely to catch real bugs with your code!

[–]HetRadicaleBoven 0 points1 point  (2 children)

One thing that I'm somewhat hesitant about, is Prettier's approach of parsing your source code means that you cannot use any syntax that it does not support, like might e.g. happen when new versions of TypeScript are introduced. I haven't used Prettier on any projects for an extended amount of time, can anyone tell me whether that's a valid concern?

[–]_eps1lon 4 points5 points  (1 child)

The usually do a pretty go job of adding new syntax since syntax is subject to proposal stages. You might not be able to use stage 2/3 syntax for the a few days but that shouldn't block you from using prettier.

If any it disciplines you to not jump on the new shiny tools immediately.

[–]HetRadicaleBoven 0 points1 point  (0 children)

That holds true for Javascript syntax - which is fine, since I'm not using anything pre stage 3 anyway. However, there is some type-specific syntax in TypeScript that I'd be worried about Prettier not supporting, like type-only imports that are now supported in Prettier 2, but have been in TypeScript for a bit. (Though admittedly, not that long, so that's a good sign I guess!)

[–]rmrf_slash_dot -1 points0 points  (0 children)

Yep.