2

Given that a typical coding mantra is "Don't induce side effects in method calls." and that the only reason (that I know off - please enlighten me if I'm wrong) to not use short circuited operators is when you depend on the side effects of a method call in subsequent code. Why isn't the default operator in languages like C# and VB.NET not a short circuited version?

IE:

 if (Method1() & Method2()) {
 }

 if Method1 And Method2 then
 End if

 if (Method1() | Method2()) {
 }

 if Method1 Or Method2 then
 End if

Would actually (by default) mean

 if (Method1() && Method2()) {
 }

 if Method1 AndAlso Method2 then
 End if

 if (Method1() || Method2()) {
 }

 if Method1 OrElse Method2 then
 End if
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
  • 1
    C# does use short-circuit evaluation. That said, this question looks like a duplicate of http://stackoverflow.com/questions/1445867/why-would-a-language-not-use-short-circuit-evaluation – jjlin Feb 05 '12 at 06:09

1 Answers1

0

Well there are two different reasons and two different answers I think.

For C# (and most Related older languages) the single ampersand or single pipe actually does bitwise operations against variables (bitwise as opposed to logical). That's why in all C/C++ code when someone wants a logical and you will see the double ampersand in their code.

For VB.NET I believe the answer is just history. Legacy Basic languages have always used "and" and "or" to do non-short circuit operations. It was an extremely poor decision but it's been carried all the way up to VB.NET.

It does bear mentioning that all the Basic languages utilize the "and/or" keywords to do bitwise operations as well. In older Basics there was no equivelant of the AndAlso or OrElse keywords to force short circuit so you had to nest If statements to get the logical equivelant of a short circuit and.

EverPresent
  • 1,903
  • 17
  • 21
  • 2
    No, in C# the single | and & operators work on bools just fine. They're logical operators on bools and bitwise operators on ints. There is no requirement that a logical operation on bools *also* be short-circuiting. – Eric Lippert Feb 05 '12 at 08:59
  • They do work on Boolean values. I'm just saying that they do bitwise operations even when used against Boolean values. When you use a single & or | on a series of Boolean values it's using bitwise operations and treating them as a series of 1 bit values to be evaluated. This _is_ how the interpreter is implementing the operators. – EverPresent Feb 05 '12 at 13:47
  • 1
    Though obviously yes, that is how the interpreter implements the feature, it is a bit odd to think of a logical operation on Booleans as being "bitwise". One normally thinks of "bitwise" operations as operating on a sequence of bits, not on a single Boolean. The fact that in practice, a Boolean is not implemented as a single bit is an implementation detail. – Eric Lippert Feb 05 '12 at 17:04
  • +1 Whether a Boolean value is a single bit or not, bitwise does the same thing. I prefer thinking of | and & as bitwise with Booleans because it keeps the semantics consistent. – Grault Oct 08 '12 at 21:57