Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of parameters for a constructor

I have a class that needs 12 parameters to be passed to its constructor. So I think that there is something wrong with the design of this class.

I would like to ask if there is any design pattern or a general collection of rules regarding the design of a class, especially its constructor.

like image 587
ali_bahoo Avatar asked Jan 04 '11 09:01

ali_bahoo


People also ask

How many parameters is too many in a constructor?

You might actually come close to the technical limit of 255 parameters for constructors and other units.

What is the parameters of constructor?

Parameterized constructorsA parameterized constructor accepts parameters with which you can initialize the instance variables. Using parameterized constructor, you can initialize the class variables dynamically at the time of instantiating the class with distinct values.

How many parameters are needed for a default constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Can a constructor have one or more parameters?

You can have constructors with multiple parameters, but also have a constructor with only the minimum parameters.


2 Answers

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.

like image 52
Péter Török Avatar answered Sep 28 '22 09:09

Péter Török


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?

like image 45
Stephane Rolland Avatar answered Sep 28 '22 09:09

Stephane Rolland