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?
-
14Forty two is best. – Andrew Thompson Apr 27 '11 at 13:33
-
I believe there is a limit in the byte code, preventing 256 or more parameters (including `this` if non static) – Peter Lawrey Apr 27 '11 at 13:46
-
Joshua Bloch (Google) says that 3 or less is ideal. – james.garriss Dec 12 '13 at 16:22
8 Answers
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.
-
8The number 7 probably comes from the fact that people can only hold about 7 items in their short-term memory at the same time. – Robert Harvey Apr 27 '11 at 22:23
-
thank you to everyone for your speedy responses, i think am going to go with 6 parameters. – user727308 Apr 28 '11 at 09:22
-
6
-
2255 parameters in static methods and 254 in non-static("this" would add up in this case) – userv Jun 01 '15 at 18:46
-
After 7 the code looses readability? It happens after 1 actually :) A good rule of thumb: 1 parameter - great, 2 parameters - ok, 3 - are you sure?, 4 - you are doing OOP wrong (unless you have a really good reason)... – Boris Churzin Dec 24 '17 at 13:54
-
In theory, the JVM specifies 255 being it's maximum number of parameters allowed. So only @userv was correct. For practice it depends on everyone's opinion – Lorenzo Dec 19 '22 at 15:35
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.

- 342,105
- 78
- 482
- 720
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

- 2,517
- 25
- 19
Checkstyle is a popular tool to check java coding standard.
Here is the link the the ParameterNumber rule: ParameterNumber

- 554
- 10
- 24
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?

- 1,245
- 1
- 10
- 23

- 1,197
- 1
- 12
- 23
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.

- 58,650
- 30
- 162
- 207
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).

- 355,277
- 75
- 744
- 836