Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a method has too many parameters? [closed]

People also ask

What to do if a function has too many arguments?

There are two techniques that can be used to reduce a functions' arguments. One of them is to refactor the function, making it smaller, consequently, reducing the arguments' number. The Extract Method technique can be use to achieve this goal.

What is too many parameters?

Too Many Parameters is a code smell that is indicated by a method or function that takes in a very large list of parameters. Imagine a function that takes in twenty parameters.

How do you fix constructors with too many parameters?

Using a builder can solve the issue of having to many parameters in a constructor when all the fields are not mandatory. It also takes away the problems with having multiple constructors for different purposes. Note that a builder still should have a constructor with the mandatory fields that always needs to be set.

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.


There is no clear limit, but I am uncomfortable with more than 3-4 parameters. AFAIR Uncle Bob Martin in Clean Code recommends max 3.

There are several refactorings to reduce the number of method parameters (see Working Effectively with Legacy Code, by Michael Feathers for details). These come to my mind:

  • encapsulate many related parameters into a single object (e.g. instead of String surName, String firstName, String streetAddress, String phoneNumber pass a Person object containing these as fields)
  • pass parameters in the constructor or other method calls prior to the invocation of this method

When you have to ask, it is probably too many.


As Steve McConnell mentions in Code Complete, golden rule is 4 +/-3 parameters. For average human it's hard to remember more than 4 parameters, 5-7 should be used just in special cases and you should never use 8 or more.


Great Buddha!! Ninety-seven????

Good practise usually advises about max. six to eight. Of course, ymmv, and there may be a valid reason, from time to time, for a ninth. But 97??!!

A few thoughts ... are these simply data, or are decisions being made based on their values?

If many/most affect flow control you have an almost unmaintainable (even understandable, or testable) "design" (for small values of "design").

If they are simply data, can they be grouped into structures and pointers ot those structures passed?

Do you have any design documentation? Might that explain what is going on.

Oh, and, "Danger, Will Robinson" - anyone who will pass 97 parameters openly might also pass any number - not so obviously - as global variables.

P.s don't know how Eclipse works on Java, but with C/C++, if you put the paramaeters on separate lines

char DoEverything(
        int meaninglessParameterName1,
        char *meaninglessParameterName2,
        ....
        long double *meaninglessParameterName97)
        { return !NULL;}

Eclipse will actually identify the line with the bad parameter


Well, if you make it a JSON object, you can then wrap all 97 (or more) in that object and only send one object.


If its more than 5-10 parameters, then create an object that takes the parameters instead, it could be a typed dataset, structure or whatever.

Instead of :

Mywebservice.CreateUser(Firstname, LastName, Age,Sex, Haircolor,AmountOfFingers,AmountOfTeeht,Childrens,,,,,,,,,,,,,and so on)

Do:

Dim MyUser as new UserObject
MyUser.Firstname="Stefan"
...and so on...
MyWebservice.CreateUser(UserObject)