24

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.

sotangochips
  • 2,700
  • 6
  • 28
  • 38
  • 3
    In the absense of someone linking to an actual scientific study proving something conclusively (which I would welcome, but I'm not sure it exists), I vote to close this as 'subjective and argumentative'. – thomasrutter May 01 '09 at 01:46
  • 3
    Oh and I love a tab size of 2 ;) – thomasrutter May 01 '09 at 01:48
  • To be fair, many programming questions don't have exact answers. I'm looking for a best practice. You know? I'm not hoping people respond with their favorite, I'm hoping someone can provide some rationale. In fact, Christian's answer is pretty good. I'm just looking for a standard going forward. Perhaps the answer is "It's always subjective" You should answer that way and people should vote it up if that's correct. – sotangochips May 01 '09 at 01:58
  • 14
    Since PEP 8 says 4 spaces, what's the question? – S.Lott May 01 '09 at 02:09
  • This is a question about indentation width, not tab size (which usually means the width with which a literal tab character is displayed). – ShreevatsaR Jun 05 '15 at 22:47

13 Answers13

44

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
esm
  • 1,730
  • 15
  • 10
14

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,368
  • 2
  • 16
  • 22
13
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
5

This discussion often involves misunderstandings, because (as jwz describes) it usually involves three distinct 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, uses this key for a context-sensitive fix-my-indentation command.

  • 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
  • 30,281
  • 14
  • 77
  • 110
4

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
  • 1,292
  • 7
  • 8
3

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
  • 5,695
  • 3
  • 25
  • 23
2

No one have mentioned this so far so I feel I am obligated to post one. The choice of indent size (which I think what OP meant), affects not just how codes are indented but also affects how much code you can fit in a line and how they align.

A development team needs to eventually come to some sort of agreement on the length of a line. I started off with 80 columns and to this day I still stick to 80 columns. AFAIK, stackoverflow uses 80 columns in source code markdown as well.

When you use an indent level of 8, with a typical function that is nested 3 levels deep, your code will start at column 24. That lefts me with just 56 characters to write a line of code.

Here is what some code in VLC looks like with indent=4:

            msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
            free( mrl );

            /* send message and get a handle for a reply */
            DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
                                                                            &err );
            dbus_message_unref( msg );

Here is what it looks like with indent=8

                        msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
                        free( mrl );


                        /* send message and get a handle for a reply */
                        DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
                                                                                        &err );
                        dbus_message_unref( msg );

While large indents make code easier to read, it also gives you less room to write nested code before they wrap around.

It is really important to keep tab size at 8. tab != indent. While it is temping to make hard tab as indents, it also have very bad consequences. A lot of people also like to align their code. So code like above will look like this with tab = 4:

            msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
            free( mrl );


            /* send message and get a handle for a reply */
            DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
                                        &err );
            dbus_message_unref( msg );                                                  

You will see that the line &err no longer aligns with conn above. The situation gets even worse when multiple comments are added in the end of each line.

some user
  • 876
  • 1
  • 12
  • 26
2

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
  • 41,644
  • 8
  • 32
  • 24
2

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.

0

I've always used one tab as two spaces.

Rook
  • 60,248
  • 49
  • 165
  • 242
0

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
  • 3,997
  • 4
  • 30
  • 36
0

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,238
  • 3
  • 12
  • 23
-3

I suppose 4 tab space makes the code far more readable... atleast as far as i have worked on my projects tab space of 4 has been the most comfortable option....