15

Maybe I'm alone in this, but few things annoy me like people indenting using spaces rather than tabs. How is typing SpaceSpaceSpaceSpace easier and more intuitive than typing Tab? Sure, tab width is variable, but it's much more indicative of indentation space than spaces. The same thing goes for backspacing; backspace once or four times?

Why do languages like Python recommend using spaces over tabs?

Naftuli Kay
  • 1,621

5 Answers5

16

Consistency, primarily.

Also, something occasionally comes along that actually uses the whitespace - like Python: Tell me, what's going to happen if I run the following code snippet?

def foo():
    print "a"
    print "b"

Answer: You'll get an IndentationError, because those two print statements aren't indented at the same level - and so not part of the same code block. (The first one uses a tab, the second all spaces)

Then there's the frustrating cases when one developer's editor has tabs set to 8 spaces, and another has them set to 4 and someone's using 5 for some odd reason... It may look perfectly normal on one workstation, but then when it's checked in to SVN and someone else updates, they'll see a horrid, horrid mess.

So those are two really good reasons to always be consistent, whether it be spaces or tabs.

But, spaces allow for far more control over the indentation than tabs, and don't require any special configuration in the editors for it to work. (Although it can be made easier - for example, in vim, just use set expandtab to insert spaces whenever you hit tab)

EDIT: And amusingly enough, the site seems to have normalized my tabs into spaces so that the browser can display it correctly. Click on "edit" to view the original, tabs included ;)

Izkata
  • 6,118
  • 7
  • 30
  • 44
12

This is a good discussion about indentation and whitespace in Python; from the article:

[I]t can be a good idea to avoid tabs alltogether, because the semantics of tabs are not very well-defined in the computer world, and they can be displayed completely differently on different types of systems and editors. Also, tabs often get destroyed or wrongly converted during copy & paste operations, or when a piece of source code is inserted into a web page or other kind of markup code.

As to your argument about pressing the spacebar or backspace key 2 or more times, since most source code editors will insert a configurable number of spaces with a single press of the tab key, and similarly un-indent, there are no more keys pressed when using spaces for indentation.

For myself, I prefer spaces because the code is always displayed with the same amount of indentation, whether I'm viewing it in an IDE, or less, or Notepad; i.e. spaces are more portable.

3

In Python indentation controls the program flow and so is vital.
If you take code formatted with tabs and copy it so that the tabs are changed or lost the structure of the code is destroyed. Spaces are always spaces = much safer.

If the wear and tear on your space bar worries you, your editor can probably be set to convert tabs to spaces automatically.

3

Spaces should always be used, because tabs alone aren't sufficiently flexible for many styles, and intermixing tabs and spaces (nearly) always produces an absolute mess.

For an example of one style that generally needs spaces, consider something like:

call_some_function(parameter1,
                   parameter2,
                   parameter3,
                   parameter4,
                   parameter5,
                   parameter6,
                   parameter7);

Unless you're willing to re-name all your functions to be an exact multiple of the tab size (minus one for the parenthesis) tabs alone simply won't do this.

As to mixing tabs and spaces, you almost immediately run into a serious problem: tabs are not consistently expanded the same way. Some software treats a tab as equivalent to a specific number of spaces. Other software will expand a tab modulo a specific number of spaces -- e.g., an item after a tab will always start at a column number that's a multiple of (say) 8.

Even if you can ensure against spaces getting mixed with your tabs, you still have a problem: tabs also play poorly with variable-width fonts. This problem arises when (for example) you want aligned trailing comments:

a.m = 9;   // this is the slope
a.i = 4;   // this is the intensity
a.x = 1;   // this is the x-intercept

As they stand right now, those all line up perfectly. Viewed with a variable width font, however, things get ugly. With spaces, the comments may (often will) get slightly mis-aligned. With tabs, however, the misalignment often becomes quite radical:

a.m = 9;          // this is the slope
a.i = 4;  // this is the intensity
a.x = 1;          // this is the x-intercept

Suddenly the small difference in width between the 'i' and the 'm' or 'x' in our variable-width font has been magnified to an entire tab stop.

The bottom line is that almost any change in how you view code with tabs, no matter how seemingly trivial, can and usually will produce an unreadable mess.

To answer your other questions: other have already pointed it out, but I can't imagine anybody in a programming editor (or much of anything else) actually using the space bar to insert the spaces, so your question about: "typing spacespacespacespace" is irrelevant because nobody does that to anyway. Likewise with back-spacing: it's hard to imagine an editor that would require pressing BkSpc four times to go to a previous tab stop, so (again) the question is irrelevant.

Bottom line: tabs are fine if you (and only you) will ever look at your code, and you only ever do so with a single editor that you never reconfigure (at all!) Those conditions, however, are so close to impossible to enforce that there's only one reasonable answer: never use tabs at all.

Jerry Coffin
  • 44,795
2

The big problem is the inconsistency of the "tab" width sometimes they are rendered as four spaces sometimes eight. In many editors you can set them to be anything from 1 to 9 spaces.

So this turns a simple WYSWYG editor into a What You See is What someone else MIGHT Get.

Its a particular problem for Python, but its also a problem in any of the "curly brackets" languages as indentation is used to convey meaning to human readers and messed up tabs make the code difficult to read.