格式化传递给Java函数的多个参数

时间:2021-10-26 23:13:42

Often the number of arguments passed to a function can be large. Consider the following case:

通常,传递给函数的参数数可以很大。考虑下面的例子:

calculate(dataManager.getLastUpdate().getNumberOfChildren(),
          dataManager.getLastUpdate().getNumberOfParents(),
          dataManager.getLastUpdate().getNumberOfGrandChildren(),
          long milliseconds,
          int somethingelse)

Is there a guideline in Java that offers a way to align the arguments? Fitting all arguments in a line would not look pretty.

在Java中是否有一个指导原则,提供了一种对齐参数的方法?将所有参数都拟合在一条直线上并不好看。

6 个解决方案

#1


14  

According to the Sun's Java coding conventions, paragraph 4.1 "Wrapping Lines":

根据Sun的Java编码约定,第4.1段“包装线”:

When an expression will not fit on a single line, break it according to these general principles:

当一个表达式不适合一行时,根据以下的一般原则来打破它:

  • Break after a comma.
  • 后一个逗号。
  • Break before an operator.
  • 打破之前操作员。
  • Prefer higher-level breaks to lower-level breaks.
  • 喜欢高级别的休息而不是低级别的休息。
  • Align the new line with the beginning of the expression at the same level on the previous line.
  • 将新行与表达式的开头在上一行的同一级别对齐。
  • If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.
  • 如果上面的规则导致代码混乱或代码被挤压到正确的边距,只需缩进8个空格即可。

The document also includes some examples for method calls:

该文件还包括一些方法调用的示例:

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

var = function1(longExpression1,
                function2(longExpression2,
                          longExpression3));

#2


19  

When I have to call a method like this I like to put the arguments on their own line, like so:

当我需要调用这样的方法时,我喜欢把参数放在它们自己的线上,比如:

final int result = calculate (
    dataManager.getLastUpdate().getNumberOfChildren(),
    dataManager.getLastUpdate().getNumberOfParents(),
    dataManager.getLastUpdate().getNumberOfGrandChildren(),
    milliseconds,
    somethingelse
);

Obviously this is a personal preference, but if you're working with others on code, try to conform to the conventions already set forth.

显然,这是一个个人偏好,但是如果您正在与其他人一起处理代码,请尝试遵循已经列出的约定。

#3


6  

I'll put my little sand grain here, long time ago some developer named Esteban suggested me this kind of formatting, which I 1st thought it was ugly after a while no other way of doing it is enough pleasent for me:

我把我的小沙粒放在这里,很久以前有个叫Esteban的开发人员给我推荐了这种格式,我一开始觉得它很难看,但是没有别的方法可以让我很高兴:

final int result = calculate (
     dataManager.getLastUpdate().getNumberOfChildren()
     , dataManager.getLastUpdate().getNumberOfParents()
     , dataManager.getLastUpdate().getNumberOfGrandChildren()
     , long milliseconds
     , int somethingelse
     );

I find this really clear, very easy to add/delete new arguments, the # of arguments clear, only one argument per line, method call end really clear, etc...

我发现这很清楚,很容易添加/删除新的参数,参数的#清晰,每一行只有一个参数,方法调用结束非常清楚,等等…

Similar pattern for defining the method too

定义方法的模式也类似

public int calculate(
    final int numberOfChildren
    , final int numberOfParents
    , final int numberOfGrandChildren
    , final long milliseconds
    , final int somethingelse
    ) throws CalucalteExceptio {

     // MyCode

    }

And finally same pattern for nested calls, StringBuilder typicall sequence

最后是嵌套调用的相同模式,StringBuilder是典型的序列

   StringBuilder sb = new StringBuilder()
       .append('Children #').append(numberOfChildren).append(NL)
       .append('Parents #').append(numberOfParents).append(NL)
       .append('GrandChildren #').append(numberOfGrandChildren).append(NL)
       ;

The only problem I found is that IDE formatters never allow this 'comma at the beginning' approach which is really interesting, and a lot more readable than any other I've tried.

我发现的唯一问题是IDE格式器不允许使用这种“开始时的逗号”方法,这种方法非常有趣,而且比我尝试过的任何方法都更容易读懂。

Hope it adds something interesting

希望它能增加一些有趣的东西

#4


3  

I might assign the return values of the getNumberOf*() methods to variables:

我可以将getNumberOf*()方法的返回值分配给变量:

SomeObject lastUpdate = dataManager.getLastUpdate();
int children = lastUpdate.getNumberOfChildren();
int parents = lastUpdate.getNumberOfParents();
int grandChildren = lastUpdate.getNumberOfGrandChildren();
calculate(children, parents, grandChildren, milliseconds, somethingelse);

#5


1  

Referring to your example, Eclipse and other IDEs format it the way you have above (1 argument per line, all left aligned) and usually that looks pretty good.

参照您的示例,Eclipse和其他ide按照上面的方式对其进行格式化(每行一个参数,都是左对齐的),通常看起来很不错。

#6


0  

I wholeheartedly agree with your example of having one argument per line, all lined up under each other.

我完全同意你的例子,每行有一个参数,所有的参数都排在一起。

It makes it very easy to scan down the list to see what is there or what is missing.

它使它非常容易扫描下的名单,看看什么是存在的或缺少的。

It also makes it easier to document null values as being "// user id" or something similar.

它还可以更容易地将空值文档化为“//用户id”或类似的内容。

I find it's particularly easy to visually parse, rather than having several long lines of densely packed values that may often look alike.

我发现可视化地解析它特别容易,而不是有几行密集的值,这些值通常看起来很相似。

#1


14  

According to the Sun's Java coding conventions, paragraph 4.1 "Wrapping Lines":

根据Sun的Java编码约定,第4.1段“包装线”:

When an expression will not fit on a single line, break it according to these general principles:

当一个表达式不适合一行时,根据以下的一般原则来打破它:

  • Break after a comma.
  • 后一个逗号。
  • Break before an operator.
  • 打破之前操作员。
  • Prefer higher-level breaks to lower-level breaks.
  • 喜欢高级别的休息而不是低级别的休息。
  • Align the new line with the beginning of the expression at the same level on the previous line.
  • 将新行与表达式的开头在上一行的同一级别对齐。
  • If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.
  • 如果上面的规则导致代码混乱或代码被挤压到正确的边距,只需缩进8个空格即可。

The document also includes some examples for method calls:

该文件还包括一些方法调用的示例:

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

var = function1(longExpression1,
                function2(longExpression2,
                          longExpression3));

#2


19  

When I have to call a method like this I like to put the arguments on their own line, like so:

当我需要调用这样的方法时,我喜欢把参数放在它们自己的线上,比如:

final int result = calculate (
    dataManager.getLastUpdate().getNumberOfChildren(),
    dataManager.getLastUpdate().getNumberOfParents(),
    dataManager.getLastUpdate().getNumberOfGrandChildren(),
    milliseconds,
    somethingelse
);

Obviously this is a personal preference, but if you're working with others on code, try to conform to the conventions already set forth.

显然,这是一个个人偏好,但是如果您正在与其他人一起处理代码,请尝试遵循已经列出的约定。

#3


6  

I'll put my little sand grain here, long time ago some developer named Esteban suggested me this kind of formatting, which I 1st thought it was ugly after a while no other way of doing it is enough pleasent for me:

我把我的小沙粒放在这里,很久以前有个叫Esteban的开发人员给我推荐了这种格式,我一开始觉得它很难看,但是没有别的方法可以让我很高兴:

final int result = calculate (
     dataManager.getLastUpdate().getNumberOfChildren()
     , dataManager.getLastUpdate().getNumberOfParents()
     , dataManager.getLastUpdate().getNumberOfGrandChildren()
     , long milliseconds
     , int somethingelse
     );

I find this really clear, very easy to add/delete new arguments, the # of arguments clear, only one argument per line, method call end really clear, etc...

我发现这很清楚,很容易添加/删除新的参数,参数的#清晰,每一行只有一个参数,方法调用结束非常清楚,等等…

Similar pattern for defining the method too

定义方法的模式也类似

public int calculate(
    final int numberOfChildren
    , final int numberOfParents
    , final int numberOfGrandChildren
    , final long milliseconds
    , final int somethingelse
    ) throws CalucalteExceptio {

     // MyCode

    }

And finally same pattern for nested calls, StringBuilder typicall sequence

最后是嵌套调用的相同模式,StringBuilder是典型的序列

   StringBuilder sb = new StringBuilder()
       .append('Children #').append(numberOfChildren).append(NL)
       .append('Parents #').append(numberOfParents).append(NL)
       .append('GrandChildren #').append(numberOfGrandChildren).append(NL)
       ;

The only problem I found is that IDE formatters never allow this 'comma at the beginning' approach which is really interesting, and a lot more readable than any other I've tried.

我发现的唯一问题是IDE格式器不允许使用这种“开始时的逗号”方法,这种方法非常有趣,而且比我尝试过的任何方法都更容易读懂。

Hope it adds something interesting

希望它能增加一些有趣的东西

#4


3  

I might assign the return values of the getNumberOf*() methods to variables:

我可以将getNumberOf*()方法的返回值分配给变量:

SomeObject lastUpdate = dataManager.getLastUpdate();
int children = lastUpdate.getNumberOfChildren();
int parents = lastUpdate.getNumberOfParents();
int grandChildren = lastUpdate.getNumberOfGrandChildren();
calculate(children, parents, grandChildren, milliseconds, somethingelse);

#5


1  

Referring to your example, Eclipse and other IDEs format it the way you have above (1 argument per line, all left aligned) and usually that looks pretty good.

参照您的示例,Eclipse和其他ide按照上面的方式对其进行格式化(每行一个参数,都是左对齐的),通常看起来很不错。

#6


0  

I wholeheartedly agree with your example of having one argument per line, all lined up under each other.

我完全同意你的例子,每行有一个参数,所有的参数都排在一起。

It makes it very easy to scan down the list to see what is there or what is missing.

它使它非常容易扫描下的名单,看看什么是存在的或缺少的。

It also makes it easier to document null values as being "// user id" or something similar.

它还可以更容易地将空值文档化为“//用户id”或类似的内容。

I find it's particularly easy to visually parse, rather than having several long lines of densely packed values that may often look alike.

我发现可视化地解析它特别容易,而不是有几行密集的值,这些值通常看起来很相似。