What is the standard number of parameters a java method should have?

I am writing a program that checks the number of method parameters and displays a warning message (its codemell program). If there is more than the standard, the problem is that I do not know what the agreed number is. I looked around and had no luck. Can someone tell me or at least point me in the right direction?

+7
source share
8 answers

There is no standard limit on the number of parameters that you can specify in Java, but according to "Code Complete" ( see this post ) you should limit the amount from parameters to about 7, and this will adversely affect the readability of your code.

+7
source

It really has nothing to do with Java. And you should definitely make it customizable because it has completely different perspectives.

In Pure Code, Robert Martin claims that the ideal number of method parameters is 0, 1 in order, 2 needs strong justification, and 3 or more requires special permission from dad.

Most people find this too strict and will not blink twice when using the three-parameter method. You can probably agree that there are too many 6 parameters.

+3
source

In Java, you cannot define more than 255 parameters for a method. This is a limitation.

For advice, Uncle Bob says: "Clean code" - the maximum number of parameters should be three.

  • Too many parameters parameter xxxxxxx exceeds the limit of 255 words suitable for method parameters
+3
source

Checkstyle is a popular tool for checking the java coding standard.

Here is the ParameterNumber rule link: ParameterNumber

+2
source

In fact, there is no standard number of parameters.

+1
source

You can use any number of arguments in a function in java. There is no standard restriction on using this number of arguments in a java function. [As you know] IMO as a practice should not have more than 4 arguments for a function, but this is not a standard, you can have any number of arguments,

0
source

There is no hard limit, but I would say that more than five is the smell of code in a language that has no keyword arguments (like Java).

0
source

My honest opinion is that there is no definite restriction on the number of parameters. My personal preference is not to have more than 3 or at least 4, as this can affect readability and mental juxtaposition (it is difficult to remember more than 4 parameters). You can also quickly browse Uncle Bob 's Clean Code and Steve McConnell Code Complete .

Is there a similar stream in StackOverflow, see If a method has too many parameters?

0
source

All Articles