7

Possible Duplicate:
How many parameters are too many?

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.

Community
  • 1
  • 1
Chris Kloberdanz
  • 4,436
  • 4
  • 30
  • 31

15 Answers15

18

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

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
16

According to Steve McConnell in Code Complete, you should

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

Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
8

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

Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192
4

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
  • 88,451
  • 51
  • 221
  • 321
2

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
  • 1,292
  • 2
  • 15
  • 21
1

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
  • 3,457
  • 4
  • 31
  • 41
1

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
  • 3,975
  • 21
  • 16
1

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.

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
0

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
  • 196,001
  • 113
  • 385
  • 569
0

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
  • 1,198
  • 2
  • 16
  • 36
0

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
  • 5,284
  • 2
  • 35
  • 32
0

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
  • 302,674
  • 57
  • 556
  • 614
0

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
  • 21,309
  • 5
  • 49
  • 53
0

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);
user35978
  • 2,302
  • 1
  • 13
  • 14
-1

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

Geo
  • 93,257
  • 117
  • 344
  • 520