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

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.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.