Jake Worth

Autoformat Your Code

Published: March 10, 2022 2 min read

Auto-formatting code is good, and you should be doing it all the time. Why? It helps you write better code in real time, it preempts trivial discussions, and it helps democratize programming.

First, writing better code in real time: I’ll use Prettier as an example. I have Prettier running on every JavaScript variant I touch, auto-formatting on write. This means that each time I write my file, which I do often, Prettier attempts to format my code. Neatening my work as I go along helps me think clearly and see refactoring opportunities.

And there’s an amazing side-effect: if my code isn’t valid JavaScript, Prettier can’t format it. When it doesn’t ‘move’, 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. Amazing.

Secondly, auto-formatting flattens the amount of trivial style debates on teams. When I was programming before auto-formatters became widely used on the frontend, many conversations centered on style. Do we include a trailing comma on an object or hash? Tabs or spaces 😈? Code isn’t a style showcase; it’s a tool to provide value. And from that perspective, most distinctions like this do not matter.

Finally, I think auto-formatters help democratize programming. Early in my career, I wrote Ruby, a stylistically relaxed language, and honed strong opinions about styles. I believed code should be written in one style, and I was proud to know the rules. I liked that you could recognize a seasoned Rubyist by how well they knew the rules.

Nowadays, I find these debates boring. I’m more interested in what the code does. It’s a uniform: when everybody looks the same, appearance becomes less important than action. Auto-formatters give us a chance to make code about features and creating, rather than expert rule-following.

I love how communities like Elixir and Go introduced built-in formatters. The conversations on such projects are generally about behavior and execution. What a gift! Compare that to a team that’s bikeshedding trivial style choices, or maintaining long configuration file 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. I’m still impressed with that decision.

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.

✉️ Get better at programming by learning with me. Subscribe to Jake Worth's Newsletter for bi-weekly ideas, creations, and curated resources from across the world of programming. Join me today!


Blog of Jake Worth, software engineer in Maine.

© 2022 Jake Worth.