-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite Optional.ofNullable(A).orElse(B)
to requireNonNullElse(A, B)
#364
Comments
Nice one. The first |
NB: I see I had this same rule in my private TODO list, followed by:
|
Another observation: the
Edit: though we may want to use a Edit 2: ah, but we already have Edit 3: ah, but we documented that this specific case isn't covered yet 🙃, |
…ireNonNullElseGet(A, () -> B)` over Optional alternative. Added Rules: (NullRules) - prefer `requireNonNullElse(A, B)` over `Optional.ofNullable(A).orElse(B)` - prefer `requireNonNullElseGet(A, () -> B)` over `Optional.ofNullable(A).orElseGet(() -> B)` Related issue: PicnicSupermarket#364
…ireNonNullElseGet(A, () -> B)` over Optional alternative. Added Rules: (NullRules) - prefer `requireNonNullElse(A, B)` over `Optional.ofNullable(A).orElse(B)` - prefer `requireNonNullElseGet(A, () -> B)` over `Optional.ofNullable(A).orElseGet(() -> B)` Related issue: PicnicSupermarket#364
…ireNonNullElseGet(A, () -> B)` over Optional alternative. Added Rules: (NullRules) - prefer `requireNonNullElse(A, B)` over `Optional.ofNullable(A).orElse(B)` - prefer `requireNonNullElseGet(A, () -> B)` over `Optional.ofNullable(A).orElseGet(() -> B)` Related issue: PicnicSupermarket#364
…ireNonNullElseGet(A, () -> B)` over Optional alternative. Added Rules: (NullRules) - prefer `requireNonNullElse(A, B)` over `Optional.ofNullable(A).orElse(B)` - prefer `requireNonNullElseGet(A, () -> B)` over `Optional.ofNullable(A).orElseGet(() -> B)` Related issue: PicnicSupermarket#364
…ireNonNullElseGet(A, () -> B)` over Optional alternative. Added Rules: (NullRules) - prefer `requireNonNullElse(A, B)` over `Optional.ofNullable(A).orElse(B)` - prefer `requireNonNullElseGet(A, () -> B)` over `Optional.ofNullable(A).orElseGet(() -> B)` Related issue: PicnicSupermarket#364
Problem
Sometimes a nullable variable can be wrapped in an
Optional
simply to perform a null-or-else check.In such cases it's simpler to use
requireNonNullElse
orrequireNonNullElseGet
fromjava.util.Objects
.Description of the proposed new feature
I would like to rewrite the following code:
to:
Considerations
Could also consider the case
Optional.of(A).orElse(B)
which can be rewritten as justA
, and similar fororElseGet
.EDIT: actually,
Optional.of
would already throw an exception ifA
wasnull
, while replacing with justA
could propagate thenull
further, so it should be replaced withrequireNonNull(A)
.Participation
The text was updated successfully, but these errors were encountered: