Java 代码换行 - 如何处理长行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3730002/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:00:29  来源:igfitidea点击:

Code line wrapping - how to handle long lines

javacoding-styleconventionsline-breaks

提问by corsiKa

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.

我面对的是 153 个字符长的特定行。现在,我倾向于在 120 个字符之后打破一些东西(当然,这在很大程度上取决于我所在的位置和当地的惯例。)但老实说,我在哪里打破线只会让它看起来很糟糕。所以我正在寻找一些关于我应该为它做什么的想法。

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.

我们不是 Java 商店,也没有针对此类事情的本地约定,或者显然我会简单地遵循它们。

Thanks!

谢谢!

采纳答案by Anon

In general, I break lines beforeoperators, and indent the subsequent lines:

通常,我操作符之前换行,并缩进后续行:

Map<long parameterization> longMap
    = new HashMap<ditto>();

String longString = "some long text"
                  + " some more long text";

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.

对我来说,领先的运营商清楚地传达了“这条线是从别的东西延续下来的,它不是独立的。” 当然,其他人有不同的偏好。

回答by Colin Hebert

IMHO this is the best way to write your line :

恕我直言,这是写你的台词的最佳方式:

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper =
        new HashMap<Class<? extends Persistent>, PersistentHelper>();

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.

这样不带任何大括号的增加缩进可以帮助您看到代码只是因为行太长而被拆分。而不是 4 个空格,8 个会让它更清晰。

回答by whiskeysierra

Uses Guava's static factory methods for Maps and is only 105 characters long.

对 Maps 使用 Guava 的静态工厂方法,并且只有 105 个字符长。

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper = Maps.newHashMap();

回答by Daniel

This is how I do it, and Googledoes it my way.

这就是我的方式,谷歌也是我的方式。

  • Break beforethe symbol for non-assignmentoperators.
  • Break afterthe symbol for =and for ,.
  • 打破之前的符号非赋值运算符。
  • 突破的符号=,

In your case, since you're using 120 characters, you can break it after the assignment operator resulting in

在您的情况下,由于您使用的是 120 个字符,因此您可以在赋值运算符之后将其中断,从而导致

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper =
        new HashMap<Class<? extends Persistent>, PersistentHelper>();

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.

在 Java 中,对于这种特殊情况,我会在中断后给出两个制表符(或八个空格),具体取决于制表符还是空格用于缩进。

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.

这当然是个人偏好,如果您的项目有自己的换行约定,那么无论您喜欢与否,这都是您应该遵循的。

回答by Sasa

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:

我认为将最后一个运算符移到下一行的开头是一个很好的做法。这样你马上就知道第二行的目的,即使它不是以操作符开头。我还建议为以前损坏的制表符使用 2 个缩进空间(2 个制表符),以区别于正常的缩进。这在继续上一行时立即可见。因此我建议这样做:

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper 
            = new HashMap<Class<? extends Persistent>, PersistentHelper>();