天天看點

Why chaining constructors is bad.

Why chaining constructors is bad

I personally find that this code snippet demonstrating constructor chaining is hard to read.

This code makes it very hard to identify which of the many constructors is the "real" one:  the constructor that does the real job and that I should call if I add a new overloaded constructor.

This is why I use the following two rules when I write overloaded constructors:

  • Have all your constructors converge toward one unique private method that you will name consistently throughout your code base (e.g. init ) and that will contain the entirety of all the parameters that your constructors may receive.
  • Don't add any logic to the constructors, all they should do is invoke init with the right parameters and null for default ones.

Here is an example, from worst:

public class Person {
  private String m_firstName = null;
  private String m_lastName = null;
  private long m_ssn = 0;

  public Person(String lastName) {
    m_firstName = null;
    m_lastName = lastName;
    m_ssn = getSsn();
  }

  public Person(String firstName, String lastName) {
    m_firstName = firstName;
    m_lastName = lastName;
  }
      

... to better:

public Person(String lastName) {
    this(null, lastName);
    m_ssn = getSsn();
  }

  public Person(String firstName, String lastName) {
    m_firstName = firstName;
    m_lastName = lastName;
  }
      

... to best:

public Person(String lastName) {
    init(null, lastName);
  }

  public Person(String firstName, String lastName) {
    init(firstName, lastName);
  }

  private void init(String firstName, String lastName) {
    m_firstName = null;
    m_lastName = lastName;
    if (null == m_firstName) {
      m_ssn = getSsn();
    }
  }
      

Here is why the latter form is preferable:

  • It doesn't duplicate any code.
  • It makes the initialization rules clear : "if no firstName was given, then ssn gets initialized".
  • The signature of init gives a good indication of the various parameters this class needs to be initialized, while a multitude of overloaded constructors obscures this fact.

TrackBack URL for this entry:

http://beust.com/weblog/mt-tb.cgi/205

Listed below are links to weblogs that reference 'Why chaining constructors is bad' from Otaku, Cedric's weblog.