How does it work?

You may have heard that mixing tabs and spaces is a very bad idea, and are already questioning the sanity of this idea. Let's frame the issue a little bit.

The problem with most of what you have heard about code formatting is that people talk about the false tabs vs spaces dichotomy. A lot. The issue as framed as though it were about indenting when it is really about formatting. If you mix tabs and spaces in your indenting you will have one of the worst experiences possible and will want to pull out all your hair.

One argument you may hear the spaces camp use is that using only tabs for formatting will cause all your code to be out of alignment when viewed at a different tab width. This is true only if you used tab characters for alignment, which is a terrible idea.

TL;DR

Use tabs at the start of a line to set the indentation level. Use spaces to do mid-line alignment.

What are the advantages of using tab characters for indentation?

This is now specifically about indentation. When you insert a series of tab characters at the start of a line of text, you are telling the computer at what indentation level that line is. You are not "compressing a certain number of spaces" and you are not doing anything visual. What you see is what you mean. You mean "this line is N levels deep".

As usual, when you give the computer more metadata, it can do more with the more information. Code indented in this fashion can be displayed with any size of indentation the user wants. A user with a smaller terminal is not bound by the formatting decisions made by a user with a larger terminal. Lines can also be usefully grouped by the computer as "at the same level", which can be used to provide useful visual or navigational aids.

What are the advantages of using space characters for aligment?

In case you have not seen it done, alignment is the practise of making subsections of sequences of lines visually "line up". This may be used when manually wrapping long lines, or when a sequence of lines contains a repeated related part (such as a list of initializations). It looks like the following:


int function_name_here(int one, int two, char* three_is_cool,
                       int another) {
	int hello   = two   + another;
	int goodbye = hello + one;
	return one + two;
}

When you insert a space character into a line of text, you are telling the computer to skip one character when printing. It is very much a visual thing. What you see is what you get. If the code is displayed with a different tab width setting, it won't matter, the code will still line up. The indentation may change in visual size, but the alignment will remain.

Can I programatically convert from what I use now?

Unfortunately, probably not. One of the huge benefits of this system is that because the computer can tell what everything means, it is very easy to convert to other systems. The reverse is not true. Imagine the example aligned code above was made with all spaces. How would the computer know that the spaces at the front of the second line should not become tabs? Imagine that it were made with all tabs. How would the computer know that the tabs at the front of the second line should become spaces?

What does a tab character mean in the middle of a line?

In almost all cases, a tab in the middle of a line is a whitespace error. Most languages have a tab escape sequence like "\t" precisely so that you will never need one.

What about elastic tabstops?

Elastic tabstops is a proposal that involves changing the interpretation of tab characters so one could use them exclusively for formatting. The page propogates the false assumption that there is a dichotomy between using only tabs or only spaces for all lining up of text

Since the days of the character mapped display, programmers have argued over whether tabs or spaces should be used to line up text.

Elastic tabstops have a certain appeal, especially to those who love proportional fonts, but have two main problems:

  1. Elastic tabstops require editor support, and editor support is lacking.
  2. Elastic tabstops cannot adequately represent the code sample above. Under elastic tabstops, there would need to be a tab on the first line after the (, causing unwanted whitespace, and the tab on the second line would be indistinguishable to the algorithm from the third and fourth lines, thus requiring the insertion of a blank line between the second and third lines in order to get the right indent formatting.

What about real/manual tabstops?

Some people have suggested using wordprocessor-style tabstops for code, thus allowing tab characters to be used for all formatting. This could work, but would require the storage format to be other than straight plain text (since you need at least the text and the list of set tabstops).

Is there any tool support for this idea?

Every text editor using a fixed width font supports this idea. That said, there are different techniques such as these ones for vim that can help identify when you've made a mistake.

contact Creative Commons Attribution License