Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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?

like image 411
user727308 Avatar asked Apr 27 '11 13:04

user727308


People also ask

How many parameters should a method have Java?

The above example static method has only one parameter. You can add many different types of parameters but java gives a limit, the limit says you can add 255 parameters or less. All primitive or non-primitive types take one unit of parameter length, except long and double.

How many parameters should a method have?

The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification - and then shouldn't be used anyway.

What are the parameters of a method in Java?

Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

How many parameters is too much Java?

In the Java edition of Building Maintainable Software, Joost Visser advises keeping the number of parameters to no more than four. The same guideline probably applies to almost all other programming languages, like C#, Scala and even FORTRAN and COBOL.


1 Answers

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.

like image 198
Michael Borgwardt Avatar answered Oct 04 '22 16:10

Michael Borgwardt