Viewed k times. Just referencing here this question which is a practical example of how many parameters are too many In JavaScript parameters are too many: jsfiddle. Active Oldest Votes. A good rule-of-thumb is the number of CPU registers, because anymore and the compiler will be forced to allocate them on the stack. Michaelangel regarding the answer you are commenting you should not say that, because it tells that there is no rule. Furthermore, the number of parameters is a matter of readability, not performance.
Compilers guys have to deal with "register spill" all the time. The only authoritative answer is to inspect the assembly of what your compiler is generating. The numbers of registers doesn't magically change on the same platform. Add a comment. I'm voting this up. I am amazed at all the other answers saying "3" and "4"! The correct answer is: the minimum necessary, which can sometimes be quite a few. If that function were designed today it would probably look a little different.
Now you have only 8 parameters. It already has been redesigned. This is C and winapi. I can't think of a worse example. No, no. This is procedural style. Windows are objects, so this is obsolette now. Today, this would be solved with aggregated classes, not with bunch of parameters.
The intuitive rule of good design is if you can not describe function including parameters in one simple sentence, it is poorly designed. I believe this is the case. Show 15 more comments.
Here's the gist: The ideal number of arguments for a function is zero niladic. I doubt it includes "this" because that is the context of execution. In functional programming, the context is global and in oop, the context is the object on which you're applying the method.
Also, if you'd include "this" in the parameter list, then it would be impossible to have 0 params ideal. No, it doesn't include "this. It's just the nature of programming with iterators i. If algorithm and collection were to be redesigned, I would make algorithms always take a whole collection, and you'd get a way to slice a view of a collection to allow algorithms to work on parts; alternatively I'd also define a shorthand override to make it easy to work on the common case. Collections would be ranges, but not all ranges would be collections.
And indeed, boost has done that already. Anyway, my point is that a massively widely used library ignores this advice, so the worst that's guaranteed to happen to you if you do too, is that many of your millions of users will tinker with minor simplifications to your interface ;- — Steve Jessop. Show 9 more comments.
Please don't do that! Jeffrey L Whitledge. Some code I was working on used class members to achieve the same thing. Rob Walker. That is a very good answer. If the parameters are organized logically x,y,w,h it is easy to remember all of them in correct order. It is mutch harder to rember where to put the FILE pointer in putc which only has two parameters , especially since fprintf is the opposite.
Nicest answer. Very well said — Anwar. Thank you very much for all your answers: It was a bit surprising to find people who also think like I do that 5 parameters is a good limit for the sanity of the code. If you don't want to read his comments, I summarize for you: in a nutshell, it depends : I hate making hard and fast rules like this because the answer changes not only depending on the size and scope of your project, but I think it changes even down to the module level.
Finally, I think wnoise much agrees with Nick, and concludes his satirical contribution with this poetical vision see comments below of the art of programming: Programming is not engineering. Please, name a language that doesn't use routines nor parameters.
I can't think of one, even assembly could be considered to have routines labels. Sorry, "ret", not "return". It's been a long day already. Sorry about the ambiguous phrasing Auron, I believe I fixed it. It was my answer that was language-specific, not the question.
Not all languages allow passing structures. Also, passing a structure means that the receiving method has to have the code to handle the structure and may, therefore, need more parameters. Show 1 more comment.
It seems like there are other considerations than mere number, here are some that come to mind: logical relation to the primary purpose of the function vs.
John Mulder. Peter S. Paul Reiners. This is good until you with with some devs would will try foo int a, float b, string c, double d. Best to try to avoid working with them I guess.
If someone else has given classes ridiculously long names I wouldn't let that influence how I define or call my routines. May I introduce you to the "carriage return"? When the list crosses one line on your IDE, then your monitor is too small to use it with that code and you should clearly buy a monitor with a higher horizontal resolution.
A line break or a reduction of the smount of parameters are just workarounds that don't solve the root problem which is that your monitor is too small for that codebase! Seven things in short term memory? Name of the function Return value of the function Purpose of the function Parameter 1 Parameter 2 Parameter 3 Parameter 4. Mike Clark. It's a rule of thumb. When you are coding the body of a function, you don't care about its name, and the return value and its purporse are closely related.
This is so sad Kirk Strauser. Well, prot and flags could have been rolled together if it was felt by the designer that 5 was somehow a magic number that's much better than 6. The last couple of code listings show how a Builder is typically used - to construct an object. Indeed, the item on the builder Item 2 in Joshua Bloch's Second Edition of Effective Java is in the chapter on creating and destroying object.
However, the builder can help indirectly with non-constructor methods by allowing an easier way to build parameters objects that are passed to methods. This is shown in the next code listing. A more traditional instantiation of a Person instance is shown after that for comparison. To demonstrate the utility of the Introduce Parameter Object refactoring, let's first look at the example from the last post which uses numerous String and boolean parameters in a method call.
This encapsulation of related parameters makes it easier to quickly ascertain what types are being passed to the method or constructor. It is easier for a developer to understand a smaller number of parameters.
Let the method be invoked with these arguments:,When you declare a parameter to a method or a constructor, you provide a name for that parameter. This name is used within the method body to refer to the passed-in argument. The method can be called either with an array or with a sequence of arguments. The code in the method body will treat the parameter as an array in either case. The method can then be called with any number of that parameter, including none.
What is a good number of parameters in a method and f you need to pass more parameters in a method what do you do? Posted on April 26, May 28, by dams. How many parameters is too many? Number of parameters by function To check the practice of parameter counts, we collected OSS projects, and counted the number of parameters for each method, function and closure that we could analyze with Exakat.
The graph has many interesting features. A long queue up to 57 Some classes may collect a huge amount of arguments. What are they? Database generated classes First, there is a category of methods that are database access. Dependency injection Other long list of arguments are needed to handle dependency injection.
Conclusion Anything up to 5 arguments is OK, and it is probably a good baseline. Starting with 6 arguments, you should see if this needs some refactoring. Sometimes, the best solution is to replace the method with a class. All parameters may be set independently, with methods, and the result is processed once all parameters are collected.
Beyond 12 arguments, the code is automatically generated, and is not meant to be handled manually. Otherwise, refactor.
0コメント