Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
18  
You can configure most editors to insert spaces instead of tabs when you hit tab. If you can't configure that in your favourite editor, you need a better editor. –  Anna Lear Nov 22 '11 at 21:49
    
one thing, many editor can be setup to insert 4-8 spaces when 'tab' is pressed, so it is just one key press just like tab. –  Echo Nov 22 '11 at 21:49
2  
It still doesn't answer my question. Why spaces instead of tabs? Sure, you can configure your editor to insert spaces instead of tabs, but you could also presumably insert tabs with spaces. The question is why would you want to do this? What possible benefit is derived? –  Naftuli Tzvi Kay Nov 22 '11 at 21:52
4  
@Tk Spaces are unambiguous tabs can be changed –  Martin Beckett Nov 22 '11 at 21:58
3  
@TKKocheran, because tabs do funky things to your output when you accidentally insert them into a string. –  zzzzBov Nov 22 '11 at 22:16
show 3 more comments

5 Answers 5

up vote 12 down vote accepted

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 ;)

share|improve this answer
2  
Your problems with tabs (especially, the horrid SVN mess) only come about if you use the tabs-to-spaces option in your editor. If you and your team agree to disable the tabs-to-space option, then a tab will always be a tab in any view: then it's just a choice of how wide you want it to display in your editor. –  HorusKol Nov 22 '11 at 22:40
    
@HorusKol Nope, the horrid mess is because spaces and tabs got mixed. I guess I didn't make that obvious enough (see the lone sentence after the "horrid, horrid mess" line). So the spaces were made to align nicely with the tabs when they're set to be 4-space equivalent, but then when someone else views it (whether or not tabs-to-spaces is set), they get a mess. –  Izkata Nov 22 '11 at 22:56
4  
I see - well, you need to set a guideline for your team - either consistently use spaces OR tabs... either way, tabs-to-spaces is a bad idea all round ;) –  HorusKol Nov 22 '11 at 22:58
16  
This is why I consider the idea of whitespace-meaningful languages to be a design flaw. Things you can't see should never break your code. –  Paul Nathan Nov 23 '11 at 0:04
    
Just for the record, I agree with @PaulNathan –  Izkata Nov 23 '11 at 0:19
show 1 more comment

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.

share|improve this answer
1  
re: "whether I'm viewing it in an IDE, or less, or Notepad; i.e. spaces are more portable" - this is the best argument for spaces I've ever heard. I'm a "tabs" guy myself, but have occasionally disliked seeing tab-spaced code rendered in unexpected ways. –  Aerik Nov 22 '11 at 23:28
add comment

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.

share|improve this answer
    
I'm more concerned with the wasted time spent typing space or backspace four times versus one keystroke to do both. –  Naftuli Tzvi Kay Nov 22 '11 at 21:56
    
Yeah, but Python is weird that way. I've never come across anything else that cares about spaces. –  Aerik Nov 22 '11 at 23:29
    
@TKKocheran Oh, that's your real concern? I don't know how they did it, but vim at my work is set up so that backspace goes back to the last tabstop, over multiple spaces, completely transparently –  Izkata Nov 23 '11 at 0:26
    
Could you dump .vimrc for me :) –  Naftuli Tzvi Kay Nov 23 '11 at 0:27
1  
@TKKocheran Found it - it's just set softtabstop=4, and both TAB and BACKSPACE will use 4 spaces as a tab. (Well, to each tabstop, treating the width as 4 spaces) –  Izkata Dec 12 '11 at 22:25
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
1  
I see you say "proportional fonts break both spaces and tabs but they break tabs worse, so use spaces". Why not use elastic tabstops? nickgravgaard.com/elastictabstops –  sparkleshy Nov 23 '11 at 1:40
    
@sparkleshy: It's a nice idea, but makes no difference to real code, and won't until all (or at least the vast majority of) real editors already include it. –  Jerry Coffin Nov 23 '11 at 1:49
    
...I know :'( - well, kinda. You can still use an editor that's backed by spaces (like the tab key should be if you're using spaces) but pretends they're elastic tabstops; it makes managing the spaces easier. –  sparkleshy Nov 23 '11 at 1:54
1  
If you're programming in proportional width fonts, well, you've got a lot more problems than just tabs to worry about... –  Brian Knoblauch Nov 23 '11 at 13:32
    
@BrianKnoblauch: I agree that they're not...ideal, but it's almost inevitable that some people are going to look at the code that way at times, and it's better if that doesn't lead to it being completely trashed. –  Jerry Coffin Nov 23 '11 at 15:10
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.