What considerations should I mind when designing methods or functions that take in a lot of parameters? A lot meaning over 4 but less than 10.
Example, I am debating whether to pass in an array like so:
function makeAssembly(array $params)
{
$pump = $factory->fromModelNumber($params['modelNumber'], $params['stages'], $params['x']);
$motor = $factory->createMotor($params['frameId'], $params['productId'], $params['x']);
}
versus spelling out parameters in the method header:
function makeAssembly($modelNumber, $stages, $x, $frameId, $productId)
{
$pump = $factory->fromModelNumber($modelNumber, $stages, $x);
$motor = $factory->createMotor($frameId, $roductId, $x);
}
Is there a way that is clearly better or are both interchangeable?
In particular, are there any tenants that can be attributed to dependency injection, such as Tell Don't Ask principles that can be reused in this case as well?
$x
, is there a need for themakeAssembly
method?makeAssembly
is a method to create anAssembly
that hides implementation details to create a fairly complex object with lots of specs. The object has a pump that can created by using a$modelNumber
or$productId
(not shown). A motor object is also created and is part of theAssembly
.makeAssembly
thus houses a complex object creation details that I want to hide and encapsulate so that the somewhat messy legacy code won't be sprinkled in many other areas, and be contained to themakeAssembly
method....$a
or$b
to i.e. create an object, but not both. Using named variables for parameters forces you to specify both,function x($a, $b)
, where only one of those is required.