Home
Do you avoid Double-Negative Conditionals in if-statements?
  v1.0 Posted at 25/04/2018 8:06 AM by Tiago Araujo

Try to avoid Double-Negative Conditionals in if-statements. Double negative conditionals are difficult to read because developers have to evaluate what is positive state of two negatives. So always try to make a single positive when you write if-statement.

​if (!IsValid)
{
// handle no error
}
else
{
// handle error
}​

Figure: Bad e​xample​

if (IsValid)
{
// handle error
}
else
{
// handle no error
}

Figure: Good example​

if (!IsValid)
{
// handle error
}

​Figure: Another good example

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.