Why You Should Use a Code Formatter

The idea behind digital computers may be explained by saying that these machines are intended to carry out any operations which could be done by a human computer. Alan Turing

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.

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