AndAlso/OrElse usage

  • Thread starter Thread starter Mike Hale
  • Start date Start date
M

Mike Hale

Is it better to use AndAlso and OrElse by default rather than the regual And
& Or?

MikeH
 
Hi Mike,

There is a syntax error in your message
Is it better to use AndAlso and OrElse by default rather than the regual And
& Or?

It has to be
Is it better to use AndAlso AndAlso OrElse by default rather than the regual
And AndAlso Or?

This was not serious, but you have my answer,

Cor
 
The effects are different.

I exclusively use AndAlso / OrElse because it short circuits the evaluation
of the rest.

And / Or evaluates the entire statement, regardless if a part of it was
false or True. Very inefficient.

AndAlso / OrElse evaluates from left to right until the condition is met.


I think MS would have liked to replace And / Or with AndAlso / OrElse, but
it would have broken many VB applications in the upgrade process. If a
programmer had done something like:

If x and y() then....

and y() did some processing which changed the state of the application...
broken app.


C# and the rest of the languages I can think of evaluate like AndAlso
OrElse.

One other consideration is if you needed to test a value for Null (Nothing)
first and then test a part of that value. And would cause a runtime error:

If not x is nothing and x.myprop...
exception will be thrown if x is nothing because it evaluates to the right.

If not x is nothing andalso x.myprop....

exception will not be thrown if x is nothing.
 
Mike,
Most of the time you will be better off using AndAlso & OrElse, however
there are times when you should not.

Remember that AndAlso & OrElse will stop evaluating the expression when the
answer to the test is obvious, which means you can avoid runtime errors and
improve efficiency!

Dim aPerson As Person
If (Not aPerson Is Nothing) AndAlso aPerson.IsAlive then
aPerson.DoSomething()
End If

If you attempted the above with just And you will have a runtime error, when
aPerson is Nothing. However with AndAlso the IsAlive is never checked if
aPerson is Nothing.

Hope this helps
Jay
 
The effects are different.

I think MS would have liked to replace And / Or with AndAlso / OrElse, but
it would have broken many VB applications in the upgrade process. If a
programmer had done something like:

In the early releases's PDC build and Beta1, this was in fact the case.
MS had made And and Or true logical operators and created BitAnd and
BitOr to handle bit wise evaluation (which is how And and Or work
currently)... Then, when they made several rollback changes (to be more
compatable with VB.CLASSIC) in Beta 2 - they put And and Or back to the
original VB.CLASSIC behavior, removed BitAnd and BitOr and added AndAlso
and OrElse for logical comparisons. Personally, I wished they had never
done the roll backs...

The one I found especially anoying was the reversion to the VB.CLASSIC
way of declaring arrays... With the number in the () beign the last
index in the array rather then the number of elements in the array.

'Current and VB.CLASSIC method
Dim a(5) As Integer ' array with 6 elements 0 - 5

'Beta1 and, IMO, supperior method
Dim a(5) As Integer ' array with 5 elements 0 - 4

Of course, since I don't actually program in VB.NET (I do C#) - I
suppose I can live with it :)
 
Boy was that helpful! Thanks! You really are an MVP!

I know that AndAlso/OrElse short circuits, checking from left to right,
which can be more efficient. Perhaps I should have worded my question "What
is more common among the VB developers out there..."

MikeH
 
Boy was that helpful! Thanks! You really are an MVP!

I know that AndAlso/OrElse short circuits, checking from left to right,
which can be more efficient. Perhaps I should have worded my question "What
is more common among the VB developers out there..."

MikeH

For most things, were your truely doing a logical comparison, I would
use AndAlso or OrElse. But, if you're doing bitwise operations or
comparisons (like checking/setting bit flags) then you use And and Or.

Right tool for the job :)
 
* "Mike Hale said:
Boy was that helpful! Thanks! You really are an MVP!

I know that AndAlso/OrElse short circuits, checking from left to
right,

If you know that, you are on the right way. If you need logical
and/or/... + short circuiting, then use 'AndAlso' or 'OrElse'. In most
other cases use the other operators.
which can be more efficient. Perhaps I should have worded my question "What
is more common among the VB developers out there..."

None is "more common". There is often only one way "right", but that
depends on the situation and the programmer's knowledge about the
_difference_ between those operators.

For me, your questions sounds like this:

"What is "more common"? Using the '+' or the '*' operator."

That's why I wrote the answer before. I am sure you understand what I
want to say.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
Tom Shelton said:
The one I found especially anoying was the reversion to the VB.CLASSIC
way of declaring arrays... With the number in the () beign the last
index in the array rather then the number of elements in the array.

'Current and VB.CLASSIC method
Dim a(5) As Integer ' array with 6 elements 0 - 5

'Beta1 and, IMO, supperior method
Dim a(5) As Integer ' array with 5 elements 0 - 4

Of course, since I don't actually program in VB.NET (I do C#) - I
suppose I can live with it :)


I tend to agree with this too. When you are dynamically build an array, the first element is 0,
so to dimension an array with 1 element you have to use Redim a(0) which has always seemed
confusing to me.



 
Mike,
I suspect the "more common" way is to use And & Or, as that is they way VB6
developers have learned to use it, hence when they move to VB.NET will
continue to use And & Or in VB.NET, without actually realizing that AndAlso
& OrElse is available and should be considered for use instead. Or they
don't fully understand the benefits of using AndAlso & OrElse over And & Or.

I hope you agree that simple because the "more common" method is to use And
& Or based on the above supposition does not make it the "better" way.

I would recommend you use AndAlso & OrElse unless you have to use And & Or.
The documentation that Herfried pointed you to should indicate which is to
be used when...

Just a thought
Jay
 
Back
Top