Chain constructors refactoring in C#

In Refactoring to Patterns, Joshua Kerievsky describes the chain contructor refactoring that can be used to remove duplicate code when a class has many contructors.

I've been coding in C# for a while now and I never figured out how to chain constructors.

As it turns out, its extremely simple. You just make the call by adding a colon and then the this keyword after the constructor parameters and pass along the parameters and default values you need.

If I use Joshua's exemple it would look like this:

public class Loan
{

private readonly float notionalField; private readonly float outstandingField;  

    private readonly int ratingField;
    private readonly DateTime expiryField;
    private readonly DateTime maturityField;

public Loan(float notional, float  outstanding, int rating, DateTime expiry)  

    {
        this.strategyField = new TermTOC();
        this.notionalField = notional;
        this.outstandingField = outstanding;
        this.ratingField = rating;
        this.expiryField = expiry;
    }

    public Loan(float notional, float  outstanding, int rating, DateTime expiry, DateTime maturity)
{
        this.strategyField = new RevolvingTermTOC();
        this.notionalField = notional;
        this.outstandingField = outstanding;
  this.ratingField = rating;
        this.expiryField = expiry;
        this.maturityField = maturity;
    }       

public Loan(CapitalStrategy strategy, float notional, float  outstanding, int rating, DateTime expiry, DateTime maturity)  
{  

        this.strategyField = strategy;
        this.notionalField = notional;
        this.outstandingField = outstanding;
        this.ratingField = rating;
        this.expiryField = expiry;
        this.maturityField = maturity;
    }

}

Refactored to:

public class Loan

{

private readonly float notionalField;

private readonly float outstandingField;

private readonly int ratingField;

private readonly DateTime expiryField;

private readonly DateTime maturityField;

public Loan(float notional, float outstanding, int rating, DateTime expiry)

    :this(new TermTOC(), notional, outstanding, rating, expiry, DateTime.MinValue)

{

}

public Loan(float notional, float outstanding, int rating, DateTime expiry, DateTime maturity)

    :this(new RevolvingTermTOC(), notional, outstanding, rating, expiry, maturity)

{

}

public Loan(CapitalStrategy strategy, float notional, float outstanding, int rating, DateTime expiry, DateTime maturity)

{

    this.strategyField = strategy;

    this.notionalField = notional;

    this.outstandingField = outstanding;

    this.ratingField = rating;

    this.expiryField = expiry;

    this.maturityField = maturity;

}

}

This article is part of the GWB Archives. Original Author: Simon Laroche

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.