I've developed a natural aversion to long parameter lists in functions. While this is to some extent a good thing, sometimes long parameter lists are the lesser of two evils compared to code duplication or ridiculously long functions due to "manual inlining". What's a good way to at least make some of these monstrosities human-readable? For example:
SomeClass[string] someFunction(SomeClass!(TemplateParam) foo,
string[][string] someAA, uint[] dataToProcess, SomeEnumType flag) {
// Do stuff.
}
This doesn't score high on the readability scale, but four parameters is pretty reasonable in a lot of cases.
Long parameter list can be caused by too complex methods. Another reason is avoiding dependencies. One way to reduce number of parameters is to replace a parameter with a method call. You can also preserve a whole object or introduce a parameter object.
The method's parameter list must not contain a rest value declaration. The two parameter lists must contain the same number of required value declarations. Each value type in the method's parameter list must be a subtype of the corresponding value type in the generic function's parameter list.
Well, if a method has five or more parameters, then you should consider refactoring that method and decreasing the number of parameters. This is where Introduce Parameter Object refactoring comes into play. In short, this refactoring is a way of replacing a method's argument list with a single parameter object.
For this kind of situation, I tend to format it like this:
SomeClass[string] someFunction(
SomeClass!(TemplateParam) foo,
string[][string] someAA,
uint[] dataToProcess,
SomeEnumType flag
)
{
// Do stuff.
}
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