Tabs vs Spaces

When coding, should we use spaces or tabs to indent our code. Flame on...

Let's tackle the simple arguments first :

  1. Tabs must NEVER be used in the middle of a line. That leads to madness, because then the size of the tab becomes vital for correct formatting, and there is no consensus on the size of a tab (the ancient standard is 8 columns, but I've never met a tab fan that uses 8 column tabs.
  2. Tabs are NOT designed for indentation (I've heard people state with passion that spaces are to separate words, and tabs are for indenting lines. This is just not true. Tabs (which is short for tabulation), is to create tabulated data, and that is something that programmers don't do. They only indent. That doesn't means that tabs can't be used for indentation, just that they weren't designed for that purpose.
  3. Even if tabs were designed for indentation, that doesn't mean it is the best solution.
  4. Using Spaces doesn't mean using the space bar. I use the tab key to indent and un-indent my code - my editor just happens to use spaces when performing the indentation
  5. I hope everyone can agree that mixing spaces and tabs to indent a single file is NEVER a good idea.
  6. Tabs and spaces don't have to look the same - any half-decent editor can display tabs using a light grey chevron ยป
  7. The traditional "make" tool is evil for using a tab character as special syntax - especially as it was written when people were using non-gui editor, and printed paper, where tabs and spaces do look the same. AFAIK, "make" is the only tool that forces tabs vs spaces.
  8. Using spaces is no harder or easier than using tabs. Whether you use tabs or spaces, indentation is performed by pressing the tab key (and shift+tab to un-indent).

OK, so mow I'm going to be a traitor to my space-loving clan...

Using tabs allows each developer to choose her personal preference for indentation size. So I can view a tabbed file, using my preference of 4 columns per indent, and my colleague could choose 2, or 8. The file doesn't need to be changed to suit our own preferences. As far as I'm concerned this is the ONLY advantage of tabs. (And for some people this is a big enough reason to ignore all others).

So, why don't I use tabs?

I'm too practical for that! Here's my reasoning...

  1. Source code appears in lots more places that our IDEs. Code snippets appear in wikis, blogs, emails etc. The "Tab Width" preference isn't set in these places, so tabs will usually be displayed using the "good old fashioned" 8 columns per tab. As I've already mentioned, I've never met a tab-lover who likes 8 columns per tab. Therefore, when tab infested source code is copy/pasted into an email, blog or wiki, the formatting will look bad to EVERYBODY!
  2. Sometime tabs are displayed at all! I've seen numerous blog/wiki where tab characters are silently ignored, making tab infested source code unreadable.
  3. While I have a preference for 4 columns per indent, there are occasions when I prefer 2 columns. e.g. HTML can have large amounts of nesting, and so 2 column indents are preferable. I know of no good solution to this when using hard tabs. Using spaces is much more flexible. This is an example where your personal preference should be trumped by the needs of the source code. A highly nested 2 columns indent file will display nicely when using spaces.

PS. For many years I used three spaces for indentation. I can imagine many people being horrified at such a choice, and loosing all respect for me. Hear me out though... I was coding on an old-fashioned text-only terminal (many years ago), and the keyboards had a key which sent three spaces.

And just in case you tab-lovers haven't realised yet, there was no tab key, and no way to insert a tab character into the source code. So if you think there should be a single standard way to indent code, then tabs must be ruled out straight away!