It drives me crazy when I see code where someone is checking a boolean to see if it is true or not and they write it like this.

  if(isValid == true)
{
//Do something.
}

Noooooo. Stop it! Of course true equals true. Just eliminate the condition part and check the value of the boolean.

  if(isValid)
{
//Do something.
}

If you just want to check for false, then do this

  if(!isValid)
{
//Do something.
}

If you know anyone who is doing this wrong, share this post with them.

UPDATE

It has been pointed out to me by many people on LinkedIn and the wider internet that I failed to mention the problems with dynamic languages like JavaScript and PHP, as well as the problems with nullable booleans in stongly typed languages like C#.

Why this doesn't work with PHP and JavaScript?

//Without Equals:
$boolean = true - if ($boolean) {} //true
$string = (string) 'Hello world' - if ($string) {} //true
$integer = (int) 2345 - if ($integer) {} //true
$class = new stdClass() - if ($class) {} //true
$boolean = false - if ($boolean) {} //false
$string = (string) '' - if ($string) {} //false
$integer = (int) 0 - if ($integer) {} //false
$class = null - if ($class) {} //false
//With double equal (==):
$boolean = true - if ($boolean == true) {} //true
$string = (string) 'Hello world' - if ($string == true) {} //true
$integer = (int) 2345 - if ($integer == true) {} //true
$class = new stdClass() - if ($class == true) {} //true
$boolean = false - if ($boolean == true) {} //false
$string = (string) '' - if ($string == true) {} //false
$integer = (int) 0 - if ($integer == true) {} //false
$class = null - if ($class == true) {} //false
//With triple equal (===):
$boolean = true - if ($boolean === true) {} //true
$string = (string) 'Hello world' - if ($string === true) {} //false
$integer = (int) 2345 - if ($integer === true) {} //false
$class = new stdClass() - if ($class === true) {} //false
$boolean = false - if ($boolean === true) {} //false
$string = (string) '' - if ($string === true) {} //false
$integer = (int) 0 - if ($integer === true) {} //false
$class = null - if ($class === true) {} //false
If you want to check that it is really a boolean which is true in PHP you have to do === true so that it checks the type.

Thanks to Mike B for putting this in the comments.

 

 

 

Why this doesn't work for nullable booleans.

A case for using

if(isValid == true)

If you later want to change to a nullable bool. If you declared:

bool? isValid;

and then tried:

if(isValid)

you wouldn't be able to compile. If you cast using:

if((bool)isValid)

you would get a runtime error. However, if to begin with, you were using:

if(isValid == true)

everything would be fine.

Thanks to Adam for writing this in the comments and on LinkedIn.

 

FURTHER UPDATE AND SUMMARY

From all of the comments around the web that I've managed to read, I have found:

Some people choose to write them like this for style purposes and readability:

if(isValid == true)
{
//Do something.
}

Some people like to write them like this to make sure they don't get caught out by using a single equals sign

if(true == isValid)
{
//Do something.
}

From now on, if I'm using nullables in C# I will use the null-coalescing operator. Read more about these in my blog post Null-coallescing operator.

if(isValid ?? false)
{
//Do something.
}

If I'm writing in JavaScript or PHP I will use the triple equals

if(isValid === true)
{
//Do something.
}

If you would like to know more, why not use your 10 day free trial with Pluralsight to go through Clean Code: Writing Code for Humans

Paul Seal

A .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Web: codeshare.co.uk

Comments