# Boolean Expression Best Practice

Consider a code which we want to execute if the boolean variable flag is true.

# 👍 Best Practice

if (flag)
{
  // if boolean expression evaluates to True
}
1
2
3
4

You know if body is executed only when the condition is true. flag is a boolean variable, if it is true then the body of if will run.

# 👎 Bad Practice

WARNING

The following style of code is bad. Do not use it.

if (flag == true)
{
  // if boolean expression evaluates to True
}
1
2
3
4

If flag is true, then flag == true means true == true, which in turn evaluates to true. Do you see the issue? You are repeating yourself.

If flag is true, then the body of if will run. You do not need to compare flag with true.

Last Updated: May 1, 2020, 3:58 PM