Tabs vs. Spaces

2024-01-26

Tabs vs. Spaces. A veritable holy war amongst text file authors and enthusiasts. And I'm here to stir the pot! Never heard of this holy war? Here's a clip from HBO's show Silicon Valley.

Tabs and Spaces are both used to indent code. So which one should you use? Here's my hot take: Spaces (usually). But why does it matter so much?

Tabs vs. Spaces is one cause of the dreaded Wall of Pink (another being Line Endings). What's the wall of pink? It's when you try to compare two text files with some sort of diffing tool, and your diffing tool tells you that the files are two totally different files because every line is different whereas in reality they're basically the same file with only a few minor differences. How does this happen? One programmer wrote a file and used either tabs or spaces, and another programmer opened the same file and made a little change but their editor auto-replaced all tabs/spaces in the entire file with the opposite.

Another problem is portability, and this is where tabs are problematic and spaces are your solution. When using tabs it is entirely possible that a given file (or console output) might appear totally different between different systems. If you're using tabs for indentation or alignment purposes, your carefully crafted alignment won't look the same across all systems. If clean indentation and alignment is your goal, spaces will get you that every time.

Why don't tabs looks the same across systems? Well, some systems will interpret a tab as being equivalent to the same number of spaces each time it is encountered. (4 is a common number, but not universal). So when printing to the screen, the tab character results in advancing the cursor the same amount of space each time. Other systems interpret tab as being an instruction to advance the cursor to the next tab stop. Remember, ASCII was meant to emulate physical type writers, which wrote on physical pieces of paper! A piece of paper can be divided into a physically measured grid. If your paper is 8 1/2 inches wide, your paper can be divided into half-inch tab-stop segments, meaning that tab will advance the cursor to the next closest half-inch measurement. That might be 1 space forward or it might be more! On electronic systems, this might mean "advance to the next column divisible by 8" or some other number!

Speaking of typewriters, the tab key on a type writer was a key that would let the carriage swing until it hit an obstruction. This obstruction was a tab stop, which often could be configured by the typist. This was very useful on typewriters. The very nature of being configurable on a physical typewriter meant that there was no universally agreed upon distance for what a tab stop should be. The ASCII specification has a clear definition for what a space it, but it does not have a clear definition for what a tab is.

Douglas Crockford (Who's kind of a big deal) seems to agree with me on this. Okay...okay.... I agree with him. His thinking has influenced mine.

"But wait..." I hear you say, I'm writing code that's just for me and maybe my team, we all use similar systems and we're not printing our code. Can't we all just use tabs because it's faster and easier than typing all those spaces? Well, you could. It's probably not a big deal at the end of the day. But I would argue that any good text editor can be configured such that pressing the tab key on the keyboard will be interpreted by your editor as advancing to the next "tab stop" measured in a number of spaces of your choice (mine is 4). You get the ease of using the tab key and the benefit of spaces while avoiding portability issues.

Are you still not convinced? Well, then we might have to just agree to disagree. But let me tell you this - surveys suggest that programmers who use spaces over tabs, on average, make more money! Now I'm not saying that if you ignored everything else that I've said and simply switch to spaces tomorrow you'll get that raise you've been after. Correlation is not causation, but it does wiggle its eyes suggestively and say "look over here." So what's going on? It's difficult to say with certainty, but I postulate that those who use spaces over tabs deliberately are those who also think deeply about systems and interoperability, which in turn results in higher project success, which translates to higher salaries.

So let's hear it for the spaces, and may the flame wars begin!