Over a million developers have joined DZone.

A Consistent Way of Doing Null Checks and Empty Checks on Objects

· Java Zone

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

I have always found null checks and checks for "empty" values (mostly applicable for collections or maps) spread all throughout the code base. Hence I found a neat and elegant way to handle this by providing a utility class that contains the following null checks:

Utils Class: 
/**
	 * This method returns true if the collection is null or is empty.
	 * @param collection
	 * @return true | false
	 */
	public static boolean isEmpty( Collection<?> collection ){
		if( collection == null || collection.isEmpty() ){
			return true;
		}
		return false;
	}

	/**
	 * This method returns true of the map is null or is empty.
	 * @param map
	 * @return true | false
	 */
	public static boolean isEmpty( Map<?, ?> map ){
		if( map == null || map.isEmpty() ){
			return true;
		}
		return false;
	}

	/**
	 * This method returns true if the objet is null.
	 * @param object
	 * @return true | false
	 */
	public static boolean isEmpty( Object object ){
		if( object == null ){
			return true;
		}
		return false;
	}

	/**
	 * This method returns true if the input array is null or its length is zero.
	 * @param array
	 * @return true | false
	 */
	public static boolean isEmpty( Object[] array ){
		if( array == null || array.length == 0 ){
			return true;
		}
		return false;
	}

	/**
	 * This method returns true if the input string is null or its length is zero.
	 * @param string
	 * @return true | false
	 */
	public static boolean isEmpty( String string ){
		if( string == null || string.trim().length() == 0 ){
			return true;
		}
		return false;
	}

By having these methods as static utility methods, the client code looks neat and you now have a consistent way of doing null checks.

E.g. Client code that needs to do a null check would call Utils.isEmpty(object) or call any other specific overloaded method.

Advantages:

  1. Consistency in the null check code.
  2. Avoids littering of code with individual checks for null and emptiness by doing them both in one go. (99% of the cases you want to do both null checks and check for emptiness but rarely one  in isolation).
  3. Easy to read and comprehend.


Disadvantage:

This one is not really a disadvantage but rather something that applies to overloaded method invocation in Java.

If you have code as below, it will not compile.

Utils.isEmpty(null); //This line doesn't compile. It's like testing if(null==null). Just crazy! 

I feel it's worth taking this approach since you might never have to write code as above in real life :)

I hope this helps and makes your code more cleaner and reveals intentions more clearly. I would like to hear from the readers of this site on what they think of the above approach.

The Java Zone is brought to you in partnership with JetBrains.  Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}