简体   繁体   English

java 方法应具有的标准参数数量是多少?

[英]what is the standard number of parameters that a java method should have?

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?谁能告诉我或至少指出我正确的方向?

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 个左右,再多的话会对代码的可读性。

In Java you can't define more than 255 pararmeters for a method.在 Java 中,您不能为一个方法定义超过 255 个参数。 This is the restriction.这是限制。

For and advise, Uncle Bob says -Clean Code- max parameter count should be three.为了和建议,鲍勃叔叔说 -Clean Code- 最大参数计数应该是三个。

  • Too many parameters, parameter xxxxxxx is exceeding the limit of 255 words eligible for method parameters参数过多,参数 xxxxxxx 超出了符合方法参数条件的 255 个字的限制

This really has nothing to do with Java specifically.这确实与 Java 具体无关。 And you should definitely make it configurable, because there are quite different views on this.你绝对应该让它可配置,因为对此有完全不同的看法。

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.大多数人会认为这种方式过于严格,并且不会在具有 3 个参数的方法上闪烁两次。 You can probably get broad agreement that 6 parameters is too many.您可能会普遍认为 6 个参数太多了。

Checkstyle is a popular tool to check java coding standard. Checkstyle是检查 java 编码标准的流行工具。

Here is the link the the ParameterNumber rule: ParameterNumber这是ParameterNumber规则的链接: ParameterNumber

There really is not a standard number of parameters.确实没有标准数量的参数。

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).我个人的偏好是不要超过 3 个或至少 4 个,因为这会影响可读性和思维导图(很难记住超过 4 个参数)。 You can also have a quick peep at Uncle Bob's Clean Code and Steve McConnell's Code Complete regarding this.你也可以快速浏览一下鲍勃叔叔的清洁代码和史蒂夫麦康奈尔的代码完成

There is a similar thread in StackOverflow see When a method has too many parameters? StackOverflow 中有一个类似的线程,请参阅When a method has too many parameters?

You can use any number of arguments in a function in java.您可以在 java 中的 function 中使用任意数量的 arguments。 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. 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 .

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).没有硬性限制,但我想说超过五个是没有关键字 arguments 的语言(例如 Java)的代码异味。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM