views:

694

answers:

14

Personal preferences aside, is there an optimal tab size (2 spaces? 3 spaces? 8 spaces?) for code readability? In the different projects I've worked on, people seem to have vastly different standards. I can't seem to read 2 space indents, but companies like Google use it as a standard.

Can anyone point to documentation, studies, or well-reasoned arguments for the optimal size of a tab?

If we want to get specific, I work mostly in python. The goal of this question is to pick a standard for the team I work on.

+9  A: 

The optimal tab size is "one tab". This way everybody can configure their editor to display it with as many spaces as they want.

sth
Just for the point of correctness, a "tab size" cannot be measured in "tabs".
ldigas
Hmm... that doesn't get at the root of the readability problem. Also, there's a chance some will work in soft tabs and some will work in hard tabs, thus using a single tab may produce different results which is not a great outcome.
sotangochips
This doesn't actually work in practice because you also need spaces to align various things that don't line up with the left margin of your code, and since you (usually) can't see the difference between tabs and spaces, they get intermixed and the resulting code becomes an indent minefield.
Greg Hewgill
Well, one size doesn't fit all. You won't solve the basic problem to find a tab size that everybody likes. So the best option is to let everybody decide on their own, and just establish some rules for the corner cases. Best don't add any special rules that depend on a specific tab size.
sth
pep8 recommends spaces over tabs..
John Fouhy
I'll agree with this in theory; this is exactly what tabs are designed for, and formatting with spaces is the opposite. In practice, especially in Python, life isn't always so simple though.
Kylotan
@Greg Hewgill: Always use tabs for indentations at the start of a line. You will not need extra spaces. I don't know, which 'various things' need to be aligned with spaces.
Mnementh
@Mnementh: For example, if you have a function call with a lot of long parameters you often have to break it across lines, using whitespace to align it for readability. Tabs shouldn't be used for this as the alignment will break depending on the tab size used. The proper method is to uses tabs to indent the line to match the one above and then uses spaces to make things pretty.@Greg Hewgill: Are there really decent editors out there that don't visualise tabs and spaces differently? (This is a real question, I assumed all programming editors supported this.)
Parker
+2  A: 

I don't know of any studies that would answer your question. I don't think there is a way for this to be non-subjective but my personal preference is 4 spaces.

dagorym
+2  A: 

I've always used one tab as two spaces.

ldigas
+1 My preference as well! :)
w3d
+6  A: 

I like 8 spaces (I know, right?). It makes the start/ end of blocks really obvious.

As to your question, a formal usability study would be required. Let's look at limits though:

0 spaces

function test(){
var x = 1;
for (i=0; i<=5; i++){
doSomething();
}
}

No indentation is obviously bad. You can't tell where anything begins or ends.

19 Spaces

function test(){
                   var x = 1;
                   for (i=0; i<=5; i++){
                                      doSomething();
                   }
}

Loads of indentation is obviously bad too, because you can't visually link code to its parent function or loop (or what have you) as your peripheral vision doesn't extend that far. Your eyes have to flick too far back and forth to facilitate reading.

8 spaces

function test(){
        var x = 1;
        for (i=0; i<=5; i++){
                doSomething();
        }
}

I think I decided on 8 spaces because the word 'function' is 8 characters long. But it just seems so useful for readability. All the code is in my peripheral vision, and there's no way I can miss the start of a new block of code if I'm quickly scanning.

ChristianLinnell
+1 for actually describing why you choose what you choose.
jjnguy
+18  A: 

Four spaces and no hard tabs, if you're a Pythonista.

esm
A: 

I read that 2 spaces is actually optimal, based on a study where programmers were asked to estimate the level of nesting based on indentation, but that when asked, programmers thought 4 would be optimal. Citation needed, but can't find it.

Kai
A: 

In the past I used 3 spaces. And that's still my preference. But 4 spaces seems to be the standard in the VB world. So I have switched to 4 to be in line with most code examples I see, and with the rest of my team.

Mike Lewis
+3  A: 

This discussion often involves misunderstandings, because (as jwz describes) it usually involves three different issues:

  • What happens when I press the <Tab> key in my text editor?

  • What happens when I request my editor to indent one or more lines?

  • What happens when I view a file containing U+0009 HORIZONTAL TAB characters?

My answers:

  • Pressing the <Tab> key should indent the current line (or selected lines) one additional level.

    As a secondary alternative, I can also tolerate an editor that, like Emacs, applies a context-sensitive fix-my-indentation command when I press <Tab>.

  • Indenting one or more lines should follow the reigning convention, if consensus is sufficiently strong; otherwise, I greatly prefer 4-space indentation at each level.

  • U+0009 characters should shift subsequent characters to the next tab stop. Tab stops begin at column 1 and are 8 columns apart, no exceptions.

bignose
+1  A: 

Death to the space infidels!

bignose
tabs and spaces intermixed. How can you go wrong?
Joe Philllips
Equally heretical to everybody. Win!
bignose
A: 

I think I recall that there is a section about indentation in Code Complete, quoting some studies about which level of identation makes the code most readable, but I do not have a copy of it with me right now, so I can't check it.

erik
+1  A: 

Since you're using Python, you could, as said before, take python's style guide (PEP 8) advice:

Indentation

Use 4 spaces per indentation level.

But the Linux kernel CodingStyle says diferent:

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3. Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.

This document also has some examples of how code should look like, and how identation changes that (it's in C, though)

Flávio Amieiro
A: 

The argument for tab over spaces is that it allows each person to customize their editor to see whatever level of indentation they want. The argument against tabs is that it's difficult to spot (for the writer) when they've mixed tabs and spaces. Occasionally you will want lines that aren't indented to a tab stop, which causes mixed tabs/spaces.

Using 2 spaces has these advantages: it's possible to have more nested blocks (this is important if you also have a line limit), and that using a double indent (ie 4 spaces) is nicely readable way of wrapping long lines. A disadvantage is that it's hard to judge sometimes whether two lines are at the same indent.

Using 8 spaces has the opposite advantages and disadvantages to 2 spaces. It's easy to judge indent level, but you deep nesting becomes hard to manage. Many people would judge the latter disadvantage to be an advantage (because it makes deep nesting less desirable).

4 spaces is somewhere between these two extremes.

But my personal belief is that it makes no difference what level of indentation you use. The most important thing is to pick some standard and stick to it. As others have said, follow PEP8 if you're writing python, follow Sun's java style guide if you're writing java, and if you're doing linux kernel hacking follow their style guide. Even if there were some small advantage in using one over the other, it's a waste of energy debating which to pick. Make a decision, and move on to the interesting part of software engineering.

Paul Hankin
+2  A: 
2 space 4 busy coder
3 space for heavy if statement using script kiddies 
4 space for those who make real money pressing space 4 times
8 space for the man in ties and suit who doesn't need to code