[program-l] Re: VB.NET AndAlso vs And

  • From: "David Lant" <david.lant1@xxxxxxxxxxxxxx>
  • To: <program-l@xxxxxxxxxxxxx>
  • Date: Mon, 28 Mar 2005 16:48:05 +0100

Hi Jamal,

Well, as yet, I've still not managed to come up with a single scenario where
avoiding short-circuiting would be desirable.  So I wouldn't even think a
flag would be necessary.  And given that VB.NET is a complete re-write of
the VB language, I can't see why changing the existing And operator to
short-circuit evaluation would have been a problem.  If anything, leaving
the And as it was in VB, and introducing a new AndAlso operator just seems
likely to cause the perpetuation of inefficient code by people who don't
notice the new operator.  Oh well... *shrug* 

All the best,

David


-----Original Message-----
From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx]
On Behalf Of Jamal Mazrui
Sent: 28 March 2005 04:29
To: program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: VB.NET AndAlso vs And


Hi David,
I share your sentiment, not seeing why a programmer would want a compiler to
evaluate logical conditions by other than the short-circuiting approach.  I
think this approach enhances both performance and simplicity of code.
Performance seems clearly better because less code has to be evaluated at
runtime.

Regarding code simplicity, an "If" condition, for example, might initially
test whether a variable is greater than zero.  A second condition after the
"And" conjunction might perform another test with the same variable before
proceeding to the "Then" clause.  Sometimes, evaluating the second condition
after the first test fails can cause a runtime error, so one only wants the
evaluation to occur when the first test succeeds.  

Short-circuit evaluation permits this, whereas redundant/full evaluation
does not.  I have encountered some programming languages that do not
shortcircuit, so they result in more cumbersome code where another "If"
condition needs to be nested inside the first one.

Perhaps short circuiting depends on a certain level of intelligence in the
compiler or interpreter.  This does not seem technically difficult, however,
for such tools to achieve.  Perhaps long circuiting is intended to produce
more structured and readable code, since short circuiting may not indicate
dependencies as much.  I think it should at least be a compiler flag,
however, and the default one at that.

Regards,
Jamal

>  -----Original Message-----
> From:         program-l-bounce@xxxxxxxxxxxxx
> [mailto:program-l-bounce@xxxxxxxxxxxxx]  On Behalf Of David Lant
> Sent: Monday, March 28, 2005 8:33 AM
> To:   program-l@xxxxxxxxxxxxx
> Subject:      [program-l] VB.NET AndAlso vs And
> 
> Hi all,
> 
> I was reading through an interesting article on MSDN about 
> semi-connected applications using VB.NET, and came across the AndAlso 
> logical operator.  As I'd not noticed it before, I looked it up in the 
> online help.  (Tyler, take note.)
> 
> I fully understand that using the AndAlso operator shortcuts the 
> evaluation of each expression, so that if any of the component 
> expressions evaluate to False, then the remaining expressions are not 
> evaluated.  The problem is, I can't for the life of me understand why 
> one would *not* want to shortcut such logical evaluations.
> 
> I do appreciate that the And operator has an additional function of 
> performing bitwise And operation on expressions.  But I'm left 
> thinking that what this really means is that under VB.NET, we should 
> always use AndAlso for logical conjunctions on expressions, and only 
> use And for bitwise comparisons.
> 
> Is this right?  Or am I leaping to an unfounded conclusion?  If anyone 
> can offer a reason why you would want to evaluate something that 
> wouldn't affect the result, I would very much appreciate it. <smile>
> 
> All the best,
> 
> David
> 

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: