order of expression evaluation

P

parez

Hi,


string some = null;

if (some != null && some.Trim() != "")
{
Console.Write("");
}

Above code does not throw a runtime exception..( .net 3.5)

Is it safe to assume the order of expression evaluatin is left to
right.??
or would it be a better idea to do a double ifs

TIA


PS. I know about String.IsnullOrempty
 
C

Cowboy \(Gregory A. Beamer\)

The expressions are calculated in order, from left to right, although I am
still inclined to use two Ifs, as it makes it easier to refactor even
further (if the inner test is reused, for example).

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
 
M

Marc Gravell

That code is indeed perfectly safe*. The order of execution is explained
in the language spec (ECMA 334 v4) below.

[caveat]*=there are some horrible thread-race, volatile, multi-threaed,
unsynchronized, field scenarios that might cause a problem - just don't
do that![/caveat]

-- spec --
14.11 Conditional logical operators
The && and || operators are called the conditional logical operators.
They are also called the “shortcircuiting”
logical operators.
conditional-and-expression:
inclusive-or-expression
conditional-and-expression && inclusive-or-expression
conditional-or-expression:
conditional-and-expression
conditional-or-expression || conditional-and-expression
The && and || operators are conditional versions of the & and | operators:
• The operation x && y corresponds to the operation x & y, except that y
is evaluated only if x is true.
• The operation x || y corresponds to the operation x | y, except that y
is evaluated only if x is
false.
An operation of the form x && y or x || y is processed by applying
overload resolution (§14.2.4) as if the
operation was written x & y or x | y. Then,
• If overload resolution fails to find a single best operator, or if
overload resolution selects one of the
predefined integer logical operators, a compile-time error occurs.
• Otherwise, if the selected operator is one of the predefined Boolean
logical operators (§14.10.2), the
operation is processed as described in §14.11.1.
• Otherwise, the selected operator is a user-defined operator, and the
operation is processed as described
in §14.11.2.
It is not possible to directly overload the conditional logical
operators. However, because the conditional
logical operators are evaluated in terms of the regular logical
operators, overloads of the regular logical
operators are, with certain restrictions, also considered overloads of
the conditional logical operators. This is
described further in §14.11.2.
 
A

Arne Vajhøj

parez said:
string some = null;

if (some != null && some.Trim() != "")
{
Console.Write("");
}

Above code does not throw a runtime exception..( .net 3.5)

Is it safe to assume the order of expression evaluatin is left to
right.??
or would it be a better idea to do a double ifs

Yes. It is safe.

Marc has already explained it. But let me try a bit simpler.

if (some != null && some.Trim() != "") // <---- two &
{
Console.Write("");
}

will never crash - && evaluates left to right.

if (some != null & some.Trim() != "") // <---- one &
{
Console.Write("");
}


will always crash - & evaluates all parts.

Arne
 
J

Jeff Louie

Parez.. It is an old trick taking advantage of the short circuited &&
operator.
From my old JScript Tutorial

http://www.geocities.com/jeff_louie/helloworld2.htm

Learn More About Short Circuited Logical AND

The logical AND operator, &&, returns true if both the left and right
hand
operand evaluates to boolean true. However, the evaluation is "short
circuited" if the the first operand evaluates to false. Since the result
of the
logical AND operation is known if the left sided operand is false, there
is no
need to actually evaluate the right hand operand. For efficiency, the
right
hand operand is not evaluated if the left hand operand returns false. In
other
words, the evaluation is optimized at compile time. We can use this
"short
circuited" behavior to avoid a run time null pointer exception. Calling
a
method or accessing a property on a null object will throw an exception
at
runtime, unexpectedly crashing the application with the message "Object
reference not set to an instance of an object." Here is the snippet code
in our
project that demonstrates the use of logical AND:

if (message != null && message.length > 0) {
this.message= message;
}

If the user passes a null object to this method, the evaluation is short
circuited and the right hand operand, message.length, is not evaluated
or
executed. Pretty tricky.

Regards,
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top