[WEB][42108]Optimized use of Java Optional Else #77
Labels
ecoCode-standard
measurable
Review: first done
spotter issue 1st review
Review: second Done
Spotter issue 2nd review
Techno: java
Validated: implementable
\newpage
Optimized use of Java Optional Else
Platform
Main characteristics
Severity / Remediation Cost
Rule short description
The Optional orElse method always evaluate the "other" argument, whereas the orElseGet only call the given Supplier method passed as an argument when the Optional value isn't present. Evaluate
orElse
can be inefficient.Rule short description
Text
Remember (from the Javadoc) that the Supplier method passed as an argument is only executed when an Optional value isn't present.
To avoid unneeded argument evaluation, check that "other" argument of orElse method is never a method call, but simply an object or a static value.
HTML
Implementation principle
orElse
methodorElse
method argument is a call of methodReferences
https://www.baeldung.com/java-optional-or-else-vs-or-else-get
The text was updated successfully, but these errors were encountered: