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.
<February 2010>
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

Comment Stats

DayTotal% of Total
Sunday 2046.8%
Monday 42514.1%
Tuesday 51617.1%
Wednesday 55318.4%
Thursday 58019.2%
Friday 54718.2%
Saturday 1886.2%
Total 3013100.0%

Hour1Total% of Total
12:00 AM 772.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1806.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1876.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1063.5%
6:00 PM 1013.4%
7:00 PM 1073.6%
8:00 PM 923.1%
9:00 PM 882.9%
10:00 PM 893.0%
11:00 PM 953.2%
Total 3013100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.94158
Monday 4.80384
Tuesday 4.08477
Wednesday 7.47680
Thursday 6.25675
Friday 5.02462
Saturday 4.78177
Total 5.413013

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.02295
10:00 AM 5.63276
11:00 AM 4.20193
12:00 PM 6.14350
1:00 PM 3.17133
2:00 PM 5.00230
3:00 PM 7.62320
4:00 PM 4.00108
5:00 PM 6.04169
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.413013

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles