java - 为什么在Java中使用4个空格作为缩进单位?

标签 java coding-style

<分区>

Java code conventions说:

Four spaces should be used as the unit of indentation.

但是对于为什么首选空格,它没有任何解释。如果我键入空格 4 次而不是只按一次 Tab,我必须按 3 次以上的按键而不是一次。这意味着更多的工作,因为程序员一天要写很多代码。那么这个约定从何而来,是否有任何正当理由需要做更多工作?

最佳答案

根据我发现的一些研究,6 个空格会混淆代码的阅读并使其更难阅读。类似的情况是0个空格,

据说 4 个空格最适合阅读清晰和一目了然地理解程序的逻辑层次结构。

Steve McConnell 的 Code Complete Second Edition 关于布局和样式的章节:

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.

关于java - 为什么在Java中使用4个空格作为缩进单位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4802381/

相关文章:

java - 如何检查字符串是否不等于对象?

java - JPanels 无法设置任何大小值

objective-c - 实例和方法的命名约定

vb.net - 每当访问共享成员时强制使用类名

python - 用字符串键构造字典时,我应该在 Python 中写 dict 还是写 {}?

java - Java : How to Differ Inserted/Updated/NoChange states 上的 MySQL "INSERT ... ON DUPLICATE KEY UPDATE"

java - Spring数据MongoDB : search like on multiple fields

java - 将 Selenium 与 HP ALM 集成

c - 函数声明和定义 C - 参数定义为 pascal : what should it be?

ruby - 提高大型 attr_accessor 的可读性