代码换行 - 如何处理长行

发布于 2024-09-19 06:11:55 字数 449 浏览 13 评论 0原文

我面临着一行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海蓝天 2024-09-26 06:11:55

一般来说,我在运算符之前换行,并缩进后续行:

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

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

对我来说,领先的运算符清楚地传达了“此行是从其他内容延续而来的,它并不独立”。当然,其他人有不同的偏好。

In general, I break lines before operators, 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.

神也荒唐 2024-09-26 06:11:55

我就是这样做的,Google 就是这么做的我的方式。

  • 非赋值运算符的符号之前换行。
  • =, 符号之后断开。

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

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

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

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

This is how I do it, and Google does it my way.

  • Break before the symbol for non-assignment operators.
  • Break after the symbol for = and for ,.

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

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.

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.

对你而言 2024-09-26 06:11:55

恕我直言,这是编写行的最佳方式:

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

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

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.

焚却相思 2024-09-26 06:11:55

使用 Guava 的 Maps 静态工厂方法,长度仅为 105 个字符。

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

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

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper = Maps.newHashMap();
春庭雪 2024-09-26 06:11:55

我认为将最后一个运算符移动到下一行的开头是一个很好的做法。这样您就可以立即知道第二行的用途,即使它不以运算符开头。我还建议为之前损坏的选项卡添加 2 个缩进空间(2 个选项卡),以区别于正常的缩进。这是立即可见的,是上一行的延续。因此我建议:

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

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:

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper 
            = new HashMap<Class<? extends Persistent>, PersistentHelper>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文