Is this a bad form for working with many parameters? Which alternative?

I have a search function that queries the database and has ~ 15 optional parameters. Obviously, this is ugly, and calling it a bit of a mess. PHP does not allow methods to be overloaded, so I just created huge function signatures.

In another place, I saw sentences such as creating a parameter class: Disadvantages of using a large number of parameters

But it seems too heavy. I could pass an associative array, but while it reduces the number of parameters that I think is less easy to follow, since the documentation does not have inline documents indicating which keys should exist in the array.

Is there any other way to handle this gracefully? As a rule, in other languages ​​I would have a very ugly private method, which takes up to a dozen parameters, and then creates public methods with the same name that take a subset of these parameters and internally call the private method.

+4
source share
6 answers

In PHP you can use an associative array:

 someFunction(array( "a" => 3243, "b" => 2354, "c" => 33453, "d" => 324353, "e" => 321243, "f" => 321243, "g" => 312243, "h" => 321243, )) 

Or properties of the object on which the function is called (if that makes sense). PHPMailer sends emails as follows:

 // instantiate the class $mailer = new PHPMailer(); // Set the subject $mailer->Subject = 'This is a test'; // Body $mailer->Body = 'This is a test of my mail system!'; // Add an address to send to. $mailer->AddAddress(' foo@host.com ', 'Eric Rosebrock'); if(!$mailer->Send()) { echo 'There was a problem sending this mail!'; } 

And he has many more additional parameters. It can also use a method with 100 parameters, but it is much more readable.

EDIT: These solutions also better support advanced options. In the case of properties, this is simple; in the case of an associative array, you can combine the array with an array of default values.

+4
source

In the general case, a long list of parameters is the so-called bad code smell, which can be removed using refactoring, called the object of the "Representation" parameter. See this for reference.

Cheeres

+2
source

Yes, a good rule of thumb is to have no more than 3-4 parameters. If you need more, then usually you should use an array or object as one of the parameters. But in some cases, if you think that you really need additional parameters, then surely, why not. If this makes it easier to understand and use the code, why not.

+1
source

The “too many parameters” problem, in my opinion, is only a manifestation of the much deeper problem underlying: poor architecture. If a function really needs all of these parameter values, the likelihood that it does much more than it should.

This should be a reminder "oh hey, don't change your mind without using procedure X to do everything, but think about what really needs to be done on X and what Y and Z should be done.

+1
source

You can create a class that saves parameters as properties, allowing you to set each property as needed, and then use a method that uses these properties to query the database. The constructor can set default values ​​for these properties. It just makes calling easier.

 $function = ClassFunction(); $function->arg1 = 'Some value.'; $function->arg2 = true; $function->arg3 = 5; $result = $function->call_method(); // This uses default values for any property not set. 
0
source

It would be nice to convert your function to a class. There are two main benefits:

  • Function arguments converted to properties and can be commented out

  • Functional code, which, I think, is large enough, can be broken down into many smaller private methods.

0
source

Source: https://habr.com/ru/post/1338846/


All Articles