0

All,

In VB .net I usually test a True or False scenario as below:

' dict is a dictionary
If dict.ContainsKey(anID) = False Then
   ' Do something
End If

I know that this test could also be written as such:

' dict is a dictionary
If not dict.ContainsKey(anID) Then
   ' Do something
End If

I have often wondered if one approach is faster than the other? I've had a search of the Internet but failed to find any comparison of the two approaches. I tend to use the first example as I think its easier to read but if anyone has evidence that the second approach is faster I would be interested in hearing. Typically these are embedded in loops that may iterate thousands of times so I think I would put performance over legibility in this case.

Hornbydd
  • 321
  • 2
  • 12
  • 5
    You are in the best position to test your own code. Put a Stopwatch object around your loops and measure the two options. Then you can evaluate if it is better readability over the performance increase that you eventually get. A quick glance to a not optimized IL code shows that the two methods are translated to the same IL code – Steve Feb 02 '20 at 18:16
  • 2
    `I tend to use the first example` The programming community has a tendency to prefer the second approach. – LarsTech Feb 02 '20 at 18:18
  • 1
    On top of that, the first approach would [yield wrong results sometimes](https://stackoverflow.com/q/57374918/11683) in VB6. This does not appear to be the case anymore for VB.NET. – GSerg Feb 02 '20 at 18:32
  • If you tend to use `If x = True Then` then you should use `If x = False Then`. If you tend to use `If x Then` then you should use `If Not x Then`. Code consistency is the more important factor here. If there is a difference in performance then you'd have to execute the comparison a huge number of times to actually see. That is also how you should have tested this for yourself and then not even had to ask the question. Never ask a question when you can test for yourself. – jmcilhinney Feb 03 '20 at 01:22
  • @GSerg: The two can produce different results, but the first approach seems correct and the second approach is the one that goes haywire if the input value isn't `True` or `False`. – Ben Voigt Jun 01 '22 at 18:18
  • @BenVoigt I don't think comparing a bool to false or true explicitly is ever correct. If it was correct, the comparison expression must have been infinitely long each time (`If ((x = true) = true) = true ...`). And it goes haywire very well, if you put 42 into the storage space of a `Boolean`, both `= True` and `= False` will be false. – GSerg Jun 01 '22 at 20:13
  • @GSerg: You're confusing "correctness" with "good style". Checking for equality with True is indeed incorrect. The correct-but-unnecessarily-verbose comparisons are "equal to False" and "not equal to False". `Not` (which is bitwise-complement in VB) is also wrong if someone has stored `42` into a variable intended to be a boolean. `42` is a truthy value, unfortunately so is `Not 42`, but `42 = 0` and `42 <> 0` give the correct answer. – Ben Voigt Jun 01 '22 at 20:50

2 Answers2

3

These produced the same intermediate code via ildasm.

Here's the code I tried:

Sub Main()
    Dim x As Boolean
    If x = False Then
        Console.WriteLine("hello world")
    End If
End Sub

and this second code

Sub Main()
    Dim x As Boolean
    If Not x Then
        Console.WriteLine("hello world")
    End If
End Sub

Both produce this

.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       23 (0x17)
  .maxstack  2
  .locals init ([0] bool x,
           [1] bool V_1)
  IL_0000:  nop
  IL_0001:  ldloc.0
  IL_0002:  ldc.i4.0
  IL_0003:  ceq
  IL_0005:  stloc.1
  IL_0006:  ldloc.1
  IL_0007:  brfalse.s  IL_0015
  IL_0009:  ldstr      "hello world"
  IL_000e:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0013:  nop
  IL_0014:  nop
  IL_0015:  nop
  IL_0016:  ret
} // end of method Module1::Main
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
2

The "If not" method is fine, but for me, I like the "= false" because it's just easier to see when looking through large amounts of code. Same with C .. the whole if(!boolean) is easy to overlook. Much harder to miss when it's if(boolean == false)

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
bsp01
  • 21
  • 1