Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 8 Optional

 
Sean Corfield
Ranch Hand
Posts: 302
10
Clojure Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard,

Looking at your book's table of contents, I see you cover the new Optional class briefly under Streams.

Other languages use similar types (Maybe, Option) as a way to program safely without worrying about null pointers. How realistic do you think it is for Java developers to adopt Optional in practice and move away from using null, given all the existing library methods that return null as a matter of course?
 
Richard Reese
author
Ranch Hand
Posts: 84
5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean,

I wouldn’t expect existing libraries to be rewritten using the Optional class. It can be used to create better libraries in the future. One of the nice things about using the Optional class is that it will force you to deal with null values. You will have to actively consider the possibility that a null result may be encounter. This will make your code more stable. Of course this comes with a cost: more time spent developing your code. The payback is arguably better code.

 
Sean Corfield
Ranch Hand
Posts: 302
10
Clojure Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a specific example, when writing new code, do you think it's worth using Optional.ofNullable( legacyLibraryCall( some, args ) ) to wrap old (possibly null-returning) library calls so that your code can be null-free? Or do you think that it's not worth the effort at that level?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic