Why are 4 spaces used as a unit of indentation in Java?

Java code conventions says:

Four spaces should be used as the indentation unit.

But he has no explanation as to why spaces are preferable. If I type space 4 times instead of pressing a tab only once, I have to do 3 keystrokes more than one. This means that a lot more work, because coders will write a lot of code in one day. So where does this agreement come from, and are there good reasons to do more work?

+8
java coding-style
source share
4 answers

According to some research that I found, 6 spaces hide code reading and make it difficult to read. Similarly, there are 0 spaces,

4 spaces are considered optimal for reading clarity and understanding the logical hierarchy of the program at a glance.

Steve McConnell Codex Complete Edition of the Second Issue on Layout and Style:

Subjects scored from 20 to 30 percent on an understanding test when programs had a two to four space indentation pattern than when programs had no indentation at all. In the same study, it was found that it is important not to emphasize and not emphasize the logical structure of programs. The lowest scores were achieved for programs that were not indented at all. The second lowest achieved in programs that used a six-position deepening. The study found that a deviation of two to four was optimal. It is interesting to note that many experiments believed that the six-position recess was easier to use than the smaller indentation, although their scores were lower. This is possible because the six indentation in space looks nice. But no matter how beautiful it looks, six indentation turns out to be less readable. This is an example of a clash between aesthetic appeal and readability.

+20
source share

You can change the setting of your OS so that the tab of 4 spaces. Instead, do it for your development environment. You may not like the idea of ​​changing the settings at the OS level.

+6
source share

You can argue about this all day, but in the end it's an arbitrary decision. And having a standard is better than letting bicker around it until the end of time.

And “more typing” is not really a valid argument against it, since you can easily customize any normal code editing tool to insert 4 spaces when you click a tab.

+2
source share

I think this is mainly because it is not fixed how many spaces are in the tab. This makes the code more portable, because it is not prone to display errors because the tabs are displayed differently.

The thing with Java is that you're probably going to end up using the IDE anyway, which makes it obsolete to press the space bar three times.

+1
source share

All Articles