— Why should we even care about how our code looks like if only computers have to deal with it? — you might wonder. Well, computers will for sure need to "read" and understand your code but in reality, we write code to communicate our ideas to humans (or our future selves, most of the time).
Reading text is a cognitive load and requires mental energy. Reading a computer program is even more so since in order to understand the code you have to interpret it. So anything that can reduce the workload to our brains is improving our productivity. I believe that consistent code style leads to a lesser cognitive load. After we get used to some style, our brains won't need to interpret every single character but only meaningful differences in patterns. This is similar to how good typography can boost cognitive performance.
Ensuring good practices
During my work in a small team a couple of years ago, I wanted to introduce code quality tools to the codebase in order to ensure everyone uses same code style. It turned out to be a painful manual process where I had to convince everyone and then help team members to set up their environments to use those tools.
That kept me thinking if that can be automated, so I wrote lint-staged — a tool that made it easy to enforce good practices around code style across the team. In a nutshell, lint-staged runs arbitrary code quality tools on the files you have changed before you commit them to the Git repository. If any of those tools report errors, the commit will be aborted, preventing of badly formatted code slipping into the repository. The most important part of it, though, was that those commands now were shared across the team members automatically.
Make it seamless
It improved code quality dramatically but sometimes people were annoyed since this tool "kept them from doing their work". I've noticed that most of the time, commits were aborted because of code formatting inconsistencies so this kept me thinking about how this process can be automated even further.
Many modern language-specific code quality and formatting tools can reformat code automatically without doing manual work. Some modern languages even have such formatting tools backed into the language toolchain. Other languages might have third-party tools as well.
Prettier is an opinionated code formatter. This means, there are almost no options you can choose from. Almost everything is pre-defined but most importantly, you can automatically reformat your code to match those rules. This makes the combination of prettier and lint-staged so powerful and workflow so seamless that you might not even notice it!
Outsource less important decisions
At first, the idea to leave the decision about how your work will be formatted might seem crazy but this has a huge benefit in long-term. By outsourcing all style decisions to the community, you get more time to work on meaningful stuff and consistent code base across the team. This, in turn, saves you tons of cognitive energy when it comes to reading code written by someone else.
Consistent code style improves developer experience and reduces the number of bugs but sometimes it's very painful to enforce single code style across the project or team. Instead of wasting your time on code style discussions and formatting your code manually, you can automate this and keep computers busy and concentrate on what's really important — your product and your users.