I still see too many people use And and Or when beginning with VB.Net.
Since the introduction of VB.Net, way back in 2002, there have been AndAlso and OrElse. In most cases you should prefer them over And and Or.
Now why would they have picked a less logical name for something you should actually use more? As always it is simple, backward comparability. And it goes back to the beginning of the B in VB. The keywords and their behavior has been in there since pretty much the beginning. So why change it? It would seem logical now to change it, but it wasn’t way back in 2002.
You can read the exact reasoning from Paul Vick himself.
When working with reference types you should probably always use AndAlso. Because it is more of a no-brainer than And. Just look at this:
If person IsNot Nothing And person.Name.Equals("Baes") Then
The above code will throw an exception when person is Nothing. Because both sides of the And will be evaluated no matter what. To avoid this you could write:
If person IsNot Nothing Then
If person.Name.Equals("Baes") Then
But that seems wrong.
Or you could write.
If person IsNot Nothing AndAlso person.Name.Equals("Baes") Then
And not get an exception because AndAlso will only execute the first part, see that it doesn’t comply with the rules and will no longer evaluate the second part.
It all seems simple enough and it is simple enough. I just see that people keep forgetting it.
BTW: Paul Vick is now on the T-SQL team, and we know there is lots to improve there ;-).