daniel aagentah
daniel aagentah

Reputation: 1702

VB.NET - 'If Value <>' vs 'If Not Value ='

I understand that both of the following do the same thing

If Value <> and

If Not Value =

But I noticed the other developer at my job seems to use

If Not Value =

I was wondering if there is any difference in the logic or efficiency between them and if so which one would be the better one to use.

Upvotes: 1

Views: 1906

Answers (2)

Martin Verjans
Martin Verjans

Reputation: 4796

If we were twenty years ago, I would have told you to use

If Value <>

Because the CPU would have done it one operation faster, as it is just comparing the equality flag after doing the maths. Using If Not Value = will result in a negation, then a comparison with the equality flag.

But since computers can do over 2 billions of such operation in a second, it doesn't really matters anymore.

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460238

Chose whatever you find more readable in your case. It's not about efficiency but readability.

Avoid double negatives like:

Dim isEqual = Not Value <> OtherValue

instead use:

Dim isEqual = Value = OtherValue

So both do the same and are are equally efficient. But the former is more error-prone because it's more difficult to grasp.

Upvotes: 3

Related Questions