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-coalescing 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

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .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.

Related Posts

How to fix the error "Authentication failed because the remote party has closed the transport stream"

This post will show you how to fix the error "Authentication failed because the remote party has clo…

Read Post

How to solve the error Could not load file or assembly System.Security.Principal.Windows

This post help you solve the error System.IO.FileNotFoundException Could not load file or assembly S…

Read Post

How to solve the error "Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0 ..."

This post will show you how to solve the error "Could not load file or assembly 'Microsoft.CodeDom.P…

Read Post

Boosting your career by creating a personal brand

In this post I share the content of the talk I did at the Umbraco UK Festival 2018 about how to boos…

Read Post