Why you should never indent code with spaces

Tabs vs. spaces

It’s a big back-and-forth between developers, with lots of us swearing by the use of spaces, but I’m here to explain to you why everyone who uses spaces instead of tabs for indentation is not only wrong but seriously inconsiderate of their fellow developer.

First though, we must consider what a tab is and what a space character is. A tab is a typographical element specifically designed for the indentation of text to various levels. A space is a typographical element specifically designed to separate individual characters so as to distinguish words from each other.

Next we must consider the individual coders we work with. Each of us has a personal preference about how much indentation looks good, and is most readable to our eyes. Many of us like about four spaces worth per indentation level, but some prefer two or eight. What’s important here is that what’s readable and good looking to you is almost guaranteed to not look good to someone else working in the same codebase.

When you use spaces you are misusing a typographical element to emulate one that already exists. Not only that but you are enforcing your personal aesthetic on your co-contributers. However, if you were to use a tab for each indentation level then everyone else who works in the same codebase would see each level of indentation at a depth that they prefer. Every code editor out there allows you to specify how many spaces of indentation each tab should emulate. People who like it wide can set it to eight. People who like it skinny can set it to two. Everyone gets to see the code indented in a way that is aesthetically pleasing and readable to them.

There’s only one thing worse (in this discussion) than enforcing your indentation aesthetic on your co-contributers, and that’s combining tabs and spaces. When you do that it’s almost guaranteed to look screwed up in other people’s text editors. So turn on invisible characters in your editor (or at least have it display tabs). In summary:

  • Tabs were invented for indentation, so stop misusing the damn space bar for something it was never intended to do.
  • What looks good to you doesn’t look good to everyone elese.
  • What’s easily readable to you isn’t easily readable to everyone else.
  • Everyone gets to have indentation at a level they want if you use the freaking tool that was designed to indent instead of the one that was designed to separate words.
  • Turn on invisible characters so you know when some idiot’s mixed tabs and spaces.