Why Prettier is already adding value and being adopted fast.

featured-30

Prettier is an opinionated code formatter. It was created by James Long to format modern JavaScript and JSX, inspired by Go’s gofmt and Reason’s refmt tools. It now supports typed JavaScript flavours such as Flow and TypeScript as well as CSS, Less and SCSS.Enforcing a consistent code style across projects makes code easier to read and understand for all team members. It can be especially helpful for newcomers unfamiliar with a project’s codebase. However coming to a consensus on the style to adopt can be difficult, sparking seemingly endless “tabs vs. spaces” type debates that are irrelevant to the product being built. Policing the style is also a challenge, resulting in a lot of “nitpicking” in code reviews.

Static analysis tools such as ESLint can help flag up style problems alongside other rules but require extensive configuration and have very limited ability to fix issues automatically. Prettier takes a different approach by taking the maximum line length into account. It parses the code into an Abstract Syntax Tree, throwing away all the original formatting, and reprints it from scratch using an algorithm based on a paper by Philip Wadler.

Extensions are available for popular code editors which can format your code with Prettier the moment you press “save”, allowing you to stop worrying about formatting and focus on application logic. This is remarkably freeing!

Prettier provides very few configuration options and requires your team to adopt its opinionated code style. This may not match your team’s preferences exactly, however Prettier’s automatic formatting is so tremendously useful that it soon wins you over. Wide adoption of Prettier in the community is beginning to result in a consistent code style across many open source projects.

In existing projects the automatic formatting of files can sometimes make pull requests more difficult to understand and review due to many small formatting changes alongside logic changes. This can be mitigated by regularly running Prettier across the codebase.Prettier is one of those tools which is so useful you look back and wonder how you ever did without it.