tags:

views:

709

answers:

7

When I'm using an If statement and I want to check if a boolean is false should I use the "Not" keyword or just = false, like so

If (Not myboolean) then

vs

If (myboolean = False) then

Which is better practice and more readable?

+3  A: 

Since there's no functional difference between either style, this is one of those things that just comes down to personal preference.

If you're working on a codebase where a standard has already been set, then stick to that.

nickf
+13  A: 

Definitely, use "Not". And for the alternately, use "If (myboolean)" instead of "If (myboolean = true)"

The works best if you give the boolean a readable name:

 if (node.HasChildren)
James Curran
+2  A: 

! condition

In C and pre-STL C++, "!condition" means condition evaluates to a false truth value, whereas "condition == FALSE" meant that the value of condition had to equal what the system designed as FALSE. Since different implementations defined it in different ways, it was deemed better practice to use !condition.

UPDATE: As pointed out in the comment -- FALSE is always 0, it's TRUE that can be dangerous.

JohnMcG
The false value in C has never been anything but 0.
Darius Bacon
Right now that I think of it, it's TRUE that's a problem
JohnMcG
In other languages, there can be more values that evaluate to false, like Perl and Javascript.
bart
+1  A: 

Definitely use "Not", consider reading it aloud.

If you read aloud:

If X is false Then Do Y Do Y

Versus

If Not X Then Do Y

I think you'll find the "Not" route is more natural. Especially if you pick good variable names or functions.

Code Complete has some good rules on variable names. http://cc2e.com/Page.aspx?hid=225 (login is probably required)

torial
I respectfully disagree - nobody I know of speaks in that way, and when I read code I'm doing exactly that, reading it out loud (but silently - unless it's a real WTF)
endian
That's fair. Although I think ultimately when I read something, I mentally read it aloud ... I guess I prefer graphical, then audio, then text, and that approximates it. Could also explain why I read abysmally slow!
torial
I'd have to disagree too, "If myFlag is false" reads more naturally than "if not myflag". I'm not sure that means I would favour = false over not, because I'm more used to if (!myflag)...
Carl
+2  A: 

Use True and False to set variables, not to test them. This improves readability as described in the other answers, but it also improves portability, particularly when best practices aren't followed.

Some languages allow you to interchange bool and integer types. Consider the contrived example:

int differentInts(int i, int j)
{
   return i-j;  // Returns non-zero (true) if ints are different.
}

. . .
if (differentInts(4, 8) == TRUE)
   printf("Four and Eight are different!\n");
else
   printf("Four and Eight are equal!\n");

Horrible style, but I've seen worse sneak into production. On other people's watches, of course. :-)

Adam Liss
A: 

Additionally to the consensus, when there is both a true case and a false case please use

if (condition)
    // true case
else
    // false case

rather than

if (not condition)
    // false case
else
    // true case

(But then I am never sure if python's x is not None is the true-case or the false case.)

Ted
IMO it depends. I prefer to put the more common case first, if the difference is large. For example: if(it_works) ... else ... and if(not error) ... else ...
bart
Yes, that very much depends. For example, you usually want to have error cases out of the way as soon as possible, i.e. you put them ahead.
Konrad Rudolph
A: 

Something else: Omit the parentheses, they’re redundant in VB and as such, constitute syntactic garbage.

Also, I'm slightly bothered by how many people argue by giving technical examples in other languages that simply do not apply in VB. In VB, the only reasons to use If Not x instead of If x = False is readability and logic. Not that you’d need other reasons.

Completely different reasons apply in C(++), true. Even more true due to the existence of frameworks that really handle this differently. But misleading in the context of VB!

Konrad Rudolph