Use factory methods instead of overloaded constructors

If a class can be created and initialized in different ways or by using different kind of values you may often see overloaded constructors. This is a standard way to implement such an object creation but it has a major disadvantage. The constructor itself is not self-explaining.  Very often you need a good code documentation to understand the behavior of the different constructors.

A possible solution to create cleaner code may be to use factory methods instead of the overloaded constructors. These factory methods should have names explaining their use case. This will make the code clean and an additional documentation may be obsolete.

Within the .NET Framework you will find some classes, following this guideline. For example the TimeSpan class. A TimeSpan class instance may be created in different ways. It would be possibleto use a combination of concrete values for a duration e.g. year, month, days and so on or you may use ticks, seconds or minutes as single values.

If you use overloaded constructors you may implement the following ones for the TimeSpan class.

TimeSpan(double ticks)

TimeSpan(int years, int months, int days)

TimeSpan(int hours, int minutes, int seconds, int milliseconds)

TimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)

In such a case it is necessary to write a good documentation and explain all functions. For example if you want to create a TimeSpan of one month, will it be allowed to call the following constructors?





Maybe the class will support all these ways to create the instance. But you will have two major disadvantages: To write the code you have to read the documentation and the created code is hard to read.

An alternative way to implement the TimeSpan class is by using factory methods instead of constructors. For example take a look at the following methods.

FromMonths(int months)

FromMinutes(int minutes)

FromTicks(int ticks)

The factory methods will create TimeSpan instances too. But these methods will remove the disadvantages shown above. They are clean and self-explaining and the created code is easy to read. The above task to create a TimeSpan of one month will be implemented following:

TimeSpan duration = TimeSpan.FromMonths(1);

Such factory methods will make the overloaded constructors obsolete. Therefore you should remove them or make them private. In summary you should use the following guideline:

When constructors are overloaded, use static factory methods with names that describe the arguments and make the corresponding constructors private.

Dieser Beitrag wurde unter .NET, C#, Clean Code veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s