What can be avoided? = Null in java?

I read this topic: Avoid! = Null statements , but I see no benefits.

  • If you are returning an empty array, you need to check where the "real" array is and where the empty array is.

  • Instead of using code like getName() != null you should use getName().length > 0 . As an example, I am working on a project where I need to compress some data with several archivers and decided to choose the compressed data with the smallest size. If I return null from the β€œcompress” method of the archiver, I need to check that the return value is not zero. If I decide to return an empty array, then I also need to check it for empty.

I'm right?

+4
source share
3 answers

The main advantage of using empty collections or "empty" actions instead of zero is that most of the time such objects will continue to work in code without further modification. The null value, in essence, is simply much more error prone due to its nature.

Take the following code, for example:

 String[] names = data.getNames(); if (names != null) { for (String name : names) { // Do stuff } } 

null check required or you will get NPE. Using a standard for a loop does not solve the problem. On the other hand, if you know that you will always have some kind of array, your code will work fine, without additional checks. The loop will not start at all if the array is empty. The problem is resolved.

The same is true for code that implements some form of action. Another example:

 Action myAction = data.getActionToRun(); if (myAction != null) { myAction.run(); } 

Once again you need a null check. If the action is guaranteed, then you can always call action.run() without side effects, but empty actions just won't do anything. It's simple.

In many cases, null checks can simply be discarded if you change the methods return methods, which will lead to a significantly simplified and understandable code. In some cases, returning null is the right choice (for example, getting an object from a collection of keys and values), since there is no default "inaction" value. But null indicates no value at all, and this requires additional processing. Using empty, inactive, non-zero objects allows you to handle the error with a data object. This is a good encapsulation. This is good programming. It just works. β„’

Finally, returning null is certainly not a good way to handle errors. If something goes wrong in code that will never be wrong, if you, as a programmer, did not make a programming error, use assert or exceptions. These are failures. Do not use null as a case of failure, use it as a simple lack of value.

+11
source

An empty array is a more natural object that is sometimes returned. Consider the following code:

 String [] elements = getElements(); for (String element : elements) System.out.println(element); 

If you return an empty array (i.e. no elements), nothing will be printed and the code will execute correctly. However, if getElements () returns null, you will get a null pointer exception. To prevent it, you need to add more code, which complicates the situation. Returning an empty array allows you to achieve the desired behavior without additional code.

+7
source

I had this thought for a while. Leaving the NPE problem aside for a moment. I will try to explain my thoughts in terms of OOP.

Say you are writing a class that provides a getter for an instance of Glass public Glass getGlass() . When I read this signature, I expect some instance of the Glass interface, and null is not one.

You can consider the following approach public Optional<Glass> getGlass() .

An optional class (can write your own or use Google guava Optional ) contains your generic class instance, offering the isPresent function among others. This may seem redundant because you can just use getGlass() != null instead of getGlass().isPresent() from your client code. But the main advantage here is that the signature is simple. No one is hiding anything, and you do not need to guess.

So you get the advantage:

  • NPE avoidance
  • simple signature
  • clean client code
+1
source

All Articles