Java code conventions says:
Four spaces should be used as the unit of indentation.
But it doesn't have any explanation as to why spaces are preferred. If I type space 4 times instead of pressing tab just once, I have to do 3 key presses more than just one. That means a lot more work, because coders will write a lot of code in one day. So where does this convention come from, and are there any valid reasons to do more work?
According to some research I found, 6 spaces obscures reading of the code and makes it harder to read. Similar is the case with 0 spaces,
4 spaces is said to be optimum for reading clarity and understanding the logical hierarchy of a program at a glance.
Steve McConnell's Code Complete Second Edition chapter on Layout and Style:
Subjects scored 20 to 30 percent higher on a test of comprehension when programs had a two-to-four-spaces indentation scheme than they did when programs had no indentation at all.The same study found that it was important to neither under-emphasize nor over emphasize a program’s logical structure. The lowest comprehension scores were achieved on programs that were not indented at all. The second lowest were achieved on programs that used six-space indentation. The study concluded that two-to-four-space indentation was optimal. Interestingly, many subjects in the experiment felt that the six-space indentation was easier to use than the smaller indentations, even though their scores were lower. That’s probably because six space indentation looks pleasing. But regardless of how pretty it looks, six-space indentation turns out to be less readable. This is an example of a collision be tween aesthetic appeal and readability.
You can change your OS setting to have tab
as 4 spaces. Better still do it for your IDE, instead. You might not like the idea of changing the setting on OS level.
One can argue about it all day long, but in the end it's an arbitrary decision. And having a standard is better than letting the bickering about it continue until the end of times.
And "more typing" is not really a valid argument against it as you can easily set up any sane code editing tool to insert 4 spaces when you press tab.
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