我们应该在 Java 中每行保留 80 个字符吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6724945/
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
Should we keep 80 characters per line in Java?
提问by Kit Ho
There are lots of websites or blogs said that we should keep 80 characters per line in our program since it is relatively to display in some console environment.
有很多网站或博客说我们的程序每行应该保持80个字符,因为它相对于在某些控制台环境中显示。
However, I found that it is very hard to keep this practice in Java. When we write Java code, we write a function in a class, it requires us to do some indentation already, let alone to add a few if-else statements inside the function.
但是,我发现在 Java 中很难保持这种做法。我们在写Java代码的时候,在一个类中写一个函数,就已经需要我们做一些缩进了,更不用说在函数内部加几条if-else语句了。
Apart from that, we always keep call functions in a object. Such kind of behavior make that it is hard to make 80 characters per line.
除此之外,我们总是将调用函数保存在一个对象中。这种行为使得每行很难制作 80 个字符。
Wrapping the line is a method, but it decrease the readability of the code.
换行是一种方法,但会降低代码的可读性。
I am not a professional java programmer. Would you still follow this rule when writing Java? or is there common practice on the indentation of java?
我不是专业的 Java 程序员。你在写Java的时候还会遵循这个规则吗?或者是否有关于java缩进的常见做法?
回答by duffymo
80 character line length restrictions made sense when we dealt with paper punch cards and small monitors. I think it makes little sense now that we read code on wider monitors.
当我们处理纸质穿孔卡片和小型显示器时,80 个字符的行长度限制是有意义的。我认为现在我们在更宽的显示器上阅读代码没什么意义。
I find that nesting and methods have little effect on readability. If I see nesting that's too deep, it's a suggestion that the cyclomatic complexity of my method is too high - time to decompose.
我发现嵌套和方法对可读性几乎没有影响。如果我看到嵌套太深,则表明我的方法的圈复杂度太高 - 需要分解。
I don't want to scroll back and forth, but I find that 120 characters is manageable.
我不想来回滚动,但我发现 120 个字符是可以管理的。
回答by Kristian
It depends on your environment/team/ways of working. Since all of us have widescreens on our desks we want to use as much screen estate as possible. We have 120 characters and it has worked well for us.
这取决于您的环境/团队/工作方式。由于我们所有人的办公桌上都有宽屏,我们希望尽可能多地使用屏幕空间。我们有 120 个字符,对我们来说效果很好。
If you are having problems with always filling the 120 characters width I'd say you should look over your code, perhaps some refactoring is in place.
如果您在总是填充 120 个字符的宽度时遇到问题,我会说您应该查看您的代码,也许进行了一些重构。