6

I'm feeling quite silly for asking this question, but I've already seen this code on two separate corporate codebases and I'm starting to think there's some chunk of ancient java knowledge I'm not aware of.

So, we got this kind of code:

/* Lot of corporate stuff */
if (ClassicUtilsClass.isNotNull(variable)) {
    /* Variable handling shenanigans. */
}

Going to our ClassicUtilsClass revealed the implementation of the function:

/* Lot of useful static functions*/
public static isNotNull(Object o) {
    return o!=null;
}
public static isNull(Object o) {
    return o==null;
}

I read it, scratched my head, looked around the internet, asked colleagues and friends, then laugh at the uselessness of the function. Then I switched jobs and a colleague showed me more or less the same code.

In the same codebase we have the classic "isNullOrEmpty" which does the standard "return object==null || object.size() == 0;", so I can understand having a function to wrap the null comparison... plus something else, but I don't see the point of creating a whole new function for null checking.

I'm losing something here?

Neuromante
  • 531
  • 2
  • 12
  • 29
  • 3
    See also [`Objects::nonNull`](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#nonNull-java.lang.Object-) and [`Objects::isNull`](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#isNull-java.lang.Object-) which I believe only exist to be used as predicates. But I can't see any reason to prefer `if (SomeClass.isNotNull(x))` over the shorter, clearer `if (x!=null)` – khelwood Nov 30 '16 at 10:14

3 Answers3

8

There are similar functions in the Objects utility class, called nonNull and isNull. The reason they are provided is to be used as predicates, e.g.

...stream().filter(Objects::nonNull)...

I can't see any reason to use

if (SomeClass.isNotNull(x))

rather than the shorter, clearer

if (x!=null)
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Uhm... I should have specified (who knew ,haha), but It can't be, as both places I found this used Java 1.7, not 1.8. – Neuromante Dec 03 '16 at 10:39
3

There are only a few reasons to implement operators as methods that I can think of.

The first reason is to "profile" how many times the operation is used in an application. Profiling tools count the time spent in methods and the number of times methods are called but they don't track operator usage.

The second reason to implement operators as functions is to use them in functional programming constructs. Operators cannot be passed around using reflection, but methods can, so it is possible to create functional operations (map, reduce, etc) that use reflection and in those cases operators would need to be implemented as methods to be utilized.

A third possible reason to implement the not null operator as a method is to simplify the automated translation of some other programming languages into java. In java primitive types cannot be null so checking if they are null directly using the == operator would cause a compiler error. Java will autobox primitives into objects so they can be used as arguments to methods which accept Object as their argument type. In this case a code translator could translate null checks to method calls without needing to track the variable type.

Ralph Ritoch
  • 3,260
  • 27
  • 37
  • The first count would be meaningless tbh – TheLostMind Nov 30 '16 at 10:33
  • Could this be a requirement for any framework for those reasons? All the places I've seen this stuff were corporate-level applications. Maybe there's something hidden somewhere Spring/Whatever which leads to this... – Neuromante Dec 03 '16 at 10:37
0

Maybe the reason could be that in some tests it can be useful to mock ClassicUtilsClass.isNotNull and ClassicUtilsClass.isNull methods to return true or false.

Anyway as you I can't see the point. Different would be a StringUtils.isEmpty by Apache Commons, which returns true if the string is null or equal to ""

flavio
  • 168
  • 1
  • 9
  • Can you provide a link to that class? I can't find it googling. – Neuromante Dec 03 '16 at 10:40
  • Sure. The library is apache-commons-lang – flavio Dec 04 '16 at 12:17
  • Here there is the documentation https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html And the library can be downloaded here https://commons.apache.org/proper/commons-lang/ – flavio Dec 04 '16 at 12:17