在 Java 中格式化传递给函数的多个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6023391/
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
Formatting multiple arguments passed to a function in Java
提问by Leonid
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
提供了一种对齐论点的方法?将所有参数放在一行中看起来并不漂亮。
回答by Nate W.
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.
显然,这是个人偏好,但如果您正在与其他人一起编写代码,请尝试遵守已经制定的约定。
回答by Vivien Barousse
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));
回答by tonio
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
希望它能增加一些有趣的东西
回答by Michael
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);
回答by Abdullah Jibaly
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 按照您上面的方式对其进行格式化(每行 1 个参数,全部左对齐),通常看起来很不错。
回答by Evvo
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.
我发现它特别容易在视觉上解析,而不是有几长行密集的值,这些值通常看起来很相似。