WhiningBuilder
From APIDesign
One problem with ChameleonBuilder is that the change of the return type works only for a single essential attribute. Can we modify the builder pattern to work with multiple essential attributes? Another problem is the reported error - it doesn't push the user towards proper fix. Can we improve that and encourage cluelessness in users of our builders?
Contents |
Multiple Throws
Java allows only a single return value - yet, it allows multiple throws - and throwing an exception from a method invocation is a kind of a return value as well. As the BuilderWithConditionalException shows it is possible use a generic type parameter to control whether the exception thrown from the build() method is checked exception or unchecked one. Let's do the same with multiple throws for two essential attributes (name and mimeType):
public final class Builder<MissingName extends Exception,MissingMIMEType extends Exception> { public Source build() throws MissingName, MissingMIMEType { if (this.name == null) { throw (MissingName)new Exception("name missing"); } if (this.mimetype == null) { throw (MissingMIMEType)new Exception("MIME type missing"); } // create and return source; } public static Builder<Exception,Exception> newFromText(String content) { // create new builder without name and MIME type return theBuilder; } public Builder<RuntimeException,MissingMIMEType> name(String name) { // specifying name this.name = name; // keeps the "mime type" exception unchanged but // turns the "name" exception into runtime one return (Builder<RuntimeException,MissingMIMEType>)this; } public Builder<MissingName,RuntimeException> mimeType(String mimeType) { // specifying MIME Type this.mimeType = mimeType; // keeps the "name" exception unchanged but // turns the "mime type" exception into runtime one return (Builder<MissingName,RuntimeException>)this; } }
Please note that MissingName and MissingMIMEType are generics type parameters - e.g. they aren't real exceptions, just specifications/place holders for the actual Exception subclass. This gives us early compile time warning as in ChameleonBuilder. When one tries to compile:
private static Source buildMySource() { return Source.newFromText("function hi() { print('Hi'; }"). build(); }
one gets a compilation error, as the build method throws a checked exception. Even if one specifies name, the error still remains. Only if both name and mimeType are specified the checked exceptions go away (as they are replaced by runtime exceptions).
The runtime and compile time behavior work in orchestration: if a checked exception is reported - it will also be thrown. E.g. trying to put a try/catch block around the the build() call makes no sense. If the exception is declared, it will really be generated.
Explaining Why
One problem of the ChameleonBuilder lies in insufficient reporting of the proper error when an essential attribute isn't specified. That is not that great in the previous example either - one only gets an Exception. It is not that easy to find the real reason for the failure. But that can be easily improved:
public final class MissingNameException extends Exception { } public final class MissingMIMETypeException extends Exception { } // and change the method public static Builder<MissingNameException, MissingMIMETypeException> newFromText(String content) { // uninitialized builder return theBuilder; }
with this change the user always needs to handle the specific exception. If name isn't specified, one needs to catch MissingNameException. If mimeType attribute isn't specified, one needs to catch MissingMIMETypeException. Both exception classes may have proper Javadoc explaining that rather than catching them, it is advised to set the corresponding attribute.
With such support the programmers should be able to understand what is wrong and properly recover from their mistakes while coding in an editor, without the need to execute their code.
Why Does It Work?
The ChameleonBuilder works only with a single essential attribute because each attribute can influence only a single generic type. The reason why this pattern works is that one method can have unlimited number of of Exceptions in its throws clause. Depending on the number of essential attributes one defines N type parameters:
public final class Builder<E1 extends Exception, E2, extends Exception, ... En extends Exception> { public Result build() throws E1, E2, E3, ..., En { // check the attributes and return createdResult; } }
Each of the E type parameters is independent representing single condition: was the associated attribute set or not? All of the conditions need to be satisfied otherwise the build() method keeps throwing a checked exception. This kind of or-behavior is impossible to achieve with a single return type.
Drawbacks
The biggest strength of this pattern - e.g. N independent condition is also the biggest flaw: Imagine clueless users seeing the signature with n-exception type parameters! They would be scared. On the other hand, the typical usage of the builder pattern doesn't expose the type variables at all:
Source src = Builder.newFromText("content"). attr1("value"). attr2(42). ... attrN(3.14). build();
The code looks OK in spite of enormous number number of generic type parameters. Most of the time the generics types remain invisible.
Evolution
An interesting question from an evolution point of view: what happens when we need to add new essential attribute?
Obviously, if we want 100% BackwardCompatiblity then we should add new builder class:
public final class Builder2<E1,E2,E3 extends Exception> extends Builder<E1,E2> { }
this class can add new type parameters, overwrite the already existing methods of Builder to return Builder2, etc.
On the other hand, if 99% BackwardCompatiblity is enough, we could add new generics type parameter into already existing Builder class. That is of course not source compatible - but thanks to erasure of generic types, it is binary compatible and that is what matters most in Java. Moreover the common usage of the builder doesn't expose the generics in code at all - so extending the number of generic types parameters should be mostly harmless.
Summary
Using checked exception to track essential attributes that yet need to be provided is a nice and no-overhead way to co-ordinate the requested runtime behavior with compiler and let users know at runtime that their code is wrong and needs additional tweaks.