Autoformat Your Code
- 2 minutes read - 425 wordsAuto-formatting code is good, and you should be doing it all the time. Why? It helps you write better code in real-time, preempt trivial discussions, and maybe even democratize programming.
Better Code in Real-time
First, writing better code in real-time: I’ll use the Prettier auto-formatter as an example. I have Prettier running everywhere, auto-formatting on write. This means that each time I write my file, which I do a lot, Prettier attempts to format my code. Neatening my work as I go along helps me think clearly and see refactoring opportunities.
There’s an amazing side-effect: if my code isn’t compilable code, Prettier can’t format it. When I save a file and Prettier doesn’t adjust the code, I know my code isn’t compilable. Instead of viewing an error in the logs or the browser console, I get instant feedback that something is wrong.
Preempts Trivial Discussions
Secondly, auto-formatting flattens the amount of trivial style debates on teams. Before auto-formatters became widely used on the frontend, many conversations centered on subjective style preferences. Do we include a trailing commas? Tabs or spaces 😈? Code isn’t a style showcase; it’s a tool to provide customer value. From that perspective, most distinctions like this do not matter.
Democratizes Programming
Finally, I think auto-formatters help democratize programming. Early in my career, I wrote Ruby, a stylistically permissive language. As a result, I honed strong opinions about styles. I believed code should be written in one style, and I proudly enforced the rules. I liked that you could recognize a seasoned Rubyist by how they formatted their code.
Nowadays, I’m more interested in what the code does. I think of autoformatting like a uniform: when everybody looks the same, appearance becomes less important than action. Auto-formatters give us an opportunity to make code about features, rather than knowing and expertly following a long list of unwritten rules.
Community-Level Support
I love how communities like Elixir and Go introduced built-in formatters. Proposed features on such projects are generally about behavior and strategy, not tactical style. What a gift! Compare that to teams bikeshedding trivial style choices, maintaining long configuration files that enforce an ever-changing set of rules. Who’s doing better work? Who’s having more fun?
I spoke about this at ElixirDaze 2018, advocating for Elixir’s then new autoformatter. As time has passed, that strategic decision has aged very well.
Auto-formatting is good. Beyond the above arguments, auto-formatters save me untold keystrokes that I’d waste pushing characters around. Install an autoformatter and write better code, reduce meaningless debate, and make programming more accessible.