Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many function parameters is too many? [duplicate]

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.

like image 967
Chris Kloberdanz Avatar asked Nov 25 '08 21:11

Chris Kloberdanz


People also ask

How many parameters to a method is too many?

The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification - and then shouldn't be used anyway.

How many parameters should a function have?

The main function can be defined with no parameters or with two parameters (for passing command-line arguments to a program when it begins executing). The two parameters are referred to here as argc and argv, though any names can be used because they are local to the function in which they are declared.

How many parameters can be passed?

There is no maximum limit to pass parameters or arguments to a user defined function. But for a pre-defined function it depends. Not sure, however some sources say that C functions accept at least 127 parameters.

How many parameters is too much java?

In the Java edition of Building Maintainable Software, Joost Visser advises keeping the number of parameters to no more than four. The same guideline probably applies to almost all other programming languages, like C#, Scala and even FORTRAN and COBOL.


2 Answers

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

like image 190
Paul Tomblin Avatar answered Oct 02 '22 12:10

Paul Tomblin


According to Steve McConnell in Code Complete, you should

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

like image 22
Paul Reiners Avatar answered Oct 02 '22 12:10

Paul Reiners