Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
}

Update:

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..

like image 806
mezoid Avatar asked Jan 29 '09 06:01

mezoid


People also ask

How many constructors can a class have?

You can have 65535 constructors in a class(According to Oracle docs). But IMPORTANTLY keep this in your mind.

Can class have 2 constructors?

A class can have multiple constructors that assign the fields in different ways. Sometimes it's beneficial to specify every aspect of an object's data by assigning parameters to the fields, but other times it might be appropriate to define only one or a few.

How many constructors can a class have in C?

In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments.

What is the minimum number of constructors A class can have?

Within a class, you can create only one static constructor. A constructor doesn't have any return type, not even void. A static constructor cannot be a parameterized constructor. A class can have any number of constructors.


2 Answers

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.

like image 188
sh1mmer Avatar answered Oct 12 '22 19:10

sh1mmer


As little as possible,
As many as necessary.

like image 23
Przemek Avatar answered Oct 12 '22 17:10

Przemek