Avoid == vs. = accidents

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Avoid == vs. = accidents

Post by Jackolantern »

I just read this technique in a book, and I can't believe I never thought of it before (perhaps because I have not used many languages that require "$x == 5" for evaluation). Anyone who has used PHP or a similar language for really any length of time has made the mistake of writing code like this:

Code: Select all

if ($x = "value") {
    //do something
}
It can be hard to spot the problem. The "Do something" part will always run. That is because I just assigned "value" to $x, instead of checking to see if $x was equal to "value"! Since an assignment always returns true, this IF statement will always run. These can be terrible bugs to track down, because your brain sees the = sign, and since you see so many single = signs, your brain accepts it as correct and moves on. I have sometimes debugged a script for hours trying to find what is wrong before noticing it.

So what can you do to make sure this won't happen? Change to a language that doesn't allow assignment in conditional expressions! Just kidding of course(although I do really wish they would make code like this illegal, like in Java and C# so that you could actually perform equality checks with only one =)!

Make a habit to always put your literals on the left[/i] side when you are comparing values, like this:

Code: Select all

if ("value" == $x) {
       //....
}
Because if you only enter one = sign, you will have this:

Code: Select all

"value" = $x;
Since you obviously cannot assign a variable to a literal, this causes a compiler/interpreter error! This is an extra level of error detection.

I am working on getting into the habit of this as we speak! It may take time to break, since I have been doing it the other way around for years :P
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Avoid == vs. = accidents

Post by hallsofvallhalla »

friggn sweet!

After fighting with this a couple times and searching my code for hours only to end up with a slap to the forehead over this error I have trained my brain to catch it. Well most of the time. But will definitely go this route in the future.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Avoid == vs. = accidents

Post by Jackolantern »

Well, I have to say that at least for me, it has been very hard to break myself of the "variable on the left" method. I have been writing code that way for nearly 20 years, going back to BASIC on the Apple IIe :shock: I just write it without thought!

But I think it is more than worth changing over. I know what you mean about training your eye to catch it, but it can still be hair-pulling for however long it takes to find it (and it can cause some weeeeird logic errors in some cases that are hard to track down). I will just have to keep reminding myself to reverse them in all cases, even when not in a conditional so it becomes more natural!

I wonder why I never thought of that before? It seems so obvious, but I had to read it out of a book to even think about it :lol:
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Avoid == vs. = accidents

Post by hallsofvallhalla »

well i said I trained my self, I just made the mistake again :P, luckily it only cost me about 5 minutes :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Avoid == vs. = accidents

Post by Jackolantern »

A small price to pay for one of those errors! One night I spent over 6 hours hunting one of those down that I had made 2 function calls down the stack! :?
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Avoid == vs. = accidents

Post by hallsofvallhalla »

yikes!
User avatar
kaos78414
Posts: 507
Joined: Thu Jul 22, 2010 5:36 am

Re: Avoid == vs. = accidents

Post by kaos78414 »

Also might be worth mentioning the difference between "==" (equal) and "===" (identical), though I may be a bit off-topic.

Example:

Code: Select all

if ("10" == 10) echo "yes.";
else echo "no.";
Will return yes, but

Code: Select all

if ("10" === 10) echo "yes.";
else echo "no.";
Will return no, because the left operand is missing the quotes.

I honestly don't use this very much... But the more you know right?
w00t
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Avoid == vs. = accidents

Post by Jackolantern »

The identity operator (===) is useful for telling the difference between 0 and false. Since PHP is weakly-typed, 0 is the same thing as false in typical conditional statements. However, if you actually need to know if it is 0 or false, you can compare with ===.

As you said, though, honestly uses for it don't come up very often, but when you need it, there is usually little other options.
The indelible lord of tl;dr
Post Reply

Return to “Coding”