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.
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.
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.
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:
String surName, String firstName, String streetAddress, String phoneNumber
pass a Person
object containing these as fields) 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With