14

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?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user727308
  • 189
  • 1
  • 1
  • 14

8 Answers8

13

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.

Community
  • 1
  • 1
phuibers
  • 1,239
  • 11
  • 11
  • 8
    The 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
    There is a technical maximum of 255 parameters that a method can have. – scravy Jan 29 '13 at 14:30
  • 2
    255 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
7

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.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
6

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
Burhan ARAS
  • 2,517
  • 25
  • 19
2

There really is not a standard number of parameters.

John Kane
  • 4,383
  • 1
  • 24
  • 42
2

Checkstyle is a popular tool to check java coding standard.

Here is the link the the ParameterNumber rule: ParameterNumber

mohlendo
  • 554
  • 10
  • 24
2

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?

Boris Churzin
  • 1,245
  • 1
  • 10
  • 23
MalsR
  • 1,197
  • 1
  • 12
  • 23
0

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.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
0

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

Fred Foo
  • 355,277
  • 75
  • 744
  • 836