Tabs or spaces?

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.

Nyuk nyuk nyuk.

The Debate

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

One of the instances where a “middle ground” is the worst possible idea.

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.


About Shannon Quinn

Oh hai!
This entry was posted in Internet, lolcat, Programming, Technology and tagged , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Tabs or spaces?

  1. Colin says:

    I feel like I’m somehow responsible for this post.

  2. eksith says:

    Jeff is what we’d call a “mid-school” coder. Too young to be called “old-school”, to old for “new-school”. His wisdom, in an odd way, is by virtue of being in between these worlds. The value of being exposed to both is incalculable.

    But I have to say, I haven’t seen this problem that with tabs that much even on different environments.

    “the spaces would have to be manually deleted and tabs inserted”

    There’s a trick in Notepad which I’ve been using to make this consistent.

    It’s usually 4 spaces equalling one tab, so I just replace one set of 4 spaces with a tab. Copy the tab and hit Ctrl + H. Insert 4 spaces in “Find what” and paste the copied tab into “Replace with”. Hit Replace All.

    When I need to submit the code back to an OCD case with a fondness for spaces, I do the reverse. Of course, this is only practical when sending back a few of pages of code. If you’re talking big project, then IDE somersaults are necessary to preserve their brand of holy water.

    But I must say, I’m definitely a tab guy. I can get away with it because, as Jeff says, “…The only programming project with no disagreement whatsoever on code formatting is the one you work on alone”. If someone submits code to me and I’m in a group project, I always return in the same format I’ve been given. If we find the majority prefer spaces or if it’s the project policy, then we switch to that.

    Many times, I find people gravitating around a formatting standard already set by a popular existing project, if it makes sense. Say, the WordPress standard for example.

    Side note: I’m now turned on to Sublime for a lot of side projects thanks to you mentioning it… Best recommendation ever! 😀

    As for the font of knowlege, I’d go with Futura. It’s the same font on the moon plaques! 😉

    • magsol says:

      I don’t have any problems conforming to a specification either–like you and Jeff said, by far the least of the possible evils is to be using spaces when you’d rather be using tabs, or vice versa–but I have definitely gravitated towards spaces. Granted, 99% of the time, I’m coding for nobody but myself, which makes such standards enforcement all but moot.

      Though I should probably refresh my memory as to what Apache Mahout’s programming practices are, since I’m supposed to be committing stuff over there… o_O

      Tabs definitely seems to be the preferred method, though, and it comes back to the same reason: you can scale the tabs to be whatever width you want on your personal machine without it affecting anyone else, while still maintaining uniformity in coding style. Obviously this can’t be done with spaces. It’s a powerful argument, I admit.

      ISN’T SUBLIME AWESOME?? I still haven’t purchased a license, but I’m absolutely going to do that soon. I am more than happy to throw some of my money at them, given how spectacular their product is and how helpful it’s been to my own work.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s