Sonar rule to report the use of Objects.isNull() and Objects.nonNull() in if-then statements

Viewed 402

The Javadoc for the methods Objects.isNull() and Objects.nonNull() clearly state their purpose. e.g.

This method exists to be used as a Predicate, filter(Objects::nonNull)

But I see developers starting to use these methods within if-then statements more frequently. e.g.

if (Objects.isNull(foo)) {
  foo = new Foo();
}

if (Objects.nonNull(bar)) {
  bar.doSomething();
}

I guess it is subjective as to whether these new methods are cleaner/easier to read than traditional == and != operators.

For those of us that are not fans of this (e.g. using these functions outside of the purpose for which they were designed for), is there a Sonar rule that reports this?

0 Answers
Related