• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Prof Horstmann's Take on the Optional class.

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, I have mentioned before that Urma Fusco and Mycroft think the Optional class should be used to obviate nulls. I think they would use Optionals everywhere if given half a chance. Do you agree with them?
 
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, yes, I think Optional is good, and you should use it instead of returning null when a method has no result.

Conversely, when someone gives you an Optional, then don't call isPresent and get. That's no safer than a possibly null pointer. Instead, use orElse, ifPresent, map, or flatMap to process the wrapped value.

For example,



Cheers,

Cay
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cay Horstmann wrote:Conversely, when someone gives you an Optional, then don't call isPresent and get. That's no safer than a possibly null pointer. Instead, use orElse, ifPresent, map, or flatMap to process the wrapped value.


I totally agree, being a proponent of tell, don't ask, but I can't help feeling that Optional is a bit of a kludge.

Winston
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's a big shame that ifPresent() doesn't return an object on which I can call orElse(), because I've found a few scenarios in which I had to resort to calling isPresent() anyway.
 
Cay Horstmann
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sometimes happens to me too. I'd like something like an ifElse with two lambdas.

Stephan van Hulst wrote:I think it's a big shame that ifPresent() doesn't return an object on which I can call orElse(), because I've found a few scenarios in which I had to resort to calling isPresent() anyway.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic