Why You Should Use a Code Formatter

Ryan McDermott
May 31, 2017 · 2 min read

Code style is like the music during a dramatic scene in a movie: if you notice it then it’s a problem. Good style makes reading code a pleasurable and consistent experience. However, having to follow a style guide can feel far less than pleasurable. Think about how much there is to remember:

  • Indentation
  • Tabs vs. spaces
  • Line breaking before column count
  • Spaces between parentheses
  • Spaces before and after braces
  • Semi-colons at the end of statements
  • Line breaks on chained statements
  • Single quotes vs. double quotes
  • Trailing commas vs. no trailing commas
  • etc.

Like Alan Turing said, the end goal of a computer is to do the operations of a human. If it feels dull, then a computer should do it. Manually styling your code is about as fun as working on taxes at the DMV. Automate that job!

The benefits of automating code styling go beyond making your life easier: automatic code styling can potentially reduce bugs. “That’s preposterous!”, you might say. Well, you’re right that it’s not empirically verified and this is conjecture. What is true though is that the human working memory is very limited. Just try to remember the following numbers after one read: 71828182845904523536028747135266249775724709369995957496696.

The human working memory is limited to about 7 (plus or minus 2) items. If you and your teammates have to constantly occupy working memory with style rules while writing and reviewing code, you might miss important things. Your attention is just as limited, as demonstrated by the Invisible Gorilla experiment below:

In the famous study by Daniel J. Simons, participants are told to count how many times the players wearing white pass the basketball. Many participants fail to see the gorilla walk right through the scene. This demonstrates that when we focus attention on certain things we can miss major changes in our environment

When you format your code automatically, everyone can think about the important stuff when they read your work, and you can write as sloppy as you want and the computer will make it beautiful for you automagically! There goes your case of the Mondays!

If you’re using JavaScript, perhaps the best formatter is Prettier. Other languages have really great tools of their own, such as Go’s go fmt, Elm’s elm-format, and C++’s clang-format.

You and your team may not agree with every choice a formatter makes, and you may not even agree with the conjecture of less bugs via automatic formatting, but you’ll agree on far less if you let everyone try to follow a style guide. Code is written and reviewed by humans, we should do everything we can to limit human error. Get your team a formatter today, and don’t look back.

Thanks to Greg Ziegan

Ryan McDermott

Written by

Developer, musician, and lover of the great indoors. https://twitter.com/ryconoclast

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade