A more comprehensive comparison of indenting code

Yes, this is going to be about tabs vs spaces. Wait, don’t leave! I know it’s a rather silly topic to discuss, but please, hear me out. Arguments online tend to be shallow and one sided, but I’m hoping that this post will be different.

The right answer

Ask an experienced programmer to choose between tabs and spaces, and they will tell you that it doesn’t matter which one you choose, just be consistent about it. And they’re right.

Whichever side you are on, your code will work the same. And as long as you don’t mix different styles in the same project, things will stay predictable and properly aligned.

In fact, nowadays it’s (thankfully) becoming more and more common to have some tool, such as gofmt, automatically take care of it along with many other less important formatting issues.

With that said, let’s proceed to the arguments.

Tabs: it’s just one key to press

Being just one character gives tabs two advantages: just one key to press and faster navigation. In practice, these can apply to spaces as well.

If you’re programming anything serious, you’re probably using an editor meant for programming. When starting a new line, most of these editors will follow the indentation level of the prervious line, saving you from the trouble of reindenting the new line.

These editors can also be easily configured to have the tab key insert spaces instead of the tab character. So just like tabs, in one press of a button, you’ve got all of the spaces you need.

Some might argue, that it’s still much faster to dedent code and move around with tabs. But editors already have better shortcuts for these operations like shift+tab to dedent one or more lines, or ctrl+<arrow key> to jump around words instead of characters. Depending on your editor of choice, there may be a heck lot more useful shortcuts available (users of vim know what I’m talking about).

So it looks like as long as you are using a decent editor, these advantages no longer apply. It’s quite uncommon to type code outside of text editors, but if you are, it’s usually so short that it doesn’t matter anyways. If it does matter, I suggest you type it out in an editor first, and then just copy-paste it.

One issue with the tab key though, is that some applications use it for switching focus between interactable elements in the GUI, which is awfully convenient for power users. In those scenarios, you might as well use spaces.

Tabs: it’s meant for indenting

Some think that tabs were meant for indenting, and therefore would be the logical choice. But that is incorrect. Tabs were meant for tabular data (as in tables), hence the name “tabs”.

This explains why the width of the tab character changes whenever it comes after other characters. Its width shrinks to a size that will make itself align with the tabs on the other lines. This is super handy for neatly making columns. This may also explain why the tab’s standard default size is 8 characters long, which is a bit large.

Tabs: it’s configurable

With tabs, any decent editor will provide you with the option to set the size to match your taste. Unlike spaces, there’s no need to argue about which size to follow. Everyone gets to decide, and so everyone wins, right?

Well, not when you need some sort of text alignment. Suddenly the size of your tab matters, otherwise things that should’ve aligned won’t. This is why some people use tabs for indentation, but spaces for alignment. Perhaps editors in the future will natively support elastic tabstops, or we’ll move on to structured editors, but I doubt it.

Personally, I don’t find alignments to be worth the trouble (except for this one time). Sometimes I just format the code differently and get things to align just via indentation instead. My use of proportional fonts may have affected this decision.

Spaces: it’s the same everywhere

With spaces, no matter what application you use, it’ll look the same everywhere. This makes it impossible for weird alignment issues to occur without having to resort to an impure mix of tabs and spaces.

Tabs may configurable in a decent editor, but that won’t be the case in many other applications like the terminal or in a web browser. Many applications are stuck with the size of 8, a size most people find to be too long.

This does mean that you might not have your way with the indentation size. Just give it some time, you’ll get used to it, and stop caring. There are much more important matters to worry about.

Spaces: it pays better

Not so long ago, Stack Overflow ran a survey and found out that people using spaces were better paid. Even after factoring a few things out, they still weren’t able to figure out why.

Some people came up with a few possible theories saying less competent confused the tab option as pressing the tab key. Someone even wrote a post detailing some other possible candidates, but nothing is clear cut just yet.

This isn’t really a valid argument though. Switching to spaces won’t magically increase your salary. Still, it was quite an interesting observation.

Deciding between the two

As you can see, both options aren’t so much better than each other. It really is a matter of opinion, and pointless to spend time arguing about. Honestly, I’m not sure if this post was even worth writing.

If you haven’t decided on one, then just follow the preference of your current environment. This may be the most common convention in your company or even your programming language.

By following the same conventions, you lessen the likelihood of having to repeatedly switch between different conventions when sharing (read copy-pasting) code or working on multiple projects.

My personal preference

When I was new to programming, I initially preferred tabs since it seemed like the more logical choice. But as discussed earlier, this isn’t really the case. At some point, I ended up switch to spaces. I can’t recall the exact reasons why though, but it might be because it’s the more common standard followed by most programming languages.

In my day job, we use tabs. I used to hate it, not because it didn’t align (pardon the pun) with my personal preferences, but because it was very inconsistently enforced. Many files in the project would mix tabs and spaces, even on the same line (and not for alignments in between)! Once, we introduced a tool to automatically fix the formatting it for us, it has never been an issue again.

So if you’re still deciding between the two, just flip a coin, use a formatter to keep things consistent, and pick spaces!

Tags: #comparison #programming