java 为什么在Java中使用4个空格作为缩进单位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4802381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why 4 spaces are used as the unit of indentation in Java?
提问by newbie
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?
但它没有任何解释为什么首选空格。如果我输入 4 次空格而不是只按一次 Tab,我必须按 3 次以上的按键。这意味着要做更多的工作,因为编码人员一天要编写大量代码。那么这个约定从何而来,是否有任何正当理由做更多的工作?
回答by Arjun J Rao
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,
根据我发现的一些研究,6 个空格会阻碍代码的阅读并使其更难阅读。0个空格的情况类似,
4 spaces is said to be optimum for reading clarity and understanding the logical hierarchy of a program at a glance.
据说4个空格最适合阅读清晰和一目了然地理解程序的逻辑层次结构。
Steve McConnell's Code Complete Second Edition chapter on Layout and Style:
Steve McConnell 的 Code Complete 第二版关于布局和样式的章节:
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.
与完全没有缩进的程序相比,当程序有 2 到 4 个空格的缩进方案时,受试者在理解测试中的得分高 20% 到 30%。同一项研究发现,既不强调也不强调也不重要。过分强调程序的逻辑结构。完全没有缩进的程序的理解分数最低。第二低的是使用六个空格缩进的程序。该研究得出的结论是,2 到 4 个空格的缩进是最佳的。有趣的是,实验中的许多受试者都认为六格缩进比较小的缩进更容易使用,即使他们的分数较低。那可能是因为六个空格缩进看起来令人愉悦。但不管它长得多么漂亮,事实证明,六空格缩进的可读性较差。这是审美吸引力和可读性之间冲突的一个例子。
回答by Adeel Ansari
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.
您可以将操作系统设置更改tab
为 4 个空格。最好还是为您的 IDE 执行此操作。您可能不喜欢在操作系统级别更改设置的想法。
回答by Joachim Sauer
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.
并且“更多输入”并不是真正有效的反对它,因为您可以轻松设置任何理智的代码编辑工具以在按 Tab 键时插入 4 个空格。
回答by slhck
I think it's mainly because it is not fixed how many spaces are in a tab stop. This makes the code more portable as in not prone to display errors just because tabs are displayed differently.
我认为这主要是因为制表位中有多少个空格是不固定的。这使得代码更具可移植性,因为不会因为选项卡的显示方式不同而显示错误。
The thing with Java is that you're probably going to end up using an IDE anyway, which makes it obsolete to hit the space key three times.
Java 的问题在于,无论如何您最终可能会使用 IDE,这使得按空格键三次已经过时了。