Tabs. Spaces. Indentation. Alignment.

Let's talk about them.

What are they?

Hard tabs are tab characters. Nearly all editors allow configuring how they behave and what their width is.

Soft tabs are space characters. Generally a single soft tab consists of multiple spaces.

Indentation is a way of visually notating scope in your code.

Alignment is a way of making things visually line up and look pretty.

Forget the term "tabstop". It is a historical thing that should not be mentioned when talking about source code. Tabs are not for alignment.

Try to remember, we're talking about source code inside text editors here. We're not talking about ancient terminals or old printers or word processors.

What is LB's opinion?

Soft tabs should not be used.

Tabs are for indentation. Spaces are for alignment. Tabs should never be used for alignment. Spaces should never be used for indentation.

Tabs are for indentation. Spaces are for alignment. Tabs should never be used for alignment. Spaces should never be used for indentation.

Burn it into your neurons.

If you are using spaces to indent things, that's your problem, not mine. If you are using hard tabs to line things up, you are doing it wrong.

Why?

Since any decent text editor allows users to define how hard tabs behave on a per-language basis, they can have any indentation size they want whenever they want. Since tabs are only used for indentation and not alignment, changing the size or semantics of the tab character has no affect on the alignment of the code. Everyone can use different tab sizes and no one would even notice. If you have concerns about changing the tab size, you have already begun to misread this article.

Since one tab is one indentation level, editors are free to have different tab widths for different indentation levels at the wish of the user. With spaces, that information is baked into the source code, and not configurable by the user. If you want people to contribute to your code, you should try to make it accessible.

If you exclusively use spaces, there is no way to tell apart identation from alignment, meaning that should someone choose to switch to tabs for indentation, they'd have to do it manually. Conversely, if you use tabs for indentation and spaces for alignment, it is trivial for someone to switch to exclusively using spaces.

Decent text editors also allow visually differentiating diferent kinds of whitespace, making it easy to tell apart indentation from alignment should it ever come into question.

But what about...

Why do you care? The tab key can be used to both increase and decrease indentation regardless of if it is tabs or spaces.

Because there is already a dedicated key on the keyboard for inserting spaces: the spacebar. The tab key should only ever insert hard tabs - to do anything else would make it some other key, such as "the indentation key". It isn't that though. As for removing indentation, I find the backspace key more intuitive than shift+tab or other concoctions.

Due to legacy, different text editors treat hard tabs different. UNIX text editors prefer hard tab is 8 spaces, Windows text editors and IDEs (Eclipse) prefer that a hard tab is 4 spaces.

If a text editor does not allow you to configure how tabs are treated and what their width is, it is a bad text editor and you should not use it. This isn't the fault of the tab character and you shouldn't base your opinion on historical decisions.

The hard tab length agreement between different text editors cannot be reached / The hard tab length agreement between people cannot be reached

Again: letting the user decide for themselves is a good thing. If you are worried about the length of a tab on someone else's screen, you're a control freak - let people have their own preferences.

Thus, hard tabs may break source code readability and editability if there is more than a single person editing the file. They will open the file in an editor with different tab settings, edit it and next time you open the file it is ruined and all indentations are wrong.

Clearly this person does not understand what "indentation" is - see above for a refresher. If a change in the size of tab characters "breaks" a source file, that means that tabs were being used for alignment, which is wrong.

This is even worse on white space sensitive languages (Python, CoffeeScript) as this might actually cause syntax errors or programming logic errors

Languages should make it hard to do the wrong thing and easy to do the right thing. That includes making it easy to make the code readable and hard to make code look like it does one thing while actually doing another. Languages which require spaces for indentation are asking for trouble unless they also require consistency.

There is no style guide or coding conventions saying that the tab character should [be] the indent. This assumption is easy to make because it allows you to stick your head into a sand, ignore the surrounding world and by singing “let the users pick their own tab width” mantra. However, though a cunning idea, this perceived simplicity causes compatibility and co-operation issues

No style guide? This page is one. You should try to make things so that people's assumptions are correct. Only a malignant person would want to make things so people's assumptions are wrong or dangerous.

There is nothing gained by having “user chooseable indentation width by adjusting tab character width”. The most used indentation width is 4 spaces anyway, so it is extra effort to maintain the freedom to have a user pickable indent width number instead. Try to go to tell some old UNIX admin that they must adjust their editor tab width

Ok, explain why. Prove that there is truly "nothing" gained by this. I have already listed several reasons above that contradict this statement. As for "some old UNIX admin", I am deeply sorry that they have to maintain a legacy system from the 1980s. It's 2015.

If you use any other value for tab width you are breaking this legacy social contract and you are making it for other peoples more difficult to work with your project.

