-
Notifications
You must be signed in to change notification settings - Fork 54
Tabs & Alignment
I’ve made it pretty clear that I’m not a fan of tabs. Code Alignment has support for tabs because people do request it. The support comes in two forms.
-
If the document has tabs in it, when figuring out the position to align character to it will treat a tab as the amount of characters it represents instead of just a single character.
-
If you tick “Use IDE tab settings for alignments” then tabs will be inserted instead of spaces and spaces will be used to finish the alignment.
Many people think my opposition to tabs in Code alignment is purely from a personal preference in the “Tabs vs Spaces” debate. It’s not. The simple truth is there is a fundamental conflict between the purpose of tabs and the purpose of code alignment.
Let’s start by asking the simplest question - What’s the point of tabs? Developers have different preferences. And one of them has always been how big your indentation should be. Four, eight and sometimes two are common and are often what programs will default to. The tab doesn’t have a size, so by using a tab each developer can display the code how they want. This isn’t a bad idea at all.
But how does this conflict with Code alignment? Well, code alignment is about saying, “This is how the code should look.”
Let’s say there are two developers sharing source code. Both use tabs. One has their tabs at 2 characters, the other at 8.
The first developer checks in code aligned with tabs at two characters long.

And then the second developer reads it…

The 2 to 8 character difference makes it more extreme, but this problem will always happen if you align with tabs.
-
No, you don’t. It just takes one developer to come on board and insist on using a different size tab because they think it shouldn’t matter because that’s the whole point of tabs.
-
Then why bother? If tabs are always 4 characters long why not use four spaces and make sure it is always viewed properly?
If you use tabs you have to go to the effort on every editor you use to make sure the tab size is the same. And in some editors the option is not offered.
Want to read it in Notepad? A tab is 8 characters. Do you use 4 characters? Well your file will look awful.
Do you use GitHub? The Viewer on the website only displays tabs as 8 characters. If you use a different size than your alignments will be broken.
Do you think it makes navigation faster? A series of tabs must be faster to move through than 4 times as many spaces, right? Except that IDEs offer great navigation shortcuts. Ctrl left, right is standard across Windows.
Do you use tabs to make your files smaller? Your Hard Drive is measured in GB or even TB, the difference in tabs vs spaces is measured in bytes. But it’s a Javascript file and you need to send it across the web? Then use a minifier, they’ll do a much, much better job than using tabs instead of spaces ever will.
Actually yes. In theory, tabs used properly is consistent with code alignment. This might seem contrary to my initial statements so let me clarify. Tabs are made for indentation. After the indentation, it just makes no sense to use tabs.
So if you only use tabs for indentation, and you align on the same indentation level, code alignment works perfectly…


Because in practice it’s just impractical. IDEs don’t have a nice option to switch between tabs used indentation and pressing the tab key after indentation (Although it really isn’t that hard to implement). And there are some cases where people confuse what’s an indentation and what’s an alignment…

In this case the indentation isn’t simply the space at the start of the line. It’s the same indentation for all lines. And the spaces after the tabs are the alignment.
This is a lot of effort to go to, that you need everyone to do, and one person not doing it properly can screw it all up. It’s just so much easier to use spaces.
Does your team mind if you put a space instead of a tab before an equals? Would they really be bothered by two? When team rules say, “Use tabs” they’re talking about indentation.
The most important thing is that you're consistent. Advantages of different sizes barely go past personal preference. That said, I would recommend 4. Why?
-
It's the most commonly used size already, so least chance of annoying someone.
-
I would guess 99% of developers use a 2, 4 or 8 indent. 4 is the middle number. Someone who normally uses 8 is going to be much more comfortable switching to 4 than 2. And same with people who normally use 2.
But again it's not worth fighting over, if the majority of your team wants to standardize on something else, just use that.
The idea of tabs isn’t a bad thing, but in reality, the gain they give is pretty minor – It really isn’t that big a deal having to switch from 8 character indenting to 4 character indenting. But to use them right, so that you can still use code alignment properly is a huge pain.