Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1093397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:49:17+00:00 2026-05-16T23:49:17+00:00

Supposing I have a function with following signature: Foo[] getSomeFoos() { //return null —

  • 0

Supposing I have a function with following signature:

Foo[] getSomeFoos() 
{
      //return null         --- option A 
      or 
      //return new Foo[0];  --- option B
}

What is the recommended practice to return the value indicating there is no elements returned? Any pros or cons for each option?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:49:18+00:00Added an answer on May 16, 2026 at 11:49 pm

    If your method GetSomeFoos() actually ‘found’ 0 elements, then it should return new Foo[0].

    If there was some sort of error, you should throw an Exception.

    The reason is because users shouldn’t have to look for nulls:

    for (Foo f: GetSomeFoos()) {
        // do stuff
    }
    

    In the above case, if GetSomeFoos returned null, a user will have to deal with an NullPointerException. But, if you return an empty array, the code will never enter the loop, and the user will not have to deal with an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing I have this: foo bar 1 and foo bar 2 How can I
Say I have the following template function: template <class T> void apply(const vector<complex<T> >&
Let's say I have following ArrayObject : $array_object = new ArrayObject(array( array('1', '2', '3',
i want a function with the following signature: bool signed_a_greater_than_signed_b(unsigned char a, unsigned char
What's the object type returned by Datepicker? Supposing I have the following: $(#txtbox).datepicker({ onClose:
Supposing I have the following code which returns a Javascript object which I can
I have the following: <input type=submit id=submit value=Submit /> Inside of jQuery I have
Supposing I have a StringerHelper(Component|Helper|Behaviour).php class with a method for random strings. I might
Supposing I have 3 tables: Users, Roles and UserRoles. I can fetch the user_roles
Supposing I have one perl in /usr/bin (which came along with my os-distribution) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.