-
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
Prefer simple null
reference check over calling Objects#{isNull,nonNull}
#228
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a commit and some comments.
I'm also fine with applying the suggestions myself, but I figured it could also be the nice part of this PR and playing around with Refaster, so I left most un-applied :).
@@ -22,4 +24,18 @@ long testIsNullFunction() { | |||
long testNonNullFunction() { | |||
return Stream.of("foo").filter(s -> s != null).count(); | |||
} | |||
|
|||
boolean testIsNullReference() { | |||
var ref1 = "foo"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not use var
in this codebase :).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify the tests quite a bit, we only need a simple and singular test case per before template. Besides that, we prefer using ImmutableSet
s over Set
s. I don't think we need a Set
here TBH and can have simplified test :).
As a reference, see other tests for the other templates from NullTemplates
:).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! My thought originally was to see whether refaster would match and apply the transformation for the 3 cases (including inferred type with var
). I'm updating the test input/output.
*/ | ||
static final class IsNullReference<T> { | ||
@BeforeTemplate | ||
boolean before(@Nullable T ref) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boolean before(@Nullable T ref) { | |
boolean before(@Nullable Object object) { |
The parameter for isNull
is Object
therefore we can be more specific than using T
. In such cases we name the parameter of the before template something like object
or obj
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also holds for the other template.
* Prefer <code>==</code> operator over {@link java.util.Objects#isNull(Object)} on boolean | ||
* expressions. | ||
*/ | ||
static final class IsNullReference<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have the convention to name Refaster templates after the content of the after template. In this case there is not a lot of content in the after template, but I'd suggest naming it something like:
ObjectEqualNull
or ObjectEqualsNull
.
The other template could be something like ObjectNotEqualNull
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thought.
@@ -55,4 +56,30 @@ Predicate<T> after() { | |||
return Objects::nonNull; | |||
} | |||
} | |||
|
|||
/** Prefer the <code>==</code> operator over {@link java.util.Objects#isNull(Object)}. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps better to add for null checks
? To the Javadoc, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a commit.
Changes look good to me! Thanks for opening the PR @jeandersonbc 🚀 !
Suggested commit message:
Prefer simple `null` reference check over calling `Objects#{isNull,nonNull}` (#228)
@@ -22,4 +23,14 @@ long testIsNullFunction() { | |||
long testNonNullFunction() { | |||
return Stream.of("foo").filter(s -> s != null).count(); | |||
} | |||
|
|||
boolean testObjectEqualsNull() { | |||
Object obj = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can even use a simple String "foo"
for this, will push a proposal.
Discussed some things offline with @Stephan202 , applied the feedback 😄. W.r.t. the naming suggestion for the template, it's hard but we plan to formalize this :). |
Augments NullTemplates to prefer == and != operators over Objects.isNull and Objects.nonNull on boolean expressions. E.g., ref == null is preferable over Objects.isNull(ref)
- Updated Javadoc - Updated naming convention - Updated test input/output
ee7a386
to
478902e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebased and added a commit. Moved the templates up in the file, as they seem more "fundamental".
(If/when we sort templates lexicographically this'll change, but that's for another day.)
Slightly tweaked the suggested commit message; @rickie feel free to merge if you're okay with the changes.
/** | ||
* Prefer the {@code ==} operator over {@link java.util.Objects#isNull(Object)} for null checks. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** | |
* Prefer the {@code ==} operator over {@link java.util.Objects#isNull(Object)} for null checks. | |
*/ | |
/** Prefer the {@code ==} operator over {@link Objects#isNull(Object)}. */ |
/** | ||
* Prefer the {@code !=} operator over {@link java.util.Objects#isNull(Object)} for null checks. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong method referenced 👀
SGTM. Nice catch with the Javadoc. |
null
reference check over calling Objects#{isNull,nonNull}
This PR introduces rules to prefer the use of
==
and!=
overObjects.isNull
andObjects.nonNull
for null checks on expressions.Suggested commit message: