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

发布于 2024-11-07 14:34:26 字数 365 浏览 0 评论 0原文

通常,传递给函数的参数数量可能很大。考虑以下情况:

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

Java 中是否有提供对齐参数方法的指南?将所有参数放在一行中看起来不太漂亮。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

丶情人眼里出诗心の 2024-11-14 14:34:26

当我必须调用这样的方法时,我喜欢将参数放在自己的行上,如下所示:

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

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

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.

娇俏 2024-11-14 14:34:26

根据 Sun 的 Java 编码约定,“换行”部分”:

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

  • 逗号后换行。
  • 在运算符之前中断。
  • 优先选择较高级别的休息时间而不是较低级别的休息时间。
  • 将新行与上一行同一级别的表达式开头对齐。
  • 如果上述规则导致代码混乱或代码被挤到右边距,只需缩进 8 个空格即可。

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

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

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

According to Sun's Java coding conventions, section "Wrapping Lines":

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.

The document also includes some examples for method calls:

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

var = function1(longExpression1,
                function2(longExpression2,
                          longExpression3));
遥远的她 2024-11-14 14:34:26

我会把我的小沙粒放在这里,很久以前有一个名叫 Esteban 的开发者
建议我采用这种格式,我首先认为它很难看
一段时间后,没有其他方法可以让我满意:

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

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

定义方法的模式也类似

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

     // MyCode

    }

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

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

我发现的唯一问题是IDE格式化程序从来不允许这种“开头逗号”方法,这非常有趣,还有更多比我尝试过的任何其他内容都可读。

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

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:

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 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.

Hope it adds something interesting

野侃 2024-11-14 14:34:26

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

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

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

SomeObject lastUpdate = dataManager.getLastUpdate();
int children = lastUpdate.getNumberOfChildren();
int parents = lastUpdate.getNumberOfParents();
int grandChildren = lastUpdate.getNumberOfGrandChildren();
calculate(children, parents, grandChildren, milliseconds, somethingelse);
太阳哥哥 2024-11-14 14:34:26

参考您的示例,Eclipse 和其他 IDE 按照上面的方式对其进行格式化(每行 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.

筑梦 2024-11-14 14:34:26

我完全同意你的例子,每行一个参数,所有参数都排列在彼此下面。

它使得浏览列表以查看有什么或缺少什么变得非常容易。

它还可以更轻松地将空值记录为“// user id”或类似的内容。

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文