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.
The default value for the tab-size property is 8 space characters, and it can accept any positive integer value.
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.
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.
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.
Four spaces and no hard tabs, if you're a Pythonista.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With