Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Optional vs null  RSS feed

 
Sean Corfield
Ranch Hand
Posts: 312
14
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was very pleased to see java.util.Optional added, along with java.util.function.Supplier and java.util.function.Consumer, to provide the possibility of a "null-free" work environment.

However, given how prevalent null return values are in existing Java code, I really wonder how practical it is to attempt to use Optional everywhere in your own code to avoid null while still interacting with existing Java libraries that happily accept arguments that are value-or-null?

It seems like you'd end up with a lot of x.orElse(null), y.orElse(null) expressions in calls to "legacy" Java code, or a fair bit of flatMap and map calls.

For example, suppose you have two functions that return Long or null and you want to add the results if they're both non-null (or produce an empty result):

I will confess it took me a while to figure out the right combination of map and flatMap to make that work!

Do you have any recommendations for mixing Optional with "traditional" Java code?

Also, do you have suggestions for additional reading material around the concept of Optional (and friendly material for the Maybe Monad which this essentially provides)?
 
Sean Corfield
Ranch Hand
Posts: 312
14
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative to that code would be:

Although now you've "polluted" your nice, clean functional code with null checks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15860
80
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Corfield wrote:However, given how prevalent null return values are in existing Java code, I really wonder how practical it is to attempt to use Optional everywhere in your own code to avoid null while still interacting with existing Java libraries that happily accept arguments that are value-or-null?

This is indeed going to be hard and probably Optional is never going to be standard idiom in Java, or at least it's going to take years before everyone is using it everywhere. Ofcourse even the standard API uses null everywhere, and the standard API is never going to change because of backward compatibility.

I know the concept mainly from Scala (where it's called Option / Some / None). Scala also has null (unfortunately) but using null in Scala is really non-idiomatic; you should really use it only for interfacing with Java code.

In Java, since any reference can be null, you never know by looking at a method declaration whether it ever might return null or not. That means you'll need to add null checks to your code even though they might not be necessary, or if you don't check you might get a NullPointerException.

Suppose that null didn't exist, then you'd immediately see by looking at the method declaration if it could ever return "no value" - the method would return an Optional instead of some other type. So, in that way, using Optional helps you prevent bugs.

Another advantage is that you can use monadic methods such as filter, map and flatMap on an Optional, which can make your code more concise (you wouldn't need so many if-blocks).

Unfortunately there is no easy way to make it work realy smoothly with other Java code that uses nulls. You could ofcourse make wrappers for methods that return nulls:

In Scala you have a very nice for-syntax to make this a lot more readable:

 
Sean Corfield
Ranch Hand
Posts: 312
14
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:This is indeed going to be hard and probably Optional is never going to be standard idiom in Java

Yes, my worry is that Java will become an increasingly confusing mixture of styles. It's one of the things that put me off Scala (after using it for a year or so in production). The tension between traditional imperative APIs and state-based classes on one hand, and functional APIs and immutable data on the other can make Scala code hard to read and difficult to work with (due to a lack of consistency).
I know the concept mainly from Scala (where it's called Option / Some / None).

And Maybe / Just / Nothing in Haskell. It's a nice idiom in a strongly-typed language for dealing with values that may be missing.

I'm mostly working in Clojure these days which takes completely the opposite approach: it's idiomatic that nil is treated as a false value, and there are easy ways to chain function calls that stop as soon as you get nil. The upside is very concise code that reads easily along the "happy path" but the downside is you still trip over NPEs fairly regularly... Unfortunately, Clojure doesn't provide a good idiom for this particular problem out of the box. The best you can do is:

Suppose that null didn't exist, then you'd immediately see by looking at the method declaration if it could ever return "no value" - the method would return an Optional instead of some other type. So, in that way, using Optional helps you prevent bugs.

Agreed. It really lets the type system help you in ways it can't in a world full of null values!
Unfortunately there is no easy way to make it work realy smoothly with other Java code that uses nulls. You could ofcourse make wrappers for methods that return nulls:

Good point. I wonder if we'll see third party libraries spring up that provide such wrappers for standard library classes? I wonder if Java will add such libraries as standard in the future?
In Scala you have a very nice for-syntax to make this a lot more readable:

Yes, Scala's way of providing syntactic sugar for flatMap and map calls is very nice.
 
Sean Corfield
Ranch Hand
Posts: 312
14
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard, would love to hear your thoughts on this?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!