Navigation

RSS 2.0 New Entries Syndication Feed Atom 0.3 New Entries Syndication Feed

Show blog menu v

 

General

Use it

Documentation

Support

Sibling projects

RIFE powered

Valid XHTML 1.0 Transitional

Valid CSS!

Blogs : Archives

avatar
< Rarely lauched so much with a site   The deceitful safety of coverage tools >
Minimal constructors and chainable setters

I used to write a collection of constructors when a class could be instantiated in a variety of ways. An important amount of time was spent selecting sensible combinations of constructor arguments to make them easy to use and as readable as possible.

Recently however, I started to work differently since I noticed that complex constructors always end up becoming mysterious. Without looking at the javadocs it's often difficult to understand what each argument stands for.

Consider this example class:

public class Constraint
{
    private String     mName = null;
    private boolean    mNotNull = false;
    private boolean    mNotEmpty = false;
    
    public Constraint(String name, boolean notNull, boolean notEmpty)
    {
        mName = name;
        mNotNull = notNull;
        mNotEmpty = notEmpty;
    }
}

One would create an instance of Constraint like this:

Constraint constraint = new Constraint("name", true, false);

When you re-read this statement afterwards, you immediately start to wonder which properties the true and false relate to. Was is first notNull and then notEmpty, or was it the other way around?

Now consider this alternative implementation:

public class Constraint
{
    private String     mName = null;
    private boolean    mNotNull = false;
    private boolean    mNotEmpty = false;
    
    public Constraint(String name)
    {
        mName = name;
    }
    
    public Constraint notNull(boolean state)
    {
        mNotNull = state;
        
        return this;
    }
    
    public Constraint notEmpty(boolean state)
    {
        mNotEmpty = state;
        
        return this;
    }
}

To create a instance of Constraint you would now do:

Constraint constraint = new Constraint("name").notNull(true).notEmpty(false);

This is immediately readable without any doubt or confusion. You also don't lose the benefit of being able to instantiate and setup an instance in one line.
Therefore, I now try to create as little constructors as possible. Just the bare minimum is needed to ensure that each instance receives enough information to be initialized in a usable state. All the other optional setup behaviour can be achieved through chainable setters.

You might wonder why I didn't simply change the return type of a regular setter from void to the class type. This is because the javabean spec mandates a void return type on setters. If you don't respect this, the setter will not be found through bean introspection.

posted by Geert Bevin in Java on Dec 16, 2003 5:06 AM : 2 comments [permalink]
 

Comments

Re: Minimal constructors and chainable setters

Nice! But how can you handle errors, assuming there's a more complex problem?

Also, the role of contructors was to assure that a particular object IS in a state or another. By using this notation, you open up to state corruptions, since members of your team might not know which pseudo-setters to use to get to a known state.

Re: Minimal constructors and chainable setters

In exact the same way as with a bloated constructor.

You're in fact more flexible since the minimal constructor always creates a minimally set up instance. The chainable setters can then throw exceptions in case of errors if they do more elaborate stuff.

Anyway, it's not a good idea to perform logic that can fail in a regular constructor for exactly the reason you mention above. So you can use the same techniques as before if you want (factory method, setup method, ...).

Note that I never said that the constructor leaves the instance in a disfunctional state. That's a no-no. It leaves it is the least configured functional state.

Add a new comment

:) ;)
=) :-)
:'( :(
:/ :D
:| :p
:o 8)
Your email address will not be displayed at anytime on any page.
Only provide your email address if you'd like updates on this entry
and it's comments by email.
Please answer this simple math question:
17 + 11 = 
 
 
  

Manage subscription

Remove email:
 

< Rarely lauched so much with a site   The deceitful safety of coverage tools >
 
 
 
Google
rifers.org web