Re: Question on Effective Java Item 27
- From: "S Perryman" <a@xxxxx>
- Date: Thu, 20 Apr 2006 16:18:03 +0100
<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
.
- Follow-Ups:
- Re: Question on Effective Java Item 27
- From: hforco@xxxxxxxxxxxxxx
- Re: Question on Effective Java Item 27
- References:
- Question on Effective Java Item 27
- From: hforco@xxxxxxxxxxxxxx
- Question on Effective Java Item 27
- Prev by Date: Re: Question on LSP
- Next by Date: Re: UML Structured class with utility parts?
- Previous by thread: Question on Effective Java Item 27
- Next by thread: Re: Question on Effective Java Item 27
- Index(es):