Never work with absolute numbers. These metrics don’t work. If something like this could be determined compilers would prohibit a higher number.
You can establish something for your project, but it’s silly. Basically this is what is called "good practice", that is, the person establishes a rule because he does not know how to solve the problem conveniently.
It must have as many parameters as necessary.
There are too many parameters when they do not serve the purpose of the method. They are too many when you are passing what you do not need. In general if you are not using a parameter in a given situation there should be a version of it without the parameter. For everything there is exception. We must be pragmatic.
Some people will say that the solution to many parameters is to pass an object. It even makes sense, in fact if you have several related parameters an object there reduces the amount of parameters. But it does bring some benefit?
Will you create an object just to meet this "requirement" of few parameters? And if you do that, you’ve formally decreased the amount of parameters, but the amount of information you’ve entered is the same. What’s the benefit of that? Performance? But it doesn’t cost to build this object? Type less? But type less? Can you handle this object correctly? You know that you can have different semantics, since probably the parameters end up being passed by reference?
What if the object already exists? Great, right? Just pass it. I’m not saying there’s anything wrong with it, if it makes sense, really pass it. If you know how to deal with this situation there is no problem. But who likes to say in general what is good to do will say that this causes excessive coupling (this is the most important thing you should read) since it is almost certain that you will be passing data that is not required for the method.
Here is an addendum on the book Clean Code or everything that Uncle Bob talks about, since there’s an answer (and reproduced in another answer here) in the question that served as inspiration quoting the book. Same with Code Complete. I think every programmer should read these books. But if he doesn’t have a personality, he doesn’t have a very good foundation, that book could butcher a person’s mind. There are incoherent things in the book, there is too much ideology, too much without plausible justification or context. I’m afraid when a fool reads these things and starts blindly following.
Most of the answers there in the question speak of low number and try to justify why this. The problem is that they do not give another solution or the other solution suffers essentially from the same problem. There is no magic. If the problem is complex the solution will be complex. It just cannot be complicated. And often trying to make up the complexity programmers tend to make everything more complicated. They add layers on top of layers without need. Just creating an object just to avoid passing arguments is an unnecessary layer and it doesn’t solve something since it will have to deal with various information in the same way.
There you see that people have a lot of opinion and little real sense. What comes closest to a solution is creating an object to avoid passing multiple parameters, which does not solve something and makes the code more complicated.
Of course, having too many parameters usually shows that the method is doing too many things or the data structure is poorly formulated. But this has to be analyzed case by case. Of course there comes a point where things start to get messy.
I want to point out that no one has given a solution to the case that makes sense for the method to have multiple parameters. No one says that because it depends so much on context and in many cases there is no way to do it to actually solve the problem.
What you can’t do is pass arguments unnecessarily. If the method is doing too much, it probably gets too many parameters. But the problem there is one of responsibility, not over-parameters. Solve the right problem. It’s the same question of number of lines the function should have.
People think things are orthogonal when they are not. They think that changing the argument of place solves some problem, when only the change of place.
And liability problem is a problem of lack of class cohesion, correct?
– Piovezan
Not only the class, the method itself. There is no magic rule that defines responsibility. It is a matter of correct collection of requirements, interpretation and proper use of the right mechanisms.
– Maniero
I believe that creating a Class (even if private and internal) to group the variables improves the organization and makes the code easier to understand. Once I created a Map with three Maps inside to structure the Data returned by one method and sent to another of the same class, it becomes very confusing to do such a thing.
– Douglas