Question Should AND and OR be deprecated or assume AndAlso and OrElse?

Administrator

VB.NET Forum Admin
Joined
Jun 3, 2004
Messages
1,462
Programming Experience
10+
I can't seem to ever find a reason to use "AND" or "OR" ever again. When would someone want to use this over "AndAlso" or "OrElse" comparisons? I realize backwards code but if I'm writing in .NET 4 for example, AND should automatically be AndAlso IMHO.
 
I use 'And' and 'Or' quite frequently with BitWise operations, it's required.
C# even has the equivalent "&" and "|" just for bitwise too

The problem with vb is that while Java, c/c++, C#, etc all use '&&' and '||' for short circuit logic since the beginning, vb didn't and that leads to people still using 'And' instead of 'AndAlso'.
MS could change it so 'And' is short circuited but then not only would that break old code but at this point it would create confusion since many .Net dev's are using 'AndAlso' for short circuit logic.

I vote for keeping things the way they are and continuing to inform others as needed.
 
VB.NET has been designed with upgrading VB6 code as painlessly as possible in mind. To that end, having And and Or behave the same way they did in VB6 makes sense. Making And and Or short-circuit in VB.NET would either break some upgraded VB6 code or require every upgraded VB6 application to have every instance of each operator replaced with something else to maintain the same behaviour. With all the whining that came from some VB6 developers when they were "abandoned", that might have been too much.

By the way, JB is incorrect about & and | being just bitwise operators in C#. They are also non-short-circuiting logical operators, just like And and Or:
VB.NET:
private void button1_Click(object sender, EventArgs e)
{
    if (Method1() & Method2())
    {
        MessageBox.Show("Both true");
    }
    else
    {
        MessageBox.Show("At least one false");
    }
}

private bool Method1()
{
    MessageBox.Show("Method1");
    return false;
}

private bool Method2()
{
    MessageBox.Show("Method2");
    return true;
}
 
From a purely personal point of view, I also program many different types of PLCs. All the PLC software versions I use have not yet introduced AndAlso / OrElse, and it's difficult enough at the moment - hence why you'll frequently find AND / OR in my .NET code when I know shortcuts would work properly. If AND / OR were deprecated in .NET, it would make my life even more complicated :)

I agree with JB, keep it the same.
 
All the PLC software versions I use have not yet introduced AndAlso / OrElse, and it's difficult enough at the moment - hence why you'll frequently find AND / OR in my .NET code when I know shortcuts would work properly. If AND / OR were deprecated in .NET, it would make my life even more complicated :)
The question is rather if AND operator should be changed to have shortcircuit behaviour by default, so you wouldn't have to change any code. Only if the code depended on both operands being evaluated always, even if only the first expression would determine the combined result, would the code require change. This is one of two main reasons the language team decided to introduce new logical operators instead of changing the behaviour.

jmcilhinney said:
By the way, JB is incorrect about & and | being just bitwise operators in C#. They are also non-short-circuiting logical operators, just like And and Or:
As for bitwise it was explained that logical AND is really a bitwise operation because of the bit values of True/False, further (consequently) they decided it was a bad idea to have two different behaviours for same operator (depending on operand types).

I found this article/blog by Paul Vick software architect of the VB Development team only in Google cache Why did we introduce AndAlso and OrElse?, and therefore have recited the main content here.
 
By the way, JB is incorrect about & and | being just bitwise operators in C#. They are also non-short-circuiting logical operators, just like And and Or:
VB.NET:
private void button1_Click(object sender, EventArgs e)
{
    if (Method1() & Method2())
    {
        MessageBox.Show("Both true");
    }
    else
    {
        MessageBox.Show("At least one false");
    }
}

private bool Method1()
{
    MessageBox.Show("Method1");
    return false;
}

private bool Method2()
{
    MessageBox.Show("Method2");
    return true;
}
That's what I was getting at, just used the wrong words. C# has the non-short circuited & and | operators, which are most commonly used to Bitwise but doesn't have to strictly be for just that.

As for bitwise it was explained that logical AND is really a bitwise operation because of the bit values of True/False, further (consequently) they decided it was a bad idea to have two different behaviours for same operator (depending on operand types).
I personally like having the 4 operators of 'And', 'AndAlso', 'Or' and 'OrElse', for me it makes knowing what I'm doing in the code a little more clear, for example if I'm ysing 'And' then I know I'm specifically not wanting short circuited logic to be used their and I really want both (or all) items to be performed, or I'm doing Bitwise.
 
Good info JohnH. I understand the backwards compatibility point of view, however, if I'm creating a new .NET 4 app today, I could probably be just fine with AND having the AndAlso functionality. As to coming from other languages and not being used to AndAlso / OrElse, well, we have to learn new syntax with any language transition. VB'ers need to know || is OrElse and && is AndAlso, so it's kind of a moot point there.

I just can't see any use for AND or OR in comparison logic, not getting into the bitwise stuff, just the logic piece.
 
Back
Top