Re: Question on Effective Java Item 27



S Perryman wrote:
<hforco@xxxxxxxxxxxxxx> wrote in message news:1145548622.568432.125550@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

S Perryman wrote:

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.

By 'multiple parameters' do you mean multiple methods - or are you
refering to 'out' parameters?

The latter.

Java doesn't have out parameters.

I know.
But I wanted to explain both solutions as the problem is so common.


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)

I like this ... particularly as using the two-method approach
(isExistingKey, followed by getValue) would mean that look-up of the
key is done twice.

Correct.


Although I appreciate that this is more explicit than using a null
return value, do you not agree that as long as it was documented that
null return value meant the key was not found, that it would still work
in the same way?

If it is documented so, then the intent is clear.


There is nothing fundamentally different between the following:

byte[] value = findData(key);

if (value == null) {
// key not found ....
} else {
// do something with value
}

and this ....

FDResult result = findData(key);

if (result.found) {
byte[] value = result.value;
// do something with value
} else {
// key not found
}

... other than the fact that the second example is much clearer.

But in general, there are cases where the value required is not an
object reference, but a real value. For example :

int findData(Object key)

What are you going to use as the 'not found' value ??
Or will you rewrite the interface to be :

java.lang.Integer findData(Object key)

so that you can use the null value as 'not found' ??

Because I haved programmed in different prog langs, I try to have a
consistent approach for a problem regardless of the prog lang.

So here it is multiple out parameters, or a suitable result type.
But never null = not found.



null for not found is common in Java though

e.g.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html#get(java.lang.Object)

HashMap

public Object get(Object key)

Returns the value to which the specified key is mapped in this identity hash map, or null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it is also possible that the map explicitly maps the key to null. The containsKey method may be used to distinguish these two cases.
.



Relevant Pages

  • Re: mmap for I/O access
    ... The size you're mapping to also needs to be a multiple of the page size. ... I usually write a little wrapper function which allows the user to map arbitrary addresses, computing the offset and alignment for you behind the scenes. ... Ask your hardware engineer or check the hardware guide for the board. ...
    (comp.os.linux.embedded)
  • Re: Mapping multiple search result messages to a consolidated search result - possible?
    ... SearchResultB ---+ ... I can't work the functoids to do this - I can map a single part (say ... SearchResultA) to ConsolidatedSearchResult but cant combine multiple to one ... > Do you want to combine multiple XML messages into a single XML message? ...
    (microsoft.public.biztalk.general)
  • mapper - multiple loops to a single destination
    ... I am trying to find a way in the BizTalk 2004 mapper to do multiple ... certain child nodes - all to the same destination node group in the ... where a specific child node's value meets a condition, and then map one ... <B>Because the value of sibling node A is 'TheOne', ...
    (microsoft.public.biztalk.general)
  • Re: [PATCH][2.6] first/next_cpu returns values > NR_CPUS
    ... So, if NR_CPUS is 3, and you pass an empty map to any_online_cpu ... What code, what feature, what bug ... ... recall seeing any big hairy warnings that size better be a multiple ...
    (Linux-Kernel)
  • RE: Mapping multiple search result messages to a consolidated search result - possible?
    ... Do you want to combine multiple XML messages into a single XML message? ... You need to do transformation from orchestration designer. ... (When map is created/executed not from the OD it is executed on a single message. ...
    (microsoft.public.biztalk.general)