Why is short-circuiting not the default behavior in VB?
Asked Answered
P

3

15

VB has operators AndAlso and OrElse, that perform short-circuiting logical conjunction.

Why is this not the default behavior of And and Or expressions since short-circuiting is useful in every case.

Strangely, this is contrary to most languages where && and || perform short-circuiting.

Poser answered 28/1, 2009 at 7:3 Comment(0)
P
18

Because the VB team had to maintain backward-compatibility with older code (and programmers!)

If short-circuiting was the default behavior, bitwise operations would get incorrectly interpreted by the compiler.

The Ballad of AndAlso and OrElse by Panopticon Central

Our first thought was that logical operations are much more common than bitwise operations, so we should make And and Or be logical operators and add new bitwise operators named BitAnd, BitOr, BitXor and BitNot (the last two being for completeness). However, during one of the betas it became obvious that this was a pretty bad idea. A VB user who forgets that the new operators exist and uses And when he means BitAnd and Or when he means BitOr would get code that compiles but produces "bad" results.

Poser answered 28/1, 2009 at 7:9 Comment(3)
Actually, one could add short-circuiting behavior to bitwise operators without breaking code that didn't rely on side-effects, if the rule was that the bitwise "or" operator would evaluate the right hand operand unless the left-hand operand was equal to the all-bits-set value of the result type. On the other hand, some existing code--even code where both operands to an And or Or are produced by relational operators--does rely upon side effects of evaluating both sides of the Boolean operators.Apollinaire
Link is dead, but the content is here. Thanks!Isometric
Link fixed! Long live the link!Maynord
P
1

I do not find short-circuiting to be useful in every case. I use it only when required. For instance, when checking two different and unconnected variables, it would not be required:

  If x > y And y > z Then

  End If

As the article by Paul Vick illustrates (see link provided by Ken Browning above), the perfect scenario in which short-circuiting is useful is when an object has be checked for existence first and then one of its properties is to be evaluated.

  If x IsNot Nothing AndAlso x.Someproperty > 0 Then

  End If

So, in my opinion both syntactical options are very much required.

Pitcher answered 28/1, 2009 at 7:14 Comment(2)
But in your first example if x<y there's just no point continuing with the evaluation. The only case I can think of where I wouldn't want short circuit evaluation if where I'm calling functions that both definitely need to be run.Meacham
The short-circuiting may not be needed if the 2 conditions are totally exclusive. That too is very much dependent on developers intention. If the developer want both conditions to be executed, then there is a fall-back that would force both the conditions executed. In the cases where the existence of the object has to be checked, short-circuiting become handy.Sorry
T
0

Explicit short-circuit makes sure that the left operand is evaluated first.

In some languages other than VB, logical operators may perform an implicit short circuit but may evaluate the right operator first (depending for instance on the complexity of the expressions at left and at right of the logical operator).

Tierratiersten answered 28/1, 2009 at 13:40 Comment(2)
I know this is a rather old question, but can you provide an example of a language that might evaluate the right expression before the left? Meaning, what language might evaluate the left OR the right first, and it's unknown which one will be selected for first evaluation?Hussy
Ada comes to mind. The evaluation order of logical operators is specified only for short-circuit control forms AND THEN and OR ELSE. This means that a compiler which does not implement this behaviour for simple AND or OR is conformant. (see Ada 2012 LRM)Tierratiersten

© 2022 - 2024 — McMap. All rights reserved.