在 Java 中格式化长方法调用的可接受方式是什么?

What is an acceptable way to format a long method call in Java?

这样写好吗:

objectName.methodWithManyParameters(someLongParameter1, someLongParameter2, someLongParameter3, someLongParameter4, someLongParameter5);

(一行显然太长了)as

objectName.methodWithManyParameters
(
    someLongParameter1, 
    someLongParameter2, 
    someLongParameter3, 
    someLongParameter4, 
    someLongParameter5
);

另一种方式是:

objectName.methodWithManyParameters(someLongParameter1, someLongParameter2, 
                                    someLongParameter3, someLongParameter4,
                                    someLongParameter5);

根据Oracle conventions

4.2 换行

当一个表达式无法放在一行中时,请根据以下一般原则将其拆分:

  • 逗号后换行。
  • 在运算符之前中断。
  • 比低级休息更喜欢高级休息。
  • 将新行与前一行同一层的表达式开头对齐。

如果上述规则导致代码混乱或代码被挤到右边距,只需缩进 8 个空格即可。

以下是中断方法调用的一些示例:

someMethod(longExpression1, longExpression2, longExpression3, 
        longExpression4, longExpression5);


正在恢复

第二个选项是标准约定,第一个更具可读性,但可能会损害非常长的方法,或者如果由于 类...

的长度而导致多次调用

据我所知,没有标准的换行方式。这要么是个人喜好问题,要么是公司内部编码风格标准的问题。 但是,我建议阅读Google Java Style。以下是可能相关的引述。

个人喜好如下(希望对你有帮助):

objectName.methodWithManyParameters( someLongParameter1, 
                                    someLongParameter2, 
                                    someLongParameter3, 
                                    someLongParameter4, 
                                    someLongParameter5 );

4.4 Column limit: 80 or 100

Projects are free to choose a column limit of either 80 or 100 characters. Except as noted below, any line that would exceed this limit must be line-wrapped, as explained in Section 4.5, Line-wrapping.

Exceptions:

Lines where obeying the column limit is not possible (for example, a long URL in Javadoc, or a long JSNI method reference). package and import statements (see Sections 3.2 Package statement and 3.3 Import statements). Command lines in a comment that may be cut-and-pasted into a shell. 4.5 Line-wrapping

Terminology Note: When code that might otherwise legally occupy a single line is divided into multiple lines, typically to avoid overflowing the column limit, this activity is called line-wrapping.

There is no comprehensive, deterministic formula showing exactly how to line-wrap in every situation. Very often there are several valid ways to line-wrap the same piece of code.

Tip: Extracting a method or local variable may solve the problem without the need to line-wrap.

4.5.1 Where to break

The prime directive of line-wrapping is: prefer to break at a higher syntactic level. Also:

When a line is broken at a non-assignment operator the break comes before the symbol. (Note that this is not the same practice used in Google style for other languages, such as C++ and JavaScript.) This also applies to the following "operator-like" symbols: the dot separator (.), the ampersand in type bounds (), and the pipe in catch blocks (catch (FooException | BarException e)). When a line is broken at an assignment operator the break typically comes after the symbol, but either way is acceptable. This also applies to the "assignment-operator-like" colon in an enhanced for ("foreach") statement. A method or constructor name stays attached to the open parenthesis (() that follows it. A comma (,) stays attached to the token that precedes it.

如果您正在与其他人一起工作,或者在预先存在的代码库中工作,请使用他们已经使用的任何标准。 80 对 100 列,选项 #1/2/3 等

如果您是独自工作, 可以解决问题。使用 Oracle 约定,并且可能使用 100 个字符的行长度;我们有现代屏幕,比 1996 年的显示器能容纳更多的文字。