Does parameter count really matter?

Andrei Aleksandrov
2 min readJan 29, 2018

--

Hi!

In this month I am veeeery busy and don’t have time for long and cool articles, but for today’s post I found an interesting thought to share with you ☺

We all know such a metric that is called LoC (lines of code). And for most developers it is a nonsense, because this metric doesn’t tell us so much about software. I see some cases where this metric can give us some information (for example, if we want to rewrite the software in another language or want to find anomalies in class methods), but using this as day-to-day quality indicator is not a good idea.

There is also another metric — count of parameters. I thought that it’s also a “bad” metric like LoC.

The first reason is that when we create immutable objects with constructor injection (which means that you don’t have “Set..” (and also “Get..”, but not necessary) methods), count of constructor’s parameters can tell us whether such an object is too big or not. Here is an example of such an object:

But here the count of parameters has grown:

Look, this class doesn’t correspond to single responsibility principle

And it’s time to refactor that:

Here we have three small simple objects and each of them has a single function and we have only one parameter in constructor of each object

Story

I was wondering when I got my homework exercise for a software engineering and there was a condition that “your methods shouldn’t have more that 5 parameters”.

I thought like “ha-ha-ha, it’s impossible to write such a software on Windows, because process creation itself has more than 5 parameters”:

Refactor WinAPI

But then I thought how can we reduce parameter count and will we really benefit from this?

First, I looked what parameters are necessary and which not and what we can do to pass those parameters. At the end, I came to the idea of creating such a method:

Is that really better? Yes. I have seen people that had problems with “CreateProcess” because it has too many parameters and before starting using this function you should inspect and pass all of them which is not very easy for the first time. And now we can just get a default configuration of those parameters and call CreateProcess faster:

And now I’m sitting and thinking whether parameter count matters or not…

--

--