Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal tab size for code readability [closed]

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.

like image 734
sotangochips Avatar asked May 01 '09 01:05

sotangochips


People also ask

What is the ideal tab size?

The default value for the tab-size property is 8 space characters, and it can accept any positive integer value.

Should a tab be 2 or 4 spaces?

If you represent the indents using space characters (ASCII SP), then 2 spaces is 2 characters fewer than 4 spaces. If you allow TAB characters to be used, then (on Windows) a TAB indents by up to 4 spaces, so TAB characters result in fewer characters.

Is a tab 4 or 8 spaces?

Many editors have an option to insert spaces when using the tab key. You should always have that turned on. Sun sez: each indent should be four spaces from the last level, and tabs should be eight spaces.

How many spaces should a tab be programming?

Coding software such as Sublime Text and PyCharm set the default tab width to 4 spaces and provide the opportunity for the user to convert those tab characters to spaces.


2 Answers

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

like image 73
esm Avatar answered Sep 23 '22 01:09

esm


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.

like image 36
ChristianLinnell Avatar answered Sep 23 '22 01:09

ChristianLinnell