I've noticed a few functions I work with have 6 or more parameters, whereas in most libraries I use it is rare to find a function that takes more than 3.Often a lot of these extra parameters are binary options to alter the function behaviour. I think that some of these umpteen-parametered functions should probably be refactored. Is there a guideline for what number is too many?
Are there guidelines on how many parameters a function should accept?
functions;parameters
I've never seen a guideline, but in my experience a function that takes more than three or four parameters smells like one of two problems:The function is doing too much. It should be split into several smaller functions, each which have a smaller parameter set.There is another object hiding in there. You may need to create another object or data structure that includes these parameters. See this article on the Parameter Object pattern for more information.It's difficult to tell what you're looking at without more information. Chances are the refactoring you need to do is split the function into smaller functions which are called from the parent depending on those flags that are currently being passed to the function.There are some good gains to be had by doing this:It makes your code easier to read. I personally find it much easier to read a rules list made up of an if structure that calls a lot of methods with descriptive names than a structure that does it all in one method.It's more unit testable. You've split your problem into several smaller tasks that are individually very simple. The unit test collection would then be made up of a behavioral test suite that checks the paths through the master method and a collection of smaller tests for each individual procedure.