Comparing literal and variable

suggest change

Suppose you are comparing value with some variable

if ( i  == 2) //Bad-way
{
    doSomething;
}

Now suppose you have mistaken == with =. Then it will take your sweet time to figure it out.

if( 2 == i) //Good-way
{
    doSomething;
}

Then, if an equal sign is accidentally left out, the compiler will complain about an “attempted assignment to literal.” This won’t protect you when comparing two variables, but every little bit helps.

See here for more info.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents