Scott on Writing

Musings on technical writing...

Question: Do you use AndAlso and OrElse?

By choice I program in C#, but this month I am working on a couple of consulting projects (hire me!) that use VB.NET.  I find myself using the short-circuiting operators - AndAlso and OrElse - quite a bit.  There are times when you'd need to use them if you wanted to do something like the following on one line of code:

If reader.Read() AndAlso reader(”FirstName”) = “Scott” then
  ...
End If

I don't find myself doing things like the above, but I still find myself using AndAlso and OrElse a lot.  Simple things, like if I want to only proceed if two Boolean values are true, I'll do:

If bool1 AndAlso bool2 then ...

I'm curious if the everyday / regular VB.NET programmers find themselves using the short-circuiting operators or not.  The only reason, I fathom, why you'd not want to use short-circuiting is if the evaluation you were performing had some side-effects, which is a bad programming practice.  It's a real shame the VB.NET language designers buckled to the pressure of the VB community and didn't just made And and Or short-circuiting, as initially planned.

posted on Tuesday, March 23, 2004 2:24 PM

Feedback

# re: Question: Do you use AndAlso and OrElse? 3/23/2004 2:50 PM Lorenzo Barbieri

I use them much more than And and Or...

# re: Question: Do you use AndAlso and OrElse? 3/23/2004 3:09 PM Adam

I only programmed in VB.NET for about 2 months on a consulting gig, but I used AndAlso and OrElse almost exclusively.

for instance:
If Not myString Is Nothing AndAlso myString.Trim().Length > 0 Then

It simply has to be used so frequently that I pretty much quit using And and Or.

But I'm back to C# now, so I'm not really relevant :)

# re: Question: Do you use AndAlso and OrElse? 3/23/2004 3:15 PM Sean Chase MCSD.NET

This is one of the reasons I don't like VB.NET. They changed the language around enough from VB6, they should have just made AND and OR short-circuit by default. The extra keywords I understand are for backwards compatibility, but there's enough of that broken anyway.

# re: Question: Do you use AndAlso and OrElse? 3/23/2004 4:04 PM Brian Desmond

I use the everday - don't use straight and/or much at all.

# re: Question: Do you use AndAlso and OrElse? 3/23/2004 5:07 PM Paul Vick

Check out http://www.panopticoncentral.net/PermaLink.aspx/b8439510-534a-4127-9995-ccde06b82d6a if you want a more detailed discussion of what happened in regards to these operators...

# re: Question: Do you use AndAlso and OrElse? 3/23/2004 6:23 PM Xander Sherry

Always. I can't recall the last time that I used And/Or. It's been several months, at least.

# re: Question: Do you use AndAlso and OrElse? 3/23/2004 7:59 PM Darren Neimke

Ditto... I short-circuit 100% of the time :) .

# re: Question: Do you use AndAlso and OrElse? 3/24/2004 5:15 AM Markus The

Coming from a VB/C++ background, I now use both C# and VB.NET regularly. I am used to C++/C# short-circuiting, but I'm also completely used to the fact that VB *doesn't*. Having always worked in both languages, I have trained myself to use nested If's in VB whenever I want short-circuiting. Consequently, I now find myself forgetting the new AndAlso/OrElse - so no, I don't use them a lot. But because I think they are slightly confusing (how is a novice supposed to remember the difference? Semantically, AND is equal to ANDALSO, isn't it?) I'll stick to that. In C#/C++ that's not a problem: since there is only one option, there's no confusion.

And yes, I think they should have abolished the old AND and OR in favor of the new ones. We had to convert to .NET anyway, so we could have hunted down these constructions at the same time.

# re: Question: Do you use AndAlso and OrElse? 4/8/2004 9:57 PM Jamison Roberts

I've never written a single line of code in VB.Net, so needless to say I've never even heard of AndAlso/OrElse. If they behave the same way && and || does in C#, then i'd be using them exclusively should I actually be working in VB.Net.

# re: Question: Do you use AndAlso and OrElse? 4/1/2005 12:25 PM Dan Crowdus

I use AndAlso and OrElse exclusively. Can anyone think of a legitimate reason when you WOULDN'T want to short circuit?

# re: Question: Do you use AndAlso and OrElse? 4/1/2005 12:29 PM Scott Mitchell

Dan, you might not want short-circuiting if the expressions in the If statement cause side-effects. It's not very lucid programming, but some folks do it. If you all of a sudden switch from not short-circuiting to short-circuiting, such programs will break. That's why VB.NET - which was originally going to make And and Or short-circuiting - added AndAlso and OrElse.

# re: Question: Do you use AndAlso and OrElse? 12/27/2005 5:48 PM Don

Interesting discussion, I was just wondering about this myself. I believe the AND and OR can also be used as bitwise operators - so I am guessing that is the reason for MS not making them short-circuiting.

# re: Question: Do you use AndAlso and OrElse? 12/28/2005 2:45 PM Robz

I use short circuiting exclusively, except when I (well) forget to. :)

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<July 2008>
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Comment Stats

DayTotal% of Total
Sunday 1896.8%
Monday 39014.0%
Tuesday 46916.8%
Wednesday 51518.5%
Thursday 54419.5%
Friday 50818.2%
Saturday 1706.1%
Total 2785100.0%

Hour1Total% of Total
12:00 AM 682.4%
1:00 AM 712.5%
2:00 AM 632.3%
3:00 AM 752.7%
4:00 AM 572.0%
5:00 AM 1093.9%
6:00 AM 1114.0%
7:00 AM 1615.8%
8:00 AM 1756.3%
9:00 AM 1505.4%
10:00 AM 1736.2%
11:00 AM 1826.5%
12:00 PM 1906.8%
1:00 PM 1766.3%
2:00 PM 1605.7%
3:00 PM 1324.7%
4:00 PM 1124.0%
5:00 PM 983.5%
6:00 PM 913.3%
7:00 PM 993.6%
8:00 PM 853.1%
9:00 PM 802.9%
10:00 PM 833.0%
11:00 PM 843.0%
Total 2785100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.14144
Monday 5.35353
Tuesday 4.35444
Wednesday 7.58644
Thursday 6.87625
Friday 5.45414
Saturday 5.03161
Total 5.802785

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.45109
9:00 AM 6.34279
10:00 AM 6.41250
11:00 AM 4.28184
12:00 PM 6.98342
1:00 PM 2.87112
2:00 PM 5.29222
3:00 PM 8.54299
4:00 PM 3.9190
5:00 PM 5.78156
6:00 PM 4.52113
7:00 PM 9.32177
8:00 PM 9.06154
9:00 PM 5.14113
10:00 PM 6.2381
11:00 PM 4.5732
Total 5.802785

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles