Duplicate Code? Chain Constructors

Table of contents
Reading Time: 2 minutes

After the ‘Introduction of Null Object’ and the ‘Replace One/Many Distinctions with Composite’ let us do an easy one this time. It is easy but of course it is present in a lot of code samples else we would not be discussing it here. If there are a lot of constructors which have duplicate code then see if there is a reasonable possibility to chain them.

By chaining we mean that one constructor calls the other and so on.

For example is one of our projects for a fraud management system using complex event processing we had an alert type called ScoreAlert

Following are 3 of the many constructors that it had

[sourcecode language=”java”]
public ScoreAlert(ScoreType type, float average, int rating, Date expiry) {
this.type = type;
this.average=average;
this.rating = rating;
this.expiry = expiry;
}
public ScoreAlert( ScoreType type, float average, int rating, Date expiry, Date maturity) {
this.level=AlertLevel.HIGH;
this.type = type;
this.average=average;
this.rating = rating;
this.expiry = expiry;
this.maturity = maturity;
}
public ScoreAlert(AlertLevel level, ScoreType type, float average, int rating, Date expiry, Date maturity) {
this.level = level;
this.type = type;
this.average=average;
this.rating = rating;
this.expiry = expiry;
this.maturity = maturity;
}
[/sourcecode]

now in order to remove duplication one of the ways is to chain the constructors such that one constructor calls the other.

After chaining we have something like this,

[sourcecode language=”java”]
public ScoreAlert(ScoreType type, float average, int rating, Date expiry) {
this(null, type,average, rating, expiry, null)
}

public ScoreAlert(ScoreType type, float average, int rating, Date expiry) {
this(AlertLevel.HIGH, type,average, rating, expiry, maturity)
}

public ScoreAlert(AlertLevel level, ScoreType type, float average, int rating, Date expiry, Date maturity) {
this.level = level;
this.type = type;
this.average=average;
this.rating = rating;
this.expiry = expiry;
this.maturity = maturity;
}
[/sourcecode]

This makes it easier to add new attributes to the class, which would otherwise have to be repeated endlessly in all the constructors. Whenever you have a code like this, We often accomplish this refactor it with constructor chaining.
The concept is simple, specific constructors call more general-purpose constructors until a final constructor is reached. If you have one constructor at the end of every chain, it is a catch-all constructor because it handles every constructor call. A catch-all constructor often accepts more parameters than other constructors.

Another way to handle the problem of multiple constructors is with the use of Builder pattern. Any idea how to do that? We will come to that soon.

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading