在 Java 中格式化传递给函数的多个参数
通常,传递给函数的参数数量可能很大。考虑以下情况:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当我必须调用这样的方法时,我喜欢将参数放在自己的行上,如下所示:
显然这是个人偏好,但如果您正在与其他人一起编写代码,请尝试遵守已经提出的约定。
When I have to call a method like this I like to put the arguments on their own line, like so:
Obviously this is a personal preference, but if you're working with others on code, try to conform to the conventions already set forth.
根据 Sun 的 Java 编码约定,“换行”部分”:
该文档还包括一些方法调用的示例:
According to Sun's Java coding conventions, section "Wrapping Lines":
The document also includes some examples for method calls:
我会把我的小沙粒放在这里,很久以前有一个名叫 Esteban 的开发者
建议我采用这种格式,我首先认为它很难看
一段时间后,没有其他方法可以让我满意:
我发现这非常清楚,很容易添加/删除新参数,
参数数量清晰,每行只有一个参数,方法调用结束非常清晰,
等等...
定义方法的模式也类似
最终嵌套调用的模式相同,StringBuilder典型序列
我发现的唯一问题是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:
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
And finally same pattern for nested calls, StringBuilder typicall sequence
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
我可以将 getNumberOf*() 方法的返回值分配给变量:
I might assign the return values of the getNumberOf*() methods to variables:
参考您的示例,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.
我完全同意你的例子,每行一个参数,所有参数都排列在彼此下面。
它使得浏览列表以查看有什么或缺少什么变得非常容易。
它还可以更轻松地将空值记录为“// 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.