Tip of the Week: Prettier - An Opinionated Code Formatter

Format your code fast, easy and consistent.

Rahul Pulikkot Nath
Rahul Pulikkot Nath

Table of Contents

Code Formatting is an essential aspect of writing code, and I did write about this a while back on introducing code formatting into a large code base. It's not about what all rules you and your team use, it's about sticking with the conventions and using them consistently. Code Formatting rules are best when applied automatically, and the developer does not need to do anything in particular about it.

Prettier is an opinionated code formatter, which supports multiple languages and editors and easy to get started. Getting set up is as easy as just installing the prettier package using yarn/npm. There are multiple points at which you can integrate Prettier - in your editor, pre-commit hook or CI environments.

Most of the IDE's have plugins for Prettier which makes it easy to get it into the code right from the beginning. You might need to update your IDE settings to run prettier when you save a file. For VS Code I have to set editor.formatOnSave to true to turn on this behaviour.

As the title says, Prettier is opinionated, which is useful in many ways and removes much time wasted on unnecessary discussions. However, it does provide some configuration options. Check if it provides enough for you to call a meeting to decide on one of them :).

Write prettier code!

TipOW