views:

1151

answers:

16

I was just writing a function that took in several values and it got me thinking. When is the number number of arguments to a function / method too many? When (if) does it signal a flawed design? Do you design / refactor the function to take in structs, arrays, pointers, etc to decrease the amount of arguments? Do you refactor the data coming in just to decrease the number of arguments? It seems that this could be a little less applicable in OOP designs, though. Just curious to see how others view the issue.

EDIT: For reference the function I just wrote took in 5 parameters. I use the definition of several that my AP Econ teacher gave me. More than 2; less than 7.

Duplicate of this question

A: 

I would say maximum 4 . Anything above , I think should be placed within a class .

Geo
what if it is for a classes constructor that has 20 properties?
Blankman
Property doesn't take parameters...
Daok
+13  A: 

According to Steve McConnell in Code Complete, you should

Limit the number of a routine's parameters to about seven

Paul Reiners
Big fan of that book
Uri
Seven seems to be a number that should not be exceeded in any kind of interface, parameters or menuentries.
Jonke
Seven is the amount of information that can be stored in short term memory. At least that is what they told me, but I forgot the details.
Gamecat
That's also why phone numbers started out at 7 digits.
LeopardSkinPillBoxHat
can we have a reference for that 7 digit phone number claim?
Tim
Of course you can't - it's arrant nonsense. Telephone numbers started out at 1 or 2 digits.
Will Dean
@Tim: you won't get it because it isn't true. The first phone number was "Hey! Can you hear me?"
MusiGenesis
7 is way too much IMHO.
The first phone number was a singleton :)
Uri
+10  A: 

I don't know, but I know it when I see it.

Paul Tomblin
Sorry no votes left. But this is just it.
Gamecat
So function parameters are like pornography?
Paul Fisher
Ok, that is an interesting change of subject. It gives a whole other meaning to the term software...
Gamecat
like art and porn :)
baash05
+1  A: 

Quick answer: When you have to stop and ask that question, you've got too many.

Personally I like to keep the number under six. If more is needed, then the solution depends on the problem. One approach is to use "setter" functions to give the values to an object that will eventually perform the function you desire. Another option is to use a struct, as you mentioned. Either way, you can't really go wrong.

Brian
I believe is this a.k.a. the Command pattern.
moffdub
+1  A: 

Well it would most certainly depend on what your function is doing as far as how many would be considered "too many". Having said that, it is certainly possible to have a function with a lot of different parameters that are options on how to handle certain cases inside the function, and having overloads to those functions with sane default values for those options.

With the pervasiveness of Intellisense (or equivalent in other IDEs) and tooltips showing the comments from the XML Documentation in Visual Studio, I don't really think that there's a firm answer to this question.

scwagner
+1  A: 

Too much parameter is a "Code Smell".

You can divide into multiple methods or use class to regroup variable that have something in common.

To put a number for the "Too much" is something very subjective and depend of your organization and the language you use, A rule of thumb is that if you can't read the signature of your method and have an idea of what is it doing than you might have too much information. Personnaly, I try not to go over 5 parameters.

Daok
Any reason why you prefer of using class to group variables? I thought a struct would be more sensible here.
RWendi
+3  A: 

I generally believe that if the parameters are functionally related (e.g., coordinates or color components), they should be encapsulated as a class for good measures.

Not that I always follow this myself ;)

Uri
A: 

For me is 5.

It is hard to manage ( remember name, order, etc ) beyond that. Plus If I come that far I have versions with default values that call this one.

OscarRyz
A: 

Depends on the Function as well, if your function requires heavy user intervention or variables, I wouldn't go past 7-8 range. As far as average number of parameters to go with, 5-6 is the sweet spot in my opinion. If you are using more than that you might want to consider class objects as parameters or other smaller functions.

Ayo
A: 

It varies from person to person. Personally, when I have trouble immediately understanding what a function call is doing by reading the invocation in code, it is time to refactor to take the strain off of my gray cells.

moffdub
+6  A: 

If you have to ask then that's probably too many.

Stephane Grenier
A: 

I've heard that 7 figure as well, but I somehow feel that it stems from a time when all you could pass where primitive values.

Nowadays you can pass a reference to an object that encapsulates some complex state (and behaviour). Using 7 of those would definitely be too much.

My personal goal is to avoid using more than 4.

Joachim Sauer
A: 

It depends strongly on the types of the arguments. If they are all integers then 2 can be too many. (how do I remember which order?) If any argument accepts null, then the number drops drastically.

The real answer comes from asking yourself:

  • how easy is it to understand calls when I'm reading code?
  • how easy is it to remember the correct arguments and argument order when writing code?
Darron
A: 

Robert C. Martin (Uncle Bob) recommends 3 as a maximum in Clean Code: A Handbook of Agile Software Craftsmanship

I don't have the book with me at the moment but his reasoning has to do with one, two and, to a lesser extent, three argument functions reading well and clearly showing the purpose of the function.

This of course goes hand in hand with his recommendation of very short, well named functions that adhere to the Single Responsibility Principal.

Kenny
A: 

If you ever use a language like Ada with named parameters, there should not be "too much parameters".

Alexandre Hamez
A: 

And it depends of the programming language.. In C, it's really not rare to see functions with 7 parameters.. However, in C#, I have rarely seen more than 5 parameters and I personally use less than 3 usually.

// In C 
 draw_dot(x, y, size, red, green, blue, alpha)

// In C# 
 Point point(x,y);
 Color color(red,green,blue,alpha);

 Tool.DrawDot(point, color);
You might want to replace C# with any-OO :p (and Yeah, There's got to be a new somewhere missing :p)