Prettier is a **big deal **(as is any other robust code formatter for that matter). This article will showcase why it is so essential and how to introduce it to an existing codebase.

If you have never heard of automatic code formatters before — these are tools that parse your code, transform it into AST, and then “re-print” it in a nice way. The primary purpose of this process is to ensure that all code in your codebase follows the same styling rules.

So, no matter how many quotes you use to define string or whether you put semicolons at the end of each line — the code formatter has its own opinion on how your code should look.

At first glance, it may seem to be a disadvantage — you, as the author of your code, know better where to put an extra newline to emphasize that certain pieces of code are related or when an arrow function should be defined in one line (even though it is 300 symbols long).

However, the problem is that everyone has an opinion on how code should look, and at times it may be tough to find a common denominator on code styling perspective.

Moreover, when you are sending a pull request for review and your teammate leaves one hundred nit-picky code-style comments which, to his best knowledge, must be addressed (because otherwise, the code would be unreadable and “ugly,” right?) — you spend time for nothing while adding semicolons, putting newlines between methods, and so on.

We are facing 2021, and I still see fellow C coders who are endlessly surfing through the giant proprietary “C code style document” trying to find/learn tons of styling rules. Of course, the major part of these rules has nothing to do with the concerns like the number of spaces in the tab. Still, I spend way more time on addressing tons of dummy “remove this new line,” “add one more space here” comments than I care to admit.

I used to be a C developer on a giant (10M LOC) set-top box project hosted in Clearcase. We had to follow huge and pretty well-defined coding standard. The main problem was that we didn’t have the tools to enforce rules automatically, so “surfing the document” was an integral part of our development and code review processes. We could have benefited a lot if we had a tool that would enforce all those rules automatically, but unfortunately, there was no such tool around back then.

Nowadays, the cool kids do not want to invest their time learning code-style documents (especially writing them), so now we have tools (like Prettier) that take code styling off our shoulders and let us focus on more meaningful things. So let’s discover why automatic code formatting is a good thing and what exactly it buys you.

  • 1. Code-style consistency
  • 2. Single source of truth
  • 3. No syntax errors

Adoption strategies

If you decide to incorporate automatic code formatting into your development process, there are a couple of ways to do it.

  • 1. “Big bang” approach
  • 2. File-by-file reformatting
  • 3. Format only changed lines

#programming #software-development #prettier #coding #vscode

Why the Prettier Code Formatter is a Big Deal
1.70 GEEK