Optional - The Mother of All Bikesheds by Stuart Marks

preview_player
Показать описание

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.

[VXM-7030]
Рекомендации по теме
Комментарии
Автор

From my discovery, Optional is one of the most misunderstood and misused API among Java/Scala developers. I saw many articles mislead people that Optional is a supreme of null, so people should avoid null as much as possible. That leads to lots of convoluted code because of the abuse of optional. This video gives the best explanations on the purpose of Optional and when to use or not to use it. Thanks for the insightful talk.

jianwuchen
Автор

At LAST !! i understend how Optional works!!! Thanks

lisafox
Автор

One of better videos on Optional I've seen. Thank you

ZacharyBetz
Автор

Excellent video. Recently at work some upgrades are happening which have pushed Optionals up the priority chain for improving how we work. However, I was suspicious about how they were being used, but now I have a better understanding why, and can already assist in stopping some bad habits forming. Thumbs up!

old_conrad
Автор

Kotlin took care of the mistakes that come from optional by forcing you at compile time to deal with nullability upfront when you define any instance. If you're a java developer that hasn't started using Kotlin you're in for a lot of fun.

glaurung
Автор

why doesnt optional extend collection, given its a collection of size 0 or 1

hellowill
Автор

Is it possible to replace two ifs with an optional.. Like if a! = xx and b! = yy then do something. With optional of nullable

kennethcarvalho
Автор

In given example with customerIDList, the given code is not compiled:
The commented out l2 ines, that can't be compiled, because the both methods isPresent() and get() are NOT static.
I added the filter to .filter(c -> c!=null) and it works, as the below code:

List<Customer> customerList =
customerIDList.stream()
.map(Customer::findByID)
.filter(c -> c != null)
// .filter(Optional::isPresent)
// .map(Optional::get)

Can somebody confirm/decline my chnages? I wold appreciate any help.

mahmudakbarpur
Автор

This talk actually answered a lot of questions that I was struggling with. Thank you.
But I am unable to find the @Nullable annotation in standard Java! When I use @Nullable, NetBeans imports ! What to do about that?

SourabhBhat
Автор

just saw a few minutes that someone pointed me to and I couldn't agree more with what he said. Clever programming is not always good, its a maintenance nightmare.

shanphreak
Автор

where is that slide that is addressing why not to use Optional for fields? did i miss something? there is a rule to not use it for fields, but did not catch when it was discussed (anyone got a timestamp for that?)

thh
Автор

29:00 I disagree here. E.g. Flatmapping Optionals allows for much cleaner code than having multiple nested if-not-null checks.

-morrow
Автор

I still don't understand what is the winning of doing if (object.isPresent() )instead of checking if the value is null
by doing if (object==null).

ahmadjaber
Автор

Optional of optional is the basis of a monad, which optional is / should be.

pmcgee
Автор

These guys should do more talks like this. If they gave this kind of talk before releasing Checked Exceptions the world would have been a better place. A lot of the new JDK concepts are foreign to java and will be misused and lead to terrible code if standards aren't well articulated.

KhanSlayer
Автор

is it hard to put link on slides in description?

AN-usen
Автор

2:46 Rule #1 should be: Optional will never be null, ever.

I wonder why not a little java compiler magic to transform this:
Optional<String> name = null;
to this?:
Optional<String> name = Optional.empty();

Or any combination that assigns null to an Optional (more or less like autoboxing), making Optional fully null-free.

jorsol
Автор

1 hour for Optional, you gotta be kidding me

tankostream
Автор

The customerNameByID() function can be written in Kotlin as follows:

fun customerNameByID(custList: List<Customer>, int custId) = custList.firstOrNull { it.id == custId } ?.name ?: "UNKNOWN"

More concise, less theory, more fun. I jumped the Java ship and not looking back. I just have hard time understanding why someone would want to complicate his code with Optional methods.

martinvysny
Автор

My feedback - lecture is mostly theoretical rather than hands-on as I see in venkat video lectures which are more practical. It would be great if he opens his IDE and runs his code.

nikhilagrawal