Bryan Martin
1 min readJun 19, 2020

--

In my opinion, this is misguided advice. Instead of counting the number of arguments in the signature of your function you should consider the purpose of your function and whether or not the argument list of the signature fits its purpose. A caller should never be expected to pass arguments to a function that the function doesn’t use.

Extracting arguments into a arbitrary class in order to simply reduce the number of arguments can lead to additional mess, unless the class has meaning on it’s own. For example, let’s say you have a function called playBaseball, and you create a class that includes all of the arguments needed to play baseball called playBaseBallArguments. Is that really better than having a lot of arguments? It reminds me of when I tell my daughter to clean her room and she just throws everything in her closet and closes the door. Clean means that everything should be in it’s proper place, not just out of sight.

Creating a function that takes a class as an argument that’s too broad makes the code harder to read. For example, if your playBaseBall function take a car as an argument, someone might say, “How on earth do you play baseball with a car?”. If the answer is: “You don’t, you play baseball with a ball, a bat and a glove. Those things are in the trunk of the car.”, then you should expect a code review comment :).

--

--

Responses (1)