No, legacy is making it more difficult for other people. There is a reason the word "legacy" has a negative connotation.

Tabs cannot be used to format code multi-line functions

Right, because tabs are for indentation and spaces are for alignment. I am not saying that you should use tabs everywhere and never use spaces.

the situation could have been avoided if all text editors would have sticked to soft tabs by default and all text editors would have sticked to a hard tab is 8 spaces by default

History cannot be changed. We live in the present, where hard tabs are commonly used for indentation and not alignment. Sticking to the ways of the past is how you slow innovation and hold back society.

This is Emacs’ default behavior. Well, sort of. By default, the tab size (the distance between tab stops) and the indentation offset (the number of columns an indented line is shifted to the right) have different values. Nothing good can come of this arrangement.

So why are the defaults like that if nothing good can come of them? I see the word "tab stops", your argument instantly breaks down.

the problem occurs with continuation lines. When an expression spans multiple lines, Emacs cleverly lines it up to make it more readable

Again, the default behavior here is wrong. Continuation lines should be aligned with spaces. Tabs should only be for indentation, not alignment.

There is a better way: use tabs for expressing the indentation level and spaces for alignment.

Great! Awesome! Why isn't that the default!? You have basically chosen to agree with me, so why is the title of your page "Tabs Are Evil" instead of "Tabs Should Not Be Used For Alignment"? Because it was shorter?

Of course, any tab scheme means you cannot line up comments across different indentation levels (but this is bad style anyway)

If you consider it bad style, then why are you using it as a defense? I too consider it bad style - I hate having to horizontally scroll to read comments to the right of code like that. But even if they didn't line up, it wouldn't bother me - I'd just wonder why someone tried to line them up in the first place.

Spaces aren’t useful for lining things up, particularly in word processing, or when using proportional fonts.

What does that have to do with writng and reading code? Word processing and coding are two fundamentally different things!

When hacking or reviewing source code, spaces for indentation instead of tabs is more useful when looking through code from different individuals using different editors and indentation styles.

I don't even understand what you mean. Why are spaces more useful? Because different individuals use a different number of spaces? No explanation is given; this sentence is alone and the next paragraph is unrelated.

neither people nor editors can always fully discern what the author intended for indentation. With spaces for indentation, rather than tabs, one can always see what the author intended. Tabs are ambiguous.

How could this even have been written when "There is a better way" was written just a little further up? The entire point of using tabs for indentation is that they are ambiguous. The ambiguity is a good thing.

The second line of the conditional should line up after the open-paren at the beginning of the conditional. That’s not easy to do with only tabs.

You are the only one suggesting to do it with tabs. What you are doing there is called alignment, and thus spaces should be used.

use tabs for expressing the indentation level and use spaces for alignment

There you go again! Why do you keep saying what I am saying if you don't agree with me?

The nutshell of "why soft tabs?" is: it's the only way to guarantee cross-environment rendering will always look the same. It matters particularly when alignment depends on both hard and soft tabs (i.e., spaces), because it will not align as originally intended if the viewer's hard tab width doesn't match the original's hard tab width.

Don't use tabs for alignment. Don't use tabs for alignment. You are saying that tabs are bad for indentation because they are bad for alignment, which makes no sense.

Using hard tabs means you need to know the original's tab setting, and set whatever you're looking at to use it, unless there are never any instances of tab + space(s) indentation

No, it doesn't. The code should look different to suit each person's indentation preferences, but the alignment should always look the same because tabs are not for alignment.

I've used tab characters and spaces here, a tab to indent the code block, but spaces to align the hanging argument declaration. If I change my tab depth to 2, or 8, the alignment will not be thrown out because I used space characters, but where I used a tab character to indent the code block, the indent level will vary to the preference of the reader.

This person has got it right, in my opinion. They even have a screenshot showing what I'm talking about, so if you don't understand, check it out.

Conclusion

From reading many, many discussions and articles on the web, I have come to the conclusion that people don't understand the difference between indentation and alignment. They use this confusion to claim that tabs are bad for indentation, when actually the real problem is misuing tabs for alignment.

Tabs are for indentation. Spaces are for alignment. Tabs should never be used for alignment. Spaces should never be used for indentation.

EDIT

When I went to bed, this article was finished and I checked it once more before turning off my machines - it was complete. The next morning, I checked again and the ending of it had been nuked. Everything after "There you go again! Why do you keep saying" was gone - that was where the file ended. No email from my host, no emails from angry internet people, nothing. So, I had to rewrite the rest of the article, and it's now slightly different. I will be keeping a personal backup this time, since clearly my webhost is incompetent...