43rd: Returns an array or set of 0 lengths, not NULL

Source: Internet
Author: User

Private Final list<cheese> Cheesesinstock = ...;  Public cheese[] Getcheese () {    if(cheesesinstock.size () = =0        )returnnull ;    ...}

If NULL is returned,

Processing the code on the client requires additional code to handle the null return value:

cheese[] cheeses = shop.getcheeses (); if null && arrays.aslist (cheese). Contains (Cheese.stilton))    System.out.println (" Jolly good ");}

Instead of:

cheese[] cheeses = shop.getcheeses (); if (arrays.aslist (cheese). Contains (Cheese.stilton))    System.out.println ("Jolly good");}

It is considered better to have a null return value than a zero-length array, because it is not necessary to avoid the overhead required to allocate the array.

1. Unless the analysis shows that this is a method that does affect performance

2. It is possible to return the same 0-length array for a call that does not return any type

The collections class provides a way to return the same immutable empty collection when an empty collection is required:

 Public List<cheese> getcheeselist () {    if(cheeseinstock.isempty)) {        return  collections.emptylist ();     Else        return New Arraylist<cheese>(Cheesesinstock);}

43rd: Returns an array or set of 0 lengths, not NULL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.