You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: In Java 10 and later, the Javadoc entry for Optional.get() includes this:
API Note:
The preferred alternative to this method is orElseThrow().
The Javadoc for Optional.orElseThrow(), introduced in Java 10, is identical to the former Javadoc, aside from the above API note, and the internal code implementation of the two methods are identical. The proposed PMD rule will tell the user that they should replace instances of .get() with .orElseThrow().
This rule should only apply to Java 10 and later. On Java 8 and 9, Optional.get() should be considered correct, as Optional.orElseThrow() is not available.
For what it's worth Brian Goetz was the person who originally chose the name .get() and considers it "our biggest API mistake in Java 8".
Methods like orElse or ifPresent look harmless, and are; methods like orElseThrow have potentially harmful side-effects but have names that are transparent about what harm they could do. But Optional.get() is neither safe nor transparent -- and yet awfully tempting-looking -- and this leads to frequent misuse. Naming matters, and this one was wrong, and the harm is observable. I wish I'd caught it before 8 shipped.
It seems the decision was made that the cost of deprecating .get() was too high at this time, so the "API Note" was the best they could do. Perhaps the method will be deprecated in the future.
As for rule priority, I think this rule is of comparable priority to rules like UseAssertEqualsInsteadOfAssertTrue, which are Medium (3), but could be a lower priority if desired.
Proposed Rule Name: UseOptionalOrElseThrowInsteadOfOptionalGet
Proposed Category: Best Practices
Description: In Java 10 and later, the Javadoc entry for
Optional.get()
includes this:The Javadoc for
Optional.orElseThrow()
, introduced in Java 10, is identical to the former Javadoc, aside from the above API note, and the internal code implementation of the two methods are identical. The proposed PMD rule will tell the user that they should replace instances of.get()
with.orElseThrow()
.This rule should only apply to Java 10 and later. On Java 8 and 9,
Optional.get()
should be considered correct, asOptional.orElseThrow()
is not available.For what it's worth Brian Goetz was the person who originally chose the name
.get()
and considers it "our biggest API mistake in Java 8".It seems the decision was made that the cost of deprecating
.get()
was too high at this time, so the "API Note" was the best they could do. Perhaps the method will be deprecated in the future.As for rule priority, I think this rule is of comparable priority to rules like UseAssertEqualsInsteadOfAssertTrue, which are Medium (3), but could be a lower priority if desired.
Code Sample:
Incorrect code
Correct code
Possible Properties:
The text was updated successfully, but these errors were encountered: