Is it bad form to have functions with a lot of parameters? What's the alternative?

1.3k Views Asked by At

I have a search function that queries the database and has ~15 optional parameters. Obviously this is not pretty and calling it is a bit of a mess. PHP does not allow overloading methods so I've just been creating huge function signatures.

Elsewhere I've seen suggestions such as creating a parameter class: Disadvantages of using a lot of parameters

But this seems too heavy. I could pass in an associative array, but while this reduces the number of parameters I believe it is less easy to follow as there is no built in documentation stating what keys should exist in the array.

Is there any other way to handle this gracefully? Typically in other languages I would have a really ugly private method that takes up to a dozen parameters and then create public methods of the same name which accept a subset of those parameters and internally call the private method.

6

There are 6 best solutions below

3
On BEST ANSWER

In PHP, you can use 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 that the function is being called on (if it makes sense). PHPMailer send mails like this:

// 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('[email protected]', 'Eric Rosebrock');

if(!$mailer->Send())
{
    echo 'There was a problem sending this mail!';
}

And it has many more optional parameters. It could as well use a method with hundred parameters, but this is much more readable.

EDIT: These solutions also better support optional parameters. In case of properties it is straightforward, in case of associative array, you can merge the array with array of default values.

0
On

You could create a class which stores the parameters as properties, allowing you to set each property as you need, then have a method which uses these properties to query the database. The constructor can set default values for these properties. This just makes calling a bit 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
On

Yes, the good rule of thumb is to have no more than 3-4 params. If you need more then normally you should use array or object as one of the params. But in some cases if you think you really need more params, then sure, why not. If it makes your code easy to understand and use, then why not.

0
On

In general the long parameter list is a so called bad smell in code which can be removed via refactoring called Introduce parameter object. See this for reference.

Cheeres

0
On

The problem "too many parameters" in my opinion is only the manifestation of a much deeper underlaying problem: a bad architechture. If a function really needs all those parameter values, chances are very hight that it is doing a lot more than it should.

This should be a reminder "oh hey, let's reconsider not using procedure X to do all the stuff but to think what should really be done by X and what should be done by Y and Z.

0
On

It'd be nice to convert your function to class. There are two major advantages:

  • Function arguments converted to properties and can be commented

  • Function code which I think is rather large can be split into set of smaller private methods