Spot The Problem
You could probably do this in most languages and it might be hard to spot. See for yourself.
Sometimes it takes quite a while to spot a silly mistake you’ve made. This was one of them. See if you can spot what I did wrong:
if (x == 3 && y == 2) { val = 100; } else (z == 4 && w == 1) { val = 200; }
I kept wondering why the flow of execution was going into the second block. I didn’t spot what seems so obvious now that I’ve looked at it again a few times: I omitted the “if” after the “else”! The compiler basically sees this:
if (x == 3 && y == 2) { val = 100; } else { (z == 4 && w == 1); val = 200; }
That would be totally fine as well, if not completely pointless. You’re free to evaluate an expression to a boolean and then throw away the result. In other cases this could actually result in something, such as if you called a function during the execution. In my case it seems like I should have gotten a compiler warning. MXMLC did not give me one though and it won’t give you one either. JavaScript doesn’t even have a compiler to give you such a warning, but you’ll get the error just as surely.
In C this is an error. I tried it out with GCC 4.0 and got:
error: syntax error before ‘{’ token
There may be other languages where this is legal though. Hopefully not too many. Hopefully you’ll be able to spot this error quicker than I did the first time around.
#1 by rauri on September 26th, 2009 ·
it was the syntax highlighting of the x and y (blue) versus the z and y (black) that drew my attention to the z, and therefor made me notice it… totally accidental. i assume x and y are blue on this blog because they are display properties…
#2 by jackson on September 26th, 2009 ·
Good catch! I’m not entirely thrilled about the syntax highlighter I’m using. You’re definitely right that x and y are highlighted because they are properties of DisplayObject. Maybe I should have used a, b, c, and d…