September 2019
Intermediate to advanced
816 pages
18h 47m
English
The do not use category continues with another scenario that is against the intention of using Optional. Keep in mind that Optional represents a container for objects; therefore, Optional adds another level of abstraction. In other words, improper use of Optional simply adds extra boilerplate code.
Check the following use case of Optional that shows this (this code violates the previous Do not use Optional for fields section):
// Avoidpublic class Book { // cannot be null private final String title; // optional field, cannot be null private final Optional<String> isbn; public Book(String title, Optional<String> isbn) { this.title = Objects.requireNonNull(title, () -> "Title cannot be null");