I know this debate is old as dirt, but it was resurrected on my Google+ stream not too long ago, so I wanted to poll the intertubes and see what it, the Font of Knowlej, thought.
It’s pretty straightforward: when one is writing code, does one use tabs or spaces to indent?
At first glance it doesn’t seem like that big of a deal. Everyone has experience with writing papers, and the action is almost second-nature to hit the tab button after every paragraph. The decision is almost unconscious. Why, then, would coding be any different?
To be perfectly honest, the issue itself didn’t become apparent to me until very late in my undergraduate career. The crux is that you don’t realize this is an issue until you start sharing or collaboratively working on the same codebase.
Arguments for spaces
They’re consistent across platforms. “A space by any other operating system will still be a single space”, to roughly paraphrase Shakespeare. When you’re writing code under Windows, OS X, Ubuntu, or Sky OS, the spacebar cranks out the same amount of space in between characters. Spaces in a text document saved under BSD will look identical when opened up in ChromeOS. Consequently, code committed under one operating system will have identical-looking indentations (if spaces are used) when opened under another operating system. There’s a lot more on this topic involving how the tab character actually behaves at the bit level; it revolves around the same idea of spaces being interpreted the same way across platforms.
Most modern IDEs can be configured to automatically insert spaces when TAB is hit. IDEs (integrated development environments, more or less the word processors for folks who write code) have a ton of options when it comes to customizing one’s interface with the lines of code to write, delete, modify, and otherwise warp. One of those options is typically whether or not to use tabs; the key can be tweaked so each tap of the tab button inserts a bunch of spaces, preserving the cross-platform consistency while still allowing the developer to only click one key (instead of hitting the spacebar, say, 4 times). Likewise, if this setting is enabled, the IDE will be smart enough to know that when the editor hits the backspace key, they want to delete the entire “tab”, and it will automatically remove the spaces it inserted.
Jeff Atwood believes “only a moron would use tabs to format their code.” Ok fine, so the article is mainly a plea for consistency in how whitespace is formatted, lest the team devolve into passive-aggressive backstabbing. But at the very end he does quip that tabs are inferior, and I’m inclined to go with the guy who built StackExchange from the ground up.
Arguments for tabs
Tab width can be resized on-the-fly. Again, most modern IDEs allow their users to dynamically configure the width of tabs, instantly resizing packages of code to conform with the new tab width. If some of the code uses spaces for indentation instead of actual tabs, they can’t be resized in this fashion; the spaces would have to be manually deleted and tabs inserted (hence Jeff Atwood’s plea for consistency; tabs and spaces may be two warring camps, but the absolute worst thing one could do is try to combine the two). This allows individual users to exercise personal preferences without interfering with anyone else: if Timmy Torture likes his tab width to be 2, and Sammy Suicide prefers a width of 4, they can both have their cake and eat it too provided tabs are the de facto formatting standard for the codebase they share.
Honestly, that’s all I can think of. If you have another argument for why tabs should be used instead of spaces, please let me know.
Yeah, it’s pretty obvious which I think is superior. But this comes after using tabs for years without thinking about it, and running into all the interoperability issues I mentioned. I can’t tell you how often I opened up modules that I wrote, on different operating systems, and was greeted with indentations that went about halfway across the window. Sure, I could reset tab width, but given how smart IDEs are, why not just make it look nice on every single operating system while still being able to use the TAB button? I have to hard-set the width, but honestly who cares? 4 spaces is good enough for everyone.
Unless, of course, you’re programming in whitespace.