Optional - The Mother of All Bikesheds by Stuart Marks

By: Devoxx

151   1   11674

Uploaded on 11/11/2016

The Optional class was introduced in Java 8 in order to solve a narrow but frequently occurring problem: what to return if you have nothing to return. It is a single class with less than 20 methods, but it turns out to have been one of the most controversial and most misunderstood APIs, having generated several "centithreads" of discussion on OpenJDK mailing lists. Indeed, Brian Goetz (Oracle's Java Language and Libraries Architect) has mentioned that one aspect of Optional was his biggest mistake in Java 8. Optional is also one of the more widely misused APIs. A brief survey of OpenJDK code revealed several embarrassing examples of Optional usage, and expert Java programmers have admitted to not making the most of this API.

This session covers the history and rationale of Optional, provides recommendations and examples of proper and effective usage, shows several antipatterns and code smells and how to fix them, and finally describes some current and proposed work on Optional for Java 9.

Stuart Marks is a Principal Member of Technical Staff in the Java Platform Group at Oracle. He is currently working on a variety of JDK core libraries projects, including Collections, Lambda, and Streams, as well as improving test quality and performance. As his alter ego "Dr Deprecator" he also works on the Java SE deprecation mechanism. He has previously worked on JavaFX and Java ME at Sun Microsystems. He has over twenty years of software platform product development experience in the areas of window systems, interactive graphics, and mobile and embedded systems. Stuart holds a Master's degree in Computer Science and a Bachelor's degree in Electrical Engineering from Stanford University.


Comments (11):

By anonymous    2017-09-20

Optional was designed to be used as a return type for methods that can't always return a value, like for example getFirstElementOfAList(), which can't return anything else if the list is empty: returning null would be less clear than returning an empty Optional, and could mean that the first element is null, and not that there is no first element.

The designers don't intend Optional to be used as a convenience construct to chain methods. See the presentation by Stuart Marks (who works on the JDK), and especially what he says at the 28th minute:

It's generally a bad idea to create an Optional for the specific purpose of chaining methods [...]

You're of course free to disrespect the intended design and the best practices, but your colleagues might also be free to hate you for that :-)

Note that the two snippets you posted don't do the same thing. The first one does nothing if the user name is null, whereas the second one sets the user to null (which might in turn throw an exception or overwrite the previous value)

I would just use (assuming what you want is the first version):

if (userName != null) { 

which is extremely clear, doesn't abuse Optional, and doesn't create an Optional instance just to chain methods.

Original Thread

By anonymous    2017-09-20

There is a better way to do it by StreamEx

    .flatMap(id -> service.findTrades(id))
    .orElse(... default value...); 

I just saw: "As Stuart Marks says it, 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.." in the comments under another question:

Original Thread

By anonymous    2017-11-13

https://www.youtube.com/watch?v=Ej0sss6cq14, https://stackoverflow.com/a/23464794/571407,

Original Thread

By anonymous    2017-12-18

@JimC Maybe you find [this video](https://www.youtube.com/watch?v=Ej0sss6cq14) from Stuart Marks (JDK developer) about `Optional` enlightening. It's 1 hour long, but it's worth every minute.

Original Thread

By anonymous    2018-03-12

You may take a look at [Optional - The Mother of All Bikesheds by Stuart Marks](https://www.youtube.com/watch?v=Ej0sss6cq14). A talk by one of the Java guys. He explains the class, its usage, advantages and disadvantages, misunderstandings etc. in detail.

Original Thread

By anonymous    2018-03-18

I always recommend watching [this talk](https://www.youtube.com/watch?v=Ej0sss6cq14) (by one of the designers) to people that misunderstood `Optional`. It explains when to use it and when to not use it. Maybe it helps you too.

Original Thread

By anonymous    2018-05-01

@Laurens I've augmented my answer. And even Sonar is right to point out that `Optional#get` requires care, [the `isPresent`-`get` idiom is considered bad practice](https://www.youtube.com/watch?v=Ej0sss6cq14&t=10m30s); in fact, it can be avoided here.

Original Thread

By anonymous    2018-05-01

There is a [nice talk](https://www.youtube.com/watch?v=Ej0sss6cq14) by the devs on `Optional` and its usage and misusage. It covers the idea behind this method too.

Original Thread

By anonymous    2018-05-09

here is a good video https://www.youtube.com/watch?v=Ej0sss6cq14

Original Thread

Submit Your Video

If you have some great dev videos to share, please fill out this form.