?
Solved

Return empty arrays or collections, not nulls

Posted on 2011-04-26
3
Medium Priority
?
380 Views
Last Modified: 2012-06-21
Return empty arrays or collections, not nulls

I was reading above staemtent. I have not understood the statement, concept behind clearly.
Please advise. Any ideas, sugestions, sample code, links, resources highly appreciated. thanks in advance
0
Comment
Question by:gudii9
3 Comments
 
LVL 23

Assisted Solution

by:cmalakar
cmalakar earned 200 total points
ID: 35468753
Obviously accessing members of null references will cause NullPointerException.
Hence to overcome from that, the statement suggests to return empty arrays or collections.
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 400 total points
ID: 35468770


I'd think that this is because it is easier to use
empty collections for the calling program
If people foget to check for null -
null will trhrow exception, wheraes apmpty collection
will usually work in all codes - just traversing through nothing.
So if folks are expecting to get ArrayList from method - let them
have ArrayList wuith no elements rather
than returning null
0
 
LVL 28

Accepted Solution

by:
dpearson earned 1400 total points
ID: 35470826
What this point says is it's better to do this:

public List<String> getNamesOfMyFriends() {
     if (haveNoFriends)
         return new ArrayList<String>() ;
    else
         ... return the names of the friends
}

than to do this:

public List<String> getNamesOfMyFriends() {
     if (haveNoFriends)
         return null ;       <-- Notice the difference
    else
         ... return the names of the friends
}

Both compile and work, so why is the first better?

It's because the caller can then write code like this:

List<String> names = getNamesOfMyFriends() ;
int numberOfFriends = names.size() ;

With the first version, this code always works.
With the second version, if the user has no friends, it will throw a null pointer exception here:
int numberOfFriends = names.size() ;    <-- names is null

So instead you have to write:
List<String> names = getNamesOfMyFriends() ;
int numberOfFriends = 0 ;
if (names != null) {
     numberOfFriends = names.size() ;
}

which is a lot more code, harder to read and more likely to be wrong.

Hope that explains it.

Doug
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question