简体   繁体   中英

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.

In Java you can't define more than 255 pararmeters for a method. This is the restriction.

For and advise, Uncle Bob says -Clean Code- max parameter count should be three.

  • Too many parameters, parameter xxxxxxx is exceeding the limit of 255 words eligible for method parameters

This really has nothing to do with Java specifically. 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.

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.

Checkstyle is a popular tool to check java coding standard.

Here is the link the the ParameterNumber rule: 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). 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?

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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