Search



Tags


Recent Posts


Featured Posts


.NET Oxford Links

Tabs vs Spaces

20 January 2014

Apologies for the trivial nature of this post. When it comes down to it - who cares if you use tabs or spaces for indentation? However, I've recently changed over sides in the age old argument, so thought I'd write about it anyway!

For all my programming career I've used tabs for indentation. It hadn't even occurred to me to try using spaces instead, as I was so set on using tabs - not understanding why anyone would want to use spaces. I even remember that there was a bit of an uproar in one of my previous places of work when one of the lead programmers had subtlety (without telling anyone) added a paragraph into the coding standards section of our internal wiki stating that all code should use spaces for indentation. The uproar was in the form of a heated thread in our internal forum. From what I remember, the opinions were pretty much 50/50.

One annoyance I've found with tabs, which made me decide to try spaces instead, is due to the multi-lined nature of Linq queries. If you're not familiar with Linq, then here's a simple example:

var result = from x in MyCollection
             where x.MyProperty = 5
             select x;

Using spacing instead makes it much easier to keep these in line. Likewise when splitting method parameters over multiple lines.

It also makes it much easier to paste code into a wiki or Evernote. This was the main argument that the lead programmer I mentioned above had - as we had both an internal wiki and forum which we quite often pasted code snippets into. This is also the reason why I started using Evernote for code snippets, as Evernote doesn't properly support the tab character.

My main reason previously for not liking spaces for indentation is that to delete an indentation, you had to press the backspace key multiple times. However, modern IDEs and text editors do seem to handle this better anyway, so you only need a single backspace per indentation. I used to use Vim as my primary editor, which didn't do this. Knowing Vim, there's probably a setting for this though!

One thing I've realised when changing anything related to coding standards, is that whilst a change feels horrible - it very quickly become the norm, and then your old formatting looks wrong. So change isn't as bad as you think.