代码换行 - 如何处理长行
我面临着一行 153 个字符长的特定行。现在,我倾向于在 120 个字符之后打破界限(当然,这在很大程度上取决于我所在的位置和当地的惯例。)但说实话,我在任何地方打破界限都只会让它看起来很糟糕。所以我正在寻找一些关于我应该为此做些什么的想法。
底线是这样的:
private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper = new HashMap<Class<? extends Persistent>, PersistentHelper>();
我对如何/在哪里打破界限(以及为什么)以及缩短界限本身的方法持开放态度。
我们不是 Java 商店,也没有此类事情的本地惯例,或者显然我会简单地遵循它们。
谢谢!
I'm facing a particular line that is 153 characters long. Now, I tend to break things after 120 characters (of course, this is heavily dependent on where I am and the local conventions.) But to be honest, everywhere I break the line just makes it look bad. So I'm looking for some ideas on what I should do for it.
Here's the line:
private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper = new HashMap<Class<? extends Persistent>, PersistentHelper>();
I'm open to both ideas about how/where to break the line (and why), as well as ways to shorten the line itself.
We're not a Java shop, and there aren't local conventions for this sort of thing, or obviously I would simply follow them.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一般来说,我在运算符之前换行,并缩进后续行:
对我来说,领先的运算符清楚地传达了“此行是从其他内容延续而来的,它并不独立”。当然,其他人有不同的偏好。
In general, I break lines before operators, and indent the subsequent lines:
To me, the leading operator clearly conveys that "this line was continued from something else, it doesn't stand on its own." Other people, of course, have different preferences.
我就是这样做的,Google 就是这么做的我的方式。
=
和,
符号之后断开。在您的情况下,由于您使用了 120 个字符,因此您可以在赋值运算符后将其打断,导致
In Java,对于这种特殊情况,我会在打断后给出两个制表符(或八个空格),具体取决于制表符还是制表符空格用于缩进。
这当然是个人喜好,如果您的项目有自己的换行约定,那么无论您喜欢与否,您都应该遵循它。
This is how I do it, and Google does it my way.
=
and for,
.In your case, since you're using 120 characters, you can break it after the assignment operator resulting in
In Java, and for this particular case, I would give two tabs (or eight spaces) after the break, depending on whether tabs or spaces are used for indentation.
This is of course a personal preference and if your project has its own convention for line-wrapping then that is what you should follow whether you like it or not.
恕我直言,这是编写行的最佳方式:
这样,增加不带任何大括号的缩进可以帮助您看到代码只是因为行太长而被分割。而且不是 4 个空格,而是 8 个空格会更清晰。
IMHO this is the best way to write your line :
This way the increased indentation without any braces can help you to see that the code was just splited because the line was too long. And instead of 4 spaces, 8 will make it clearer.
使用 Guava 的 Maps 静态工厂方法,长度仅为 105 个字符。
Uses Guava's static factory methods for Maps and is only 105 characters long.
我认为将最后一个运算符移动到下一行的开头是一个很好的做法。这样您就可以立即知道第二行的用途,即使它不以运算符开头。我还建议为之前损坏的选项卡添加 2 个缩进空间(2 个选项卡),以区别于正常的缩进。这是立即可见的,是上一行的延续。因此我建议:
I think that moving last operator to the beginning of the next line is a good practice. That way you know right away the purpose of the second line, even it doesn't start with an operator. I also recommend 2 indentation spaces (2 tabs) for a previously broken tab, to differ it from the normal indentation. That is immediately visible as continuing previous line. Therefore I suggest this: