CAUTION!

CAUTION! DANGER ZONE ahead. Beware of misinformation on the open internet. Contents of the site are mere opinions and are not always facts!

Sunday, July 5, 2015

Save l-values in condition checks

Do you remember the value on the right side of an assignment operator is an r-value and the one on the left side is the l-value - quite obvious!

Flip the status quo to stay safe!
Sometimes, the conditional expressions are mistakenly/ unintentionally written as assignment expressions.

if(value = getValue() ) // a bug! - can cost you hours of debugging.
{
       // I'm lucky to be executed almost always!
}

if(value = VALUE)
{

}

Quick Tip: Make sure the l-values lie on the right hand side of the conditional checks.

if(gettValue() = value) // error : cannot assign to r value
{

}

if(VALUE = value) // error : cannot assign to r value
{

}

No comments:

Post a Comment