How many constructors should a class have


I'm currently modifying a class that has 9 different constructors. Now overall I believe this class is very poorly designed… so I'm wondering if it is poor design for a class to have so many constructors.

A problem has arisen because I recently added two constructors to this class in an attempt to refactor and redesign a class (SomeManager in the code below) so that it is unit testable and doesn't rely on every one of its methods being static. However, because the other constructors were conveniently hidden out of view about a hundred lines below the start of the class I didn't spot them when I added my constructors.

What is happening now is that code that calls these other constructors depends on the SomeManager class to already be instantiated because it used to be static….the result is a null reference exception.

So my question is how do I fix this issue? By trying to reduce the number of constructors? By making all the existing constructors take an ISomeManager parameter?

Surely a class doesn't need 9 constructors! …oh and to top it off there are 6000 lines of code in this file!

Here's a censored representation of the constructors I'm talking about above:

public MyManager()
    : this(new SomeManager()){} //this one I added

public MyManager(ISomeManager someManager) //this one I added
    this.someManager = someManager;

public MyManager(int id)
    : this(GetSomeClass(id)) {}

public MyManager(SomeClass someClass)
    : this(someClass, DateTime.Now){}

public MyManager(SomeClass someClass, DateTime someDate)
    if (someClass != null)
       myHelper = new MyHelper(someOtherClass, someDate, "some param");

public MyManager(SomeOtherClass someOtherClass)
    : this(someOtherClass, DateTime.Now){}

public MyManager(SomeOtherClass someOtherClass, DateTime someDate)
    myHelper = new MyHelper(someOtherClass, someDate, "some param");

public MyManager(YetAnotherClass yetAnotherClass)
    : this(yetAnotherClass, DateTime.Now){}

public MyManager(YetAnotherClass yetAnotherClass, DateTime someDate)
    myHelper = new MyHelper(yetAnotherClass, someDate, "some param");


Thanks everyone for your responses…they have been excellent!

Just thought I'd give an update on what I've ended up doing.

In order to address the null reference exception issue I've modified the additional constructors to take an ISomeManager.

At the moment my hands are tied when it comes to being allowed to refactor this particular class so I'll be flagging it as one on my todo list of classes to redesign when I have some spare time. At the moment I'm just glad I've been able to refactor the SomeManager class…it was just as huge and horrible as this MyManager class.

When I get around to redesigning MyManager I'll be looking for a way to extract the functionality into two or three different classes…or however many it takes to ensure SRP is followed.

Ultimately, I haven't come to the conclusion that there is a maximum number of constructors for any given class but I believe that in this particular instance I can create two or three classes each with two or three constructors each..

Best Answer

A class should do one thing and one thing only. If it has so many constructors it seems to be a tell tale sign that it's doing too many things.

Using multiple constructors to force the correct creation of instances of the object in a variety of circumstances but 9 seems like a lot. I would suspect there is an interface in there and a couple of implementations of the interface that could be dragged out. Each of those would likely have from one to a few constructors each relevant to their specialism.