September 2019
Intermediate to advanced
816 pages
18h 47m
English
The do not use category continues with another common mistake of using Optional. This time let's address the usage of Optional in method arguments.
Using Optional in method arguments is just another use case that may lead to code that is unnecessarily complicated. Mainly, it is advisable to take responsibility for null-checking arguments instead of trusting that the callers will create Optional classes, especially empty Optional classes. This bad practice clutters the code and is still prone to NullPointerException. The caller can still pass null. So you have just turned back to checking null arguments.
Keep in mind that Optional is just another object (a container) and is not cheap. Optional consumes ...