Re: Question on Effective Java Item 27



<hforco@xxxxxxxxxxxxxx> wrote in message
news:1145543133.123716.177300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

In Joshua Bloch's Effective Java, Item 27 ("Return zero-length arrays,
not nulls"), he concludes with the following words:

"In summary, there is no reason ever to return null from an
array-valued method instead of returning a zero-length array."

I'm not convinced this is correct.

For example, let's say I have a look-up method for some data:

byte[] findData(Object key);

Surely in this case a zero-length returned value could be meaningful in
itself

Yes.

and so in this case we should return either null

No (IMHO) .

or throw an exception

No (IMHO) .


if an invalid key was used.

Is this different to a key that is valid, but doesn't match anything ??


This is a common problem, specifically that one is attempting to use one
value to convey multiple meanings. So either :

1. define multiple parameters, one to retrieve the outcome of the search,
and the other for the value.

2. define a result type that holds both parameters

An example of 2 is :

class FDResult
{
boolean found ;
byte[] value ;
}

FDResult findData(Object key)
// post : RESULT.found XOR (RESULT.value == null)


Regards,
Steven Perryman


.