Okay, I've got a question about handling nulls. This question can be heavily based upon opinion, therefore I'm going to ask about pro's and cons.
Let's say I've got a function that can return null, or a JSONArray. I always want a JSONArray, so I want it to create an empty one if the function's result is null.
Currently I've got the following approach:
jsonArray = jsonArray==null?new JSONArray():jsonArray;
I like this approach as it's one line, and pretty clear what it does. This does lead me to the question though, is this efficient? I've got the idea that now it'll execute jsonArray = jsonArray
while not needed. Though this does seem to save one jump you would have with an if (jsonArray == null)
What are the advantages of different ways of handling nulls?
Have you taken a look at Java 8's
Optional
class? This is an object wrapper that lets you handle null in a functional way.For example, if you have a method
public JSONArray getArray()
that you want to always return something other than null, you can use your code. Using Optional, it would change to this:In cases where jsonArray is null, the optional will be empty; in cases where it's not null, it will contain jsonArray.
You can then replace null checks with behaviour dictated by the optional. Instead of
you replace it with
This means you don't need to create empty JSONArrays, or Lists, or Sets, or Strings, or whatever. In cases where the wrapped object is actually null, a singleton Optional is returned from
Optional.ofNullable
, further reducing overhead.If you still want to take a classic approach, that's possible too. Since
if (option == null)
should always evaluate tofalse
(if you return null instead of an Optional, you kind of miss the point!), you woud useif (option.isPresent())
.If you're not using Java 8, you can either write your own Optional or use a third-party library such as Guava.
EDIT: Non-Java 8 solutions
Solution 1
Use something like Guava - take a look at http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html
Solution 2
Write your own! In this implementation,
Supplier
,Consumer
andPredicate
are interfaces that return, accept or test an object.