Cleaner Code with Guava Optionals and Preconditions

June 6, 2012 | 3 min Read

Working towards clean code is a priority for me when I develop software. For some time I’ve been using Google Guava for nearly all my projects. The reason is simple. Guava provides great facilities to beautify my code. Today I want to show you how I use Preconditions to avoid unnecessary if/throw statements and Optionals to improve my code semantics.

[ Need expert advice for your project? Our Developer Support is here to resolve your questions. | Find more tips on clean code on the Software Craftsmanship page. ]

Preconditions are not new. Apache Commons had similar functionality but not as clean as Guava’s solution. Preconditions are used for checking method arguments, states, etc. When a condition is false the Precondition will throw the expected exception. With expected I mean the following: when checking a state you can use Preconditions.checkState( condition ). If the condition is false it will throw an IllegalStateException. The same is true for other Preconditions like checkArgument which throws an IllegalArgumentException. Of course, working with Preconditions only makes sense when using static Imports. Let’s take a look at an example.

Given is a method which takes a List as an argument. When this method is invoked we want to check that the list is not null and it is not empty. A plain Java solution can look like this:

public void doSomething( List list ) {
  if( list == null ) {
    throw new IllegalArgumentException( "List must not be null" );
  }
  if( list.isEmpty() ) {
    throw new IllegalArgumentException( "List must not be empty" );
  }
  doSomethingMore( list );
}

When using Guava’s Preconditions the amount of code is reduced significantly. The solution looks like the one below.

public void doSomething( List list ) {
  checkArgument( list != null, "List must not be null" );
  checkArgument( !list.isEmpty(), "List must not be empty" );
  doSomethingMore( list );
}

This is an improvement for sure. But this approach becomes really sexy when it’s combined with Guava’s Optionals. Optionals are a concept designed to avoid the sick null concept (read this for understanding why I call it “sick”). They are basically a container for an object to avoid nullable references. For example, null is used most of the time to check if an object is there or not. And if it’s not there, an NPE occurs. The result is code like the following:

public void doSomething() {
  if( this.field == null ) {
    throw new IllegalStateException( "Field is not initialized" );
  }
  doSomethingMore();
}

Instead of saving the object directly as a field I often use an Optional. This avoids NPEs in my code and gives it better semantics. The example above is transformed into the one below when it’s combined with Preconditions:

public void doSomething() {
  checkState( field.isPresent(), "Argument is not initialized" );
  doSomethingMore();
}

In the end it’s up to you to decide if the code is better/cleaner with Optionals and Preconditions. From my point of view it’s definitely more speakable. I would love to hear your opinion about it in a comment ;).