+1 for "N".equals(whatever) to prevent NPEs

Sent from my iPhone

On Jul 31, 2010, at 9:44 AM, Theodore Petrosky <[email protected]> wrote:

> Thanks, this was exactly the information I was looking for.
> 
> Ted
> 
> 
> --- On Sat, 7/31/10, Paul Hoadley <[email protected]> wrote:
> 
>> From: Paul Hoadley <[email protected]>
>> Subject: Re: checking for null Noob question
>> To: "Theodore Petrosky" <[email protected]>
>> Cc: [email protected]
>> Date: Saturday, July 31, 2010, 8:46 AM
>> Hi Ted,
>> 
>> The '||' operator is a 'short circuit' operator, and it
>> evaluates the left-hand side first.  Assume
>> this.dueDateType() returns null, then in this case:
>> 
>> On 31/07/2010, at 9:35 PM, Theodore Petrosky wrote:
>> 
>>> if (this.dueDateType() == null ||
>> this.dueDateType().equals("N")) {
>> 
>> the expression on the left-hand side is true, and the
>> right-hand side is never evaluated (because its value won't
>> affect the value of the whole expression).  That is, it
>> "short circuits" and you avoid a NullPointerException. 
>> In this case:
>> 
>>> if (this.dueDateType().equals("N") ||
>> this.dueDateType() == null) {
>> 
>> this.dueDateType().equals("N") is evaluated, and you get a
>> NullPointerException.
>> 
>>> I don't really care that I must check in a specific
>> order... It just took me time to understand that the order
>> was important.
>> 
>> The order is only important to the extent that the LHS is
>> always evaluated, and the RHS is only evaluated if the LHS
>> is false.  So in this case, where you're doing a null
>> check specifically to avoid an exception, yes it needs to be
>> on the LHS.
>> 
>> http://en.wikipedia.org/wiki/Short-circuit_evaluation
>> 
>> 
>> -- 
>> Paul.
>> 
>> http://logicsquad.net/
>> 
>> 
>> 
> 
> 
> 
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list      ([email protected])
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/webobjects-dev/mschrag%40pobox.com
> 
> This email sent to [email protected]
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to