December 4, 2007

Put Constants To Left

To prevent making errors when you get tired, you may try to get a habit of writing constants to left in comparisons. This might save loads of time when trying to find this irritating problem. Let's say you have a comparison like below

if(phone_number = 123)
{
// Answer it
}


Yes. This is wrong. This is not a comparison. However, compiler will accept this and you might forget this in the code. If you have the habit of writing constants to left like below, you will get a compiler error.

if(123 = phone_number)
{
// Answer it
}


Then you can fix the line by replacing "=" with "==".

if(123 == phone_number)
{
// Answer it
}