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