Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disadvantages of using a lot of parameters

I am re-writing some code to make functional changes and I am stuck at a situation where either I will need to overload a function to accommodate two or three types of parameters (but performing almost identical operations on them) OR use one function with a lot of parameters. Now I am going with the latter option, and I just wanted to know specific disadvantages (if any) of using a function with a lot of parameters (and when I say lot, I mean 15).

I am looking for a general answer, nothing language specific, so I am not mentioning the language here, but just for information, I am using C#.

Thanks Rishi

like image 529
Rishi Avatar asked Feb 26 '23 05:02

Rishi


1 Answers

The problem with a lot of parameters is that at the place where you call the code it can be difficult to see what the parameters mean:

// Uhh... what?
run(x, y, max_x, true, false, dx * 2, range_start, range_end, 0.01, true);

Some languages solve this by allowing named parameters and optional parameters with sensible defaults.

Another approach is to put your parameters in a parameter object with named members and then pass that single object as the argument to your function. This is a refactoring appraoach called Introduce Parameter Object.

You may also find it useful to put one group of related parameters that belong together into one class, and another group of parameters into a different class.

like image 92
Mark Byers Avatar answered Feb 27 '23 18:02

Mark Byers