java java方法应该具有的标准参数数量是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5804691/
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
what is the standard number of parameters that a java method should have?
提问by user727308
I am writing a program that checks the number of parameters of a methods and prints out a warning message (its a codesmell program) If there are more than what the standard is, the problem is that I don't know what the agreed number is. I have looked around and not had any luck. Can anyone tell me or at least point me in the right direction?
我正在编写一个程序来检查方法的参数数量并打印出警告消息(它是一个代码气味程序)如果超过标准,问题是我不知道约定的数字是多少. 我环顾四周,没有任何运气。谁能告诉我或至少指出我正确的方向?
采纳答案by phuibers
There is no standard limit on the number of parameters you can specify in Java, but according to "Code Complete"(see this post) you should limit the amount of parameters to about 7, any more and it will have a negative effect on the readability of your code.
您可以在 Java 中指定的参数数量没有标准限制,但根据“代码完成”(请参阅此帖子),您应该将参数数量限制在 7 个左右,如果再多,则会对代码的可读性。
回答by Burhan ARAS
In Java you can't define more than 255 pararmeters for a method. This is the restriction.
在 Java 中,您不能为一个方法定义超过 255 个参数。这就是限制。
For and advise, Uncle Bob says -Clean Code- max parameter count should be three.
对于并建议,Bob 叔叔说 -Clean Code- 最大参数计数应该是三个。
- Too many parameters, parameter xxxxxxx is exceeding the limit of 255 words eligible for method parameters
- 参数太多,参数 xxxxxxx 超过了方法参数的 255 个字的限制
回答by Michael Borgwardt
This really has nothing to do with Java specifically. And you should definitely make it configurable, because there are quite different views on this.
这实际上与 Java 没有任何关系。而且你绝对应该让它可配置,因为对此有不同的看法。
In "Clean Code", Robert Martin argues that the ideal number of method parameters is 0, 1 is OK, 2 needs strong justification, and 3 or more requires special dispensation from the pope.
在“Clean Code”中,Robert Martin 认为方法参数的理想数量是 0,1 是可以的,2 需要强有力的论证,3 或更多需要教皇的特殊豁免。
Most people will consider this way too strict and wouldn't blink twice at a method with 3 parameters. You can probably get broad agreement that 6 parameters is too many.
大多数人会认为这种方式过于严格,不会对具有 3 个参数的方法闪烁两次。您可能会普遍认为 6 个参数太多了。
回答by mohlendo
Checkstyleis a popular tool to check java coding standard.
Checkstyle是一种流行的检查 Java 编码标准的工具。
Here is the link the the ParameterNumber rule: ParameterNumber
这是 ParameterNumber 规则的链接:ParameterNumber
回答by MalsR
My honest opinion is there is no defined limit to the number of parameters. My personal preference is not to have more than 3 or at least 4 since this can affect readability and mental mapping (difficult to remember more than 4 parameters). You can also have a quick peep at Uncle Bob's Clean Codeand Steve McConnell's Code Completeregarding this.
我诚实的意见是对参数的数量没有明确的限制。我个人的偏好是不要超过 3 个或至少 4 个,因为这会影响可读性和心理映射(难以记住超过 4 个参数)。您还可以快速浏览一下 Uncle Bob 的Clean Code和 Steve McConnell 的Code Complete关于这一点。
There is a similar thread in StackOverflow see When a method has too many parameters?
StackOverflow 中有一个类似的线程参见当一个方法有太多参数时?
回答by John Kane
There really is not a standard number of parameters.
确实没有标准数量的参数。
回答by Harry Joy
You can use any number of arguments in a function in java. There is no standard limit to have this number of argument in function in java.[As per I know] IMO as a practice you should not have more than 4 arguments for a function but this is not the standard you can have any number of arguments.
您可以在 java 中的函数中使用任意数量的参数。在 java 中的函数中没有这个参数数量的标准限制。[据我所知] IMO 作为一种实践,你不应该有超过 4 个参数的函数,但这不是标准,你可以有任意数量的参数.
回答by Fred Foo
There's no hard limit, but I'd say more than five is a code smell in a language that has no keyword arguments (such as Java).
没有硬性限制,但我想说,在没有关键字参数的语言(例如 Java)中,超过五个是代码异味。