Uncategorized

Blank Strings Through the Ages

As long as I have been writing Java code, I have inevitably run into cases where I need to validate a String argument. The most simple example is form field where the input is required. Let’s say I am writing validation code for an account creation component of my application. The component is as simple as it gets, enter a username and password on screen one, and the optionally enter a name, email, and location on screen two. The inputs on screen one are required, on screen two are they are all optional. If none of the inputs are filled in on screen two, we should not submit the form and just proceed with the application.

At different points through my career I would have chosen different solutions to help me with this code. Third party libs are an easy choice because they provide the the simple utilities I am looking for, and normally come with a lot more helpers that I can use. The links in this post will be to the latest release versions of these libraries, though I would have been using prior releases in the past.

Ten years ago I would have used the StringUtils class from Apache Commons Lang. Rather that having to write method to check for blank strings, I could just use StringUtils.isBlank method. From the Javadoc:

public static boolean isBlank(CharSequence cs)
     Checks if a CharSequence is whitespace, empty ("") or null.

Examples:

	StringUtils.isBlank(null) = true
	StringUtils.isBlank("") = true
	StringUtils.isBlank(" ") = true
	StringUtils.isBlank("bob") = false
	StringUtils.isBlank(" bob ") = false

Using this helper, the validation method can look like this (For simplicity,  I am ignoring any need to identify which parameters fail the validation for these examples):

This would have been about release 2.0 of Commons Lang, the most recent release was 3.4 in April of 2014 and is 435kb in size.

That did and probably would still work fine but not long after this time I started using the Spring Framework. That provided my with its own version of a StringUtils lib, so it did not make sense to also pull in Commons Lang for my Spring based projects. I started using the StringUtils.hasText method wherever I would have used isBlank. Essentially the opposite logic, hasText checks for the presence of text rather than the absence, which is what isBlank does. From the Javadoc:

public static boolean hasText(CharSequence str)
     Check whether the given CharSequence contains actual text.
     More specifically, this method returns true if the CharSequence is not null, its length is greater than 0, and it contains at least one non-whitespace character.

Examples:

	StringUtils.hasText(null) = false
	StringUtils.hasText("") = false
	StringUtils.hasText(" ") = false
	StringUtils.hasText("bob") = true
	StringUtils.hasText(" bob ") = true

Now my validation methods look like this:

The most recent stable release of the Spring Framework is 4.2.4. It was released in December of 2015 and is 1.1 MB in size.

Fast forward a few more years and I find myself doing Android work. Spring is not that important to me anymore, and I have discovered the Guava library. Guava gives me access to many String utility functions in the Strings class, but does not have its own version of the isBlank style method. The closest is the isNullOrEmpty method. From the Javadoc:

public static boolean isNullOrEmpty(@Nullable String string)
     Returns true if the given string is null or is the empty string.

The biggest difference is that blank strings, ones that are only whitespace, will return true from this method. Here is how the examples from the other two libraries methods would work for this one:

Examples:

	Strings.isNullOrEmpty(null) = true
	Strings.isNullOrEmpty("") = true
	Strings.isNullOrEmpty(" ") = false
	Strings.isNullOrEmpty("bob") = false
	Strings.isNullOrEmpty(" bob ") = false

So I have to make my own isBlank() using the nullToEmpty method Guava Strings provides. That method is a nice way to protect against nulls by standardizing them to empty strings. Now my validation methods can be the same as when I was using Commons Lang. But Guava gives you so much more to play with. Using a Joiner, my methods can look like this:

The Joiner takes a list of Strings and joins them on “”, and passes the resulting String to isBlank for validation.  If it’s blank, the user submitted no data.

I would highly recommend taking a look at the the Guava library and learning about what it has to offer. Besides the String related helpers, we make a lot of use of the Collections related classes.

The most recent stable release of Guava is 19.0. It was released in December of 2015 and is 2.2 MB in size. This is by far the largest lib of the three I have discussed, so if you are going to use Guava in you Android project be sure to turn on Proguard to shrink the lib down to just the things you need.

Keven Schulz has some great posts about using Proguard for Android with gradle and maintains a Github repo with example configuration for Guava and other popular libs.

I suspect I will be using Guava for my String validation and other helper functions for the foreseeable future. Or at least until the next better thing comes along.

Want to work with Guava and other fun stuff?   We’re hiring!

Tags:

Discussion

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 )

Google+ photo

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

Connecting to %s