Tabs vs. Spaces: I’m switching sides

It’s not very often that I make a drastic change to my coding style but after using tabs for my entire life, I am making the switch to spaces. My go to defense for using tabs was that they are more flexible because they allow developers to choose their own indent levels. Most tabs versus spaces arguments cite this as the main reason as well. The other reason is file size, but let’s be honest, file size only matters with user-facing assets and modern minification techniques totally negate the argument.

For me, I have always stuck to a tab stop of 4 and use a mixture of tabs (to indent) and spaces (to align code). Tabs before the code and spaces to do things like lining up equal signs and comments and such. This approach actually works out very well for all tab stop sizes because the spaces keep everything aligned regardless of the tab stop size.

This had worked for me for quite a while, but I wanted to be consistent with how I was horizontally spacing my code and a mixture of the two is not very consistent at all. Being a diehard tabber, I thought perhaps I should just start using tabs all over the place and dropping spaces for alignment. Everything is great when I was working with my native tab stop size of 4, but at any tab stop size things fell apart.

Tab Stop 4

$small      = 'small'
$medium     = 'medium'
$gigantic   = 'gigantic'

Switching to tab stop 3 and 2 both looked great, but what about if we drop to 1?

Tab Stop 2

$small  = 'small'
$medium  = 'medium'
$gigantic = 'gigantic'

Alignment gets jacked. Now let’s go up to 5:

Tab Stop 5

$small         = 'small'
$medium        = 'medium'
$gigantic = 'gigantic'

Pretty close, but not quite there. The equal signs don’t align until you get back up to tab stop 8. The fact is, tabs are configurable but if you’re using tabs for alignment you’re going to run into inconsistencies depending on what the tab stop is set to. This is not flexible and the inconsistencies become very apparent even with this simple 3 line example. It actually gets worse depending on the length of the variable names. Not to mention that I think the extra whitespace before the equal signs is not aesthetically pleasing to me. This looks much better:

Spaces FTW!

$small    = 'small'
$medium   = 'medium'
$gigantic = 'gigantic'

I haven’t quite made the jump yet because I still need to do some research on using vim with spaces as this is entirely new territory for me. Even though I am switching teams (and being warmly welcomed) I am not going full turncoat and adopting the PSR formatting standards. I’ve already stated my peace on using Allman style indenting back in 2009 which incidentally, also boiled down to having consistency throughout my code as well as actually having a functional benefit in doing so.

That being said, the truth is you need to use what’s good for you and in situations where you’re on a team, you need to fall in line with any pre-existing coding standards. Nobody likes it when you end up forcing your ideals on them, or go reformatting someone’s code like this jerk right here.

Are you #teamtabs or #teamspaces? Comment below and/or tweet at me! My mind’s made up, but I’m always open to other’s people’s opinions (keep it classy though, trolls 😉

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.