Number of parameters for a constructor

If your function takes eleven parameters, you probably have forgotten one more

I love this sentence because it sums it all: Bad design calls for bad design.

I took this is from the book C++ Coding Standards: 101 Rules, Guidelines, And Best Practices by Herb Sutter, Andrei Alexandrescu.

Edit: The direct quote is If you have a procedure with ten parameters, you probably missed some. It is itself a quote from Alan Perlis.

Functions with so many parameters are a symtom of bad design. One of the possibility is to try to encapsulate part of these parameters in an entity/class that has a defined goal. (not a garbage class that would list all parameters without meaningful structure).


Never forget the Single Responsibility Principle As a consequence, classes remain limited in size, and as a consequence, limited in number of member paramters, and thus limited in the size of parameters needed for its constructors. Like one of the comments below says, the class with so much constructor parameters may handle too much futile details independent of its main goal.


A look at this one is advised too: How many parameters are too many?


if it is possible you may group the parameters by classes and pass their instances to the constructor.


12 Parameters, something is most probably wrong with the design.

What is done with the parameters?

  • Does the class just send them into other constructors? Then perhaps it should just accept interfaces to ready constructed objects.
  • Is the class large and does a lot of things with all these parameters? Then the class has to much responsibility and should accept classes that takes care of the details instead.
  • Are there any "clusters" in the parameters? Perhaps are some of the parameters a class in the creation. Encapsulate them and give them the appropriate responsibility.

The alternative is that this is parameters for a lowlevel, performance critical construction, in which case the design just have to take back seat, but that is rarely the case.


12 parameters definitely sound too many to me. Options to reduce their numbers are:

  1. Introduce Parameter Object by grouping logically related parameters into an object and passing that object instead of the individual parameters.

  2. Introduce a Builder (optionally with method chaining). This does not reduce the actual parameter list but it makes the code more readable, and is especially useful if you have several different creation scenarios with varying parameters. So instead of

    MyClass someObject = new MyClass(aFoo, aBar, aBlah, aBaz, aBorp, aFlirp, 
            andAGoo);
    MyClass anotherObject = new MyClass(aFoo, null, null, aBaz, null, null, 
            andAGoo);
    

    you can have

    MyClass someObject = new MyClassBuilder().withFoo(aFoo).withBar(aBar)
            .withBlah(aBlah).withBaz(aBaz).withBorp(aBorp).withFlirp(aFlirp)
            .withGoo(aGoo).build();
    MyClass anotherObject = new MyClassBuilder().withFoo(aFoo).withBaz(aBaz)
            .withGoo(aGoo).build();
    
  3. (Maybe I should have started with this ;-) Analyse the parameters - Are all of them really needed in the constructor (i.e. mandatory)? If a parameter is optional, you may set it via its regular setter instead of the constructor.