Disadvantages of using a large number of parameters

I am rewriting code to make functional changes, and I am stuck in a situation where I need to overload a function to place two or three types of parameters (but performing almost the same operations on them) OR use one function with many parameters. Now I turn to the last option, and I just wanted to know the specific disadvantages (if any) of using a function with a lot of parameters (and when I say "a lot", I mean 15).

I am looking for a general answer, nothing specific in the language, so I do not mention the language here, but for information only, I use C #.

Thanks Rishi

+3
source share
4 answers

The problem with a lot of parameters is that in the place where you call the code, it can be difficult to understand 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 reasonable default values.

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

It may also be useful for you to put one group of related parameters that belong to each other in one class, and another group of parameters in another class.

+5
source

you can try to think like the one who will use this method. It is best to have an acceptable use of each argument.

If all arguments are not used in all cases, you can:

  • use optional parameters (e.g. C # 4 support)
  • use struct or class to store parameters and fill in the necessary properties
  • reorganize your code. I don’t know what your code does, but it seems to me that I have a huge amount of parameters.
+1
source

If you are trying to write your code "functional path", you may find "Currying" useful and create meaningful objects-objects that are initialized only with pairs of parameters. If a function takes many parameters, their list can (or should) usually be divided into meaningful fragments, and curry should form a chain of functions with meaningful intent.

So instead (an example of this answer):

 run(x, y, max_x, true, false, dx * 2, range_start, range_end, 0.01, true); 

you can use

 // initialize functors run_in_userbox = run(x, y, max_x); run_with_bounds = run_in_userbox(true, false); iterate_within_bounds = run_with_bounds(dx * 2, range_start, range_end, 0.01); result = iterate(true); //computation only starts here 

I don't know if this supports C #, but as usual the problem is solved in functional languages.

+1
source

As I usually do, you need to have very small separate methods for each required signature, but they have to urge private methods to do the actual work, which, as you said, is pretty much identical between use cases.

0
source

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


All Articles