Tabs vs Spaces and Why You Should Always Use Tabs
Monday, 4th March 2013, 19:51
Wow, been ages since I've had the time to update my blog, and when I'm insanely busy the first thing to suffer is always my blog :( I feel really bad about that but the best solution to this is add another entry, so here we go, I'm going to wade in on a contentious topic, that old classic tabs vs spaces in coding.
So, it's probably best to start with my general opinions on formatting of code. I have preferences, I prefer lots of nice white space, but I'm not pigheaded enough to think that the way I choose to format things is better than anybody else's, it is just personal preference.
Because my background comes from C/C++, I tend to favour the sort of coding style that dates back to when we had editors which didn't know what syntax highlighting was and debuggers which showed your code next to the compiled assembly language, usually on text mode 80 column screens.
Probably the closest thing to my coding style is the standard that the MAME team demand. Which translates into Javascript (the language I code most in presently) like this:
if (var a = 0; a < 1; a++)
{
// do things
}
As opposed to what is almost the complete opposite more cramped:
if(var a=0;a<1;a++) {
// do things
}
I find the former easier to read and follow. But I accept that this is just me, and when I'm coding with other people's projects where possible I try and follow their style as best I can.
Tabs or Spaces?
Which leads us onto the thorny much disputed question of whether you should indent at whatever level you choose to indent to, using tabs or if you should use spaces. And the answer is simple, tabs! Always tabs!
The reason I'm going to give isn't that spaces consume less characters in files than spaces, whilst they do, this is source code that gets compiled or minified usually in some way or another, so who cares about that.
It's just one simple reason, and that is choice.
Usually when coding, you can use any editor you like, pick any font you like, even non-proportional ones if your editor supports it and you are a bit weird like that. You might not be able to decide how your code is generally formatted, but in this day and age with people coding on everything from a small screened laptop to a 1920 pixel wide monitor and beyond, being able to choose your tab stop width is a luxury we can all live with.
If you prefer two characters for your tabs, then fine you can do it. If like me you stick with four, you can do that too!
Unless the indenting is with spaces, that is. At that point you are removing choice for no benefit whatsoever. You wouldn't want to inflict your font choice and size on another coder, so why would you want to inflict your tab stop width?
All our brains work slightly differently, some people look at wide open white spaces and struggle to see the whole code picture, others look at tight code text and cannot deal with the bombardment of information their eyes take in.
So use tabs, there is really no reason not to, where as there is at least a few for not using spaces.