>> which is more 'politically correct' (or should it be said as more
>> acceptable):
[quoted text clipped - 14 lines]
> It might look nicer, but option (a) has the advantage that it will never
> throw a NullPointerException. That's why it's so often used.
I tend to avoid option (a), for exactly that reason.
If I know null should be treated the same as a mismatch, I prefer to
explicitly test for null so that next time I look at the if, I know I
thought about it:
if( myString != null && myString.equals("something") )
If, on the other hand, I'm not planning for null myString, I would
rather get a NullPointerException than go on computing as though it were
a string and not equal to "something".
Patricia
Stefan Ram - 14 Jul 2006 01:15 GMT
>If I know null should be treated the same as a mismatch, I
>prefer to explicitly test for null so that next time I look at
>the if, I know I thought about it:
>if( myString != null && myString.equals("something") )
Possibly, for doubles you might also use:
if( x == x && y == y && x == y )
so that the next time you look at it, you know that you have
thought about the possibility for x or y to be java.lang.Double.NaN.
Chris Uppal - 14 Jul 2006 14:15 GMT
> If I know null should be treated the same as a mismatch, I prefer to
> explicitly test for null so that next time I look at the if, I know I
> thought about it:
Yes, good point. And if I /did/ want to allow null, and for some reason wanted
to avoid the explicit null test, then I'd feel obliged to add a comment to the
"backward" equals() comparison, and so there would be no saving in typing or
readability.
-- chris