Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

There is a colleague of mine who constantly writes:

if (someBool == true)

It drives me up the wall! Should I make a big deal of it or just drop it?

share

locked by Yannis Rizos Mar 13 '12 at 20:42

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

12  
I prefer the explicit if (some_flag == true) but the implicit if (is_something) or if (has_something). Note the variable names. –  FredOverflow Oct 18 '10 at 21:44
69  
Remind your colleague that the type of someBool == true is also boolean, so by the same logic it should be if ((someBool == true) == true). –  Mike Seymour Oct 19 '10 at 0:00
2  
@GSto: I suppose you know it, but in PHP you can do that to "cast" anything to a bool and can be actually useful. –  zneak Oct 19 '10 at 2:21
2  
@zneak Or you could be clearer and use $var = (bool) some_expression. And in most cases, it won't even matter as PHP will do the necessary conversions dynamically. –  waiwai933 Oct 19 '10 at 3:15
4  
always check the constant first, if (true == someBool), in case you accidentally typed = instead of == [yes i'm kidding!] –  Steven A. Lowe Oct 19 '10 at 17:45
show 8 more comments

47 Answers

It's a code smell for a novice developer. They have yet to master/internalize the boolean condition and must explicitly evaluate to the true or false condition.

share
add comment

In JavaScript, I prefer if(bool) when I have sufficient documentation explaining what the method expects... but when evaluation of true can mean the variable exists, equals the number 1 or the boolean true, it's tougher.

Strongly typing in JS also means less flexibility. Tough call. Strongly typed languages, lay down the hammer. Otherwise, ask why he does it?

Actually, there's your answer: ask him why. Then correct him.

share
add comment

use it as an opportunity to teach him. he will respect you more if you do. wouldn't you want someone to do the same for you?

share
show 1 more comment

Research shows that code with many redundancies correlates strongly with errors. Here's an interesting paper on the matter (PDF alert).

share
add comment

Yes, This is a kind of programmers mind set. All the time they considers "if condition" has no existence with out an expression with symbols =,<,>... I've seen situations people really get struggle as like "function return"

if(RowsAffected > 0) { return true; } else { return false; }

Programmers need to watch their code from the real time angle.. :)

share
1  
That means their understanding of the language is lacking, which is a real problem –  JoelFan Oct 19 '10 at 13:14
add comment

Yes, it's a big deal because the name of the boolean should tell that it is boolean. Says if you have this piece of code:

bool arrayIsEmpty = array.length == 0;
if (arrayIsEmpty) { ... }

It is already easy to read so you won't need == true, even for the person or programmer with less sense of evaluation.

share
add comment

Programming conventions are not necessarily "right" or "wrong". They exist to give the program a familiar structure to those reading it, so that possible mistakes will stand out more clearly -- it is difficult to "see" a problem if everything is slightly wrong-looking to you.

The important thing is that conventions be clearly defined and agreed upon by all participants (even if the agreement is like "I agree to use this convention while I work here" :), otherwise it does more harm than good.

Of course, the particularly case of comparing a boolean to true is plain wrong and to be avoided at all costs. ;-)

share
add comment

I write if(var==false) because after writing var i dont feel like backtracking and write ! behind it. Also i like how ==false makes it look more clear.

However == true is just weird. I dont know if there is a point of making a big deal. I wouldnt unless he is pushing others to use it or to make it a coding standard.

share
add comment

You can try approaching your colleague with the following example:

if ((x == 3) == true) {
    ...
}

He should say that the == true is redundant and should be rewritten as

if (x == 3) {
    ...
}

since x == 3 is already a boolean expression.

Now present him the someBool == true example, but slightly rewritten:

if ((someBool) == true) {
    ...
}

Since someBool is already a boolean, by comparison with the previous example, it should now be clear that the == true is redundant in this example, too. Thus, it should be rewritten as:

if (someBool) {
    ...
}
share
add comment

This may get some of your nickers in a wad.

We have extension methods for .IsTrue and .IsFalse for the bool type.

So we write

if(someBool.IsTrue())

-or-

if(someBool.IsFalse())

Yep! And I love them. When reading code it just becomes more obvious to me.

share
add comment

Comparing booleans can actually cause major problems. I recently ran into some intern code that read:

if(foo == false && bar == 2) {...}

Now, this seems pretty straight-forward, right? Just simplify it down to:

if(!foo && bar == 2) {...}

Wrong. In this case, order of operations dictates that the && gets evaluated first, meaning that this really evaluates to:

if(foo == (false && bar == 2))

also known as

if(!foo)

That extra thing we were supposed to be checking for, bar == 2, is totally gone. That is why I will never, ever approve a code review that contains someBool == [true|false].

(You can see how the reverse statement, == true and ||, would also cause problems.)

share
add comment

Coleague of mine called if a loop. I lived to tell about it.

share
add comment

Hopefully, you are using a sensible language, like VB.NET with Option Strict On, which doesn't allow coercion of other types to Boolean inside an If statement. Then there's no need to ever add the "== true" (or "= True" in VB), since the coercion case can't happen. If you want to check for non-zero then write that explicitly (ie., "If x <> 0").

share
add comment

Funny because I always replace code like:

if(foo) {

With:

if(true == foo) {
if(false == foo) {

But I got this habit because most of the time the code I worked with didn't have clear variable names.

share
show 1 more comment

One issue in C is that there's a tradition of using values other than 0 and 1 for booleans.

typedef enum { WHATEVER = 1 << 4 } MyFlags;
if (flags & WHATEVER) {
}

or even relying on -1 being true.

The problem is say you're writing an API:

struct foo { 
  unsigned int bar : 1;
};

void myapi_foo_set_bar(struct foo *foo, int bar) {
   foo->bar = bar;
}

Here if bar isn't canonicalized to 0 or 1 it won't fit in the bitfield and it breaks:

myapi_foo_set_bar(foo, flags & WHATEVER); /* broken */

You can find other ways non-canonical bools break as well, such as comparing them to TRUE or FALSE as in the question!

Anyway what I'm coming around to is that it often makes sense to canonicalize a bool:

foo->bar = bar != FALSE;

This is a C-specific weirdness, of course.

share
add comment

One can develop this habit when do a lot of programming in WPF. It uses lots of bool? (nullable bool) variables to you have to write either if (someBool == true) or if (someBool ?? false)

share
add comment

Consider yourself lucky, my half-monkey colleague writes

if (true)

I found it many times in his code. And that's not the worst thing I found. I found GOTOs. And we use C#. Now calm down and hold back your homicidal instincts.

share
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.