• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

This Optional presentation should be Required

 
Saloon Keeper
Posts: 1606
52
Eclipse IDE Postgres Database C++ Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are many versions of this talk floating around given and recorded at different events.

This one is short and beautiful, it really gets to the heart of why it is there and when and how to and not to use it:
https://inside.java/2017/04/11/optional/

I think it should be Required for everyone!

It goes into why nulls are evil a bit right at the end...
 
Bartender
Posts: 363
44
Firefox Browser MySQL Database Java Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheat sheet:

• Rule #1: Never, ever, use null for an Optional variable or return value.
• Rule #2: Never use Optional.get() unless you can prove that the Optional is present.
• Rule #3: Prefer alternatives to Optional.isPresent() and Optional.get().
• Rule #4: It's generally a bad idea to create an Optional for the specific purpose of chaining methods from it to get a value.
• Rule #5: If an Optional chain has a nested Optional chain, or has an intermediate result of Optional<Optional<T>>, it's probably too complex.
• Rule #6: Avoid using Optional in fields, method parameters, and collections.
• Rule #7: Avoid using identity-sensitive operations on Optionals.
 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jj Roberts wrote:• Rule #2: Never use Optional.get() unless you can prove that the Optional is present.


Which is why a) I'm glad that Oracle added Optional.orElseThrow(), and b) I'm disappointed they didn't deprecate Optional.get(). The two are identical in implementation, which is a mistake - one should have delegated to the other. orElseThrow() is more verbose, true, but it's much clearer what happens if the Optional is empty.
 
Jesse Silverman
Saloon Keeper
Posts: 1606
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:

Jj Roberts wrote:• Rule #2: Never use Optional.get() unless you can prove that the Optional is present.


Which is why a) I'm glad that Oracle added Optional.orElseThrow(), and b) I'm disappointed they didn't deprecate Optional.get(). The two are identical in implementation, which is a mistake - one should have delegated to the other. orElseThrow() is more verbose, true, but it's much clearer what happens if the Optional is empty.



They seem to agree with you in presentations, their biggest regret...I guess officially marking something frequently used as @Deprecated is a big deal...

With probably that one blemish, I really like Optional now, not to the point of stupidly trying to replace every possible null with it everywhere, shown as an anti-pattern in detail in this and other similar presentations, but I'm a big fan.
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They marked lots of other things @Deprecated in Java9. It would be straighforward to mark get() @Deprecated: “Use orElseThrow() instead.”
 
Rob Spoor
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They document it, but don't soft-enforce it by deprecating it.

API Note:
The preferred alternative to this method is orElseThrow().

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic