not i = 0 // Bad code

I encountered this today and thought I’d share as I thought I understood brackets and mathematical precedents but apparently I don’t.

If Not i = 0 Then // This line of code is bad
If Not (i = 0) Then // This line of code is good

The first line evaluates apparently to (not i) = 0. And (not i) is false for anything other than 0

Thus the below code fails:

[code]// This doesn’t work
Dim i as integer = 2
If Not i = 0 Then
MsgBox “True”
Else
MsgBox “False”
End If

// This DOES work
Dim i as integer = 2
If Not (i = 0) Then
MsgBox “True”
Else
MsgBox “False”
End If[/code]

Not the expected answer, but what about:

If i <> 0 Then

you compare 2 values with the = operator
left = Not i
right = 0
left is a boolean
right is an integer
so wrong code.

Nothing wrong with that code. Try this:

Dim i As Integer = 2 i = Not i print i.totext

which will give you -3.
It’s always better to be explicit about what comparison to check. Not i inverts i, and -3 is not 0.

[quote=341438:@Ulrich Bogun]Nothing wrong with that code. Try this:

Dim i As Integer = 2 i = Not i print i.totext

which will give you -3.
It’s always better to be explicit about what comparison to check. Not i inverts i, and -3 is not 0.[/quote]
???

Not sure what your point is, Ulrich. Your example agrees with Jean-Ives who said “you compare 2 values with the = operator”

I think his point is that he’s showing a Bitwise NOT vs Logical negation

IMHO this is also bad:

If Not (i = 0) Then

and should be

If i <> 0 Then

as its so much easier to read.

http://developer.xojo.com/not-equals

I agree that

If i <> 0 Then

is generally easier to read.

In my case, I had a multi part if that felt better with a not

If not SomeMethod and not level = 0 then

Which should actually have been

If not SomeMethod and not (level = 0) then

Yup, that is a pretty good use.

Making a statement into a readable sentence is always good for readability :slight_smile:

bah. Readable code… Just do some Perl for fun…

…
for (my $i = @pids; $i>=0; $i--) {
	if ($pids[$i] =~ /^someprocess/) {
		$pids[$i] =~ /^\\S+\\s+(\\d+).+/;
		$pids[$i] = $1;
		next;
	}
}
…

Oh well, It´s friday and urgent need for the weekend… :wink:

I always try to avoid NOT as negations make code harder to understand, eg

If loaded = false

Instead of

If NOT loaded = true

The negation requires some mental arithmetic that can be a cause of annoying bugs when you start combining them.

http://developer.xojo.com/operators
Not has higher precedence

[quote=341466:@Markus Winter]I always try to avoid NOT as negations make code harder to understand, eg

If loaded = false

Instead of

If NOT loaded = true

The negation requires some mental arithmetic that can be a cause of annoying bugs when you start combining them.[/quote]
I agree that that requires an unnecessary little mental step. But it can read nicely if you keep it simple.

[code]if not loaded then

if not firstRun then

etc[/code]

I would write

If (not SomeMethod) and (not (level = 0)) then

I always put ( ) because I’m afraid of

If not SomeMethod and not (level = 0) then

be interpreted as

If not (SomeMethod and not (level = 0)) then

Even simpler:

[code]if loaded then

if firstRun then

etc[/code]

:wink: