‘Stupid’ Question 18:In RE to best practices- what is the max amount of parameters a function should have (OO lang)?
A very popular code smell is: ‘Long parameter list’. I want to talk about that. What is the upper limit? What is ‘too long’ ? Yes- I know, this is another ‘it depends’. But I wanted to know what the majority thinks, so I started asking. And please keep in mind that I mean OO languages ;)
In RE to best practices- what is the max amount of parameters a function should have (OO lang.)?
Robert C. Martin talks about the number of parameters in his book Clean Coder:
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.
Steve McConnell in Code Complete states:
Limit the number of a routine’s parameters to about seven
When I ask around the majority of the devs answer from most popular answer to least:
As few as you must have (…but’ often a general recommendation would follow, setting an upper limit)
A few said 10
Nobody said ‘go for it, as many as you like’
And why this recommendation?
Argument a) it indicates that your function *might* try to do to many things
Argument b) readability, hard to follow the flow, function *might* get to big
Argument c) According to Steve McConnell research shows that people have a hard time handling more than 7 chunks of information at the time.
Argument d) many parameters indicate that the data might be related and therefore should be grouped into a class
Argument e) long list of parameters indicate complexity- function should be broken up
My conclusion? I’ll strive towards small readable functions with preferrable less than 3-4 parameters, but I’ll have to see. It does depend. Or? What do you think?
Last modified on 2012-08-08