Use object Default values carefully

Recently i have faced an implementation decision whether using default values in a Object when input parameters are not validin the consturctor. Let see an example for visualize first


public class DefaultValues {

public static final String DEFAULT_OBJ_A = "_default_obj_A";

public static final String DEFAULT_OBJ_B = "_default_obj_B";

private String objA;

private String objB;

public DefaultValues (){
If (objA == null){
objA = DEFAULT_OBJ_A;
}
if (objB == null){
objB = DEFAULT_OBJ_B;
}

public void setObjA(String objA) {
if (some constraint fulfilled){
this.objA = objA;
}
}

public void setObjB(String objB){
if (some constraint fulfilled){
this.objB = objB;
}
}

public void invoke(){
// Do operation on objA, objB.
}

}

In the above example, when objA or objB is NULL, they use default values in the constructor for keeping the object valid for the rest. Thus calling method invoke will not throw null point exception. Originally my idea is wanted to simplify client/devleoper code as they don’t need to invoke all setter method before using the object safety.

Failed in the developer perspective

This pattern fail because the object execute under unexpected behavior in the client/developer perspective. Developer may not know the object created are using default values because of constraint violation during setting method (in the above example). They may believe setup of object is correct and thus puzzled at why the internal value of object does not expect.

Resolution

In simple, you can add comment on each method about the value constraint for client/developer. Or a better approach, using Default and Complete constructor with appropriate guard clause.

The default constructor takes no argument/parameter. It initializes it’s internal state to default value. Usually it delegate to the complete constructor.

The complete constructor takes number of parameter WHICH made the object valid and consistent, client/developer SHOULD expect no more setter are required to invoke after calling this constructor in normal case.

Let review our example after applying this pattern:


public class DefaultValues {

public static final String DEFAULT_OBJ_A = "_default_obj_A";

public static final String DEFAULT_OBJ_B = "_default_obj_B";

private final String objA;

private final String objB;

/*
* Commont about the default values.
*/
public DefaultValues (){
this(DEFAULT_OBJ_A, DEFAULT_OBJ_B);
}

public DefaultValues(String objA, String objB){
if (objA NOT fulfilled some constraint){
throw new IllegalArgumentException("some thing about objA");
}
if (objB NOT fulfilled some constraint){
throw new IllegalArgumentException("some thing about objB");
}

this.objA = objA;
this.objB = objB;
}

public void invoke(){
// Do operation on objA, objB.
}

}

Now the classes DefaultValues has none setter. Initialized parameters are passed in the second constructor. This constructor validates all parameter, throwing IllegalArgumentException when any one does not satisfy the constraint. I liked this pattern because it blocks developer in the object creation and send out a message “You are making a invalid object, please fix your code)” to them prior to throwing other exception during execution of object behavior with others.

Default values are applied only when using default constructor. It delegate the complete constructor so that you don’t need to maintain two code set of object initailization.

Conclusion

The default values pattern still offer benefits for developer to simplify but have to use it carefully to provide communicate and readable for your audience by introducting default and complete consturctor with guard clause.

One Response to “Use object Default values carefully”

  1. ronnie Says:

    I like this approach, too. It serve the purpose of providing a default but in turns, safe guard the changing of this value for the not-knowing developer.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.