[Java] (list == null || list.size () == 0) I don't like checking NULL / empty

**Caution! !! ** **

For beginners + This article has a lot of personal opinions.

Operating environment

background

When I'm doing code reviews, I sometimes see

        List<String> list =Some process / method (List is returned);
        
        //NULL and empty check
        if (list == null || list.size() == 0) {
        }

It is a code that performs NULL / empty check of List.

There is no problem in operation. However, I don't like it so much, so I may ask you to fix it. There are mainly ** two ** reasons.

First: Can List be NULL?

First of all, I would like you to check if the list variable may be set to NULL in "Some process / method".

If you can make it NULL, you should fix the process there.

why?

This is to reduce the possibility of a NullPointerException. You can also eliminate the caller's null check.

If "some process / method" is common, all callers must check for NULL. If the caller does not check for NULL, there is a risk of getting a NullPointerException.

If it never returns NULL, the method caller does not have to assume that it will return NULL. The caller implementation will be a little easier.

Remove all the elements in the list using remove ()` `` or removeAll ()` ``, etc.

Collections.emptyList()Let's return an empty list with something like that.



 It is also a good idea to add final to the List declaration.
 * I told you in the comments.

# Second: I want you to use isEmpty () instead of size == 0

 Is this a personal hobby? opinion? is.

#### **`list.size ==List instead of 0.isEmpty()I want you to.`**

why?

I think there is an opinion that "the contents of ʻis Empty () `` is size == 0 `` !!!". That's right at all. Nothing changes as the processing content.

But isn't list.isEmpty ()` `` easier to understand than` `list.size == 0 ...? Perhaps `list.isEmpty ()` is easier for non-Java programmers to understand, "I'm checking if it's empty!".

Personal conclusion

I want you to stop the NULL check and only check the empty of isEmpty () `` `. To avoid the need for NULL checking, never return NULL in "some process / method", even if it returns an empty list. size() == 0Than,isempty()Is more readable**I personally think**Soisempty()```to use.

        List<String> list =Some process / method (List is returned);
        
        //Empty check only
        if (list.isEmpty()) {
        }

Sometimes I come across a "null check" code

Occasionally, there is a code that judges with ``` &&` `` as shown below, but be careful because it will be a NullPointerException if NULL comes.

if(list == null && list.size() == 0){}

bonus

Apparently in the training for new employees(list == null || list.size() == 0)It seems that he was taught. I'm not wrong, but ... I was sick of it, so I wrote an article. (What do you mean?

Recommended Posts

[Java] (list == null || list.size () == 0) I don't like checking NULL / empty
Java concurrency I don't understand
[Java] I want to check that the elements in the list are null or empty [Collection Utils]
[Java beginner] Command line arguments ... I don't know ~