How do you format your code?

Tabs or spaces?

Are you a tabs person or spaces person?

How about cuddles vs uncuddled curly braces?

And most important: How much time do you spend thinking about these things while writing your code, or reviewing code from a coworker?

Hint: There is only one correct answer.

And that correct answer is: Zero! None. Zilch. NO TIME AT ALL!

If you’re thinking about these sorts of details while coding or reviewing, you’re wasting your time.

Pick a format. Enforce it with a tool. And never think about it again!

Now don’t get me wrong. I’m not saying code formatting is unimportant. It really is important. Not only are some ways of formatting code harder to read (at least for most people) than others, but consistency goes a long way.

There’s nothing worse than reading a file that mixes tabs and spaces and curly brace placement.

So formatting is important. But which format is much less important.

Some languages come with a defined acceptable format out of the box. Go is one such language, which ships with a tool called gofmt to re-format your code for you according to the officially accepted format. But many older languages have begun adopting this approach as well, with de facto standards, if not official standards. Now there’s a saying in the Go community: “Gofmt’s style is no one’s favorite, yet gofmt is everyone’s favorite." gofmt uses tabs. I prefer spaces. But I still love gofmt, becuase I haven’t had to care or even think about tabs vs spaces for years.

What code formatting rules do you use? Are they enforced by a tool (in your editor and CI pipeline)? If not, do yourself, and the rest of your team, a big favor, and take an hour and go set that up. Right now. Seriously. Don’t wait.


Speaking of Go, did you know that I also write a daily email about the Go language? I’m breaking down the Go Spec, piece by piece, and explaining it in easy-to-understand, digestible bits. If that sounds interesting, you should subscribe!

Share this