Thursday, 10 August 2006
Code WTF
My coworkers and I have been seeing an intermittent bug in some new functionality on our customer facing web app. The new functionality basically provided a customer choice for some value added products.
The bug would surface when the web application evaluated for errors (server-side) and made a JavaScript test (client-side) on return for specific customer information then set focus on a radio button.
Up until the new features were added the error quietly failed and caused no problems. But the new functionality provided new radio buttons for customer choice and the JavaScript began exhibiting a hidden, difficult-to-debug, trait: No matter what choice the customer selected, either the radio buttons would reset to none, or would reset to the first choice made by the customer, even if the customer had changed his choice.
When we pointed the Web Developer Tools for Firefox at the problem we could see that name of the feature's radio buttons were being inexplicably set to something not shown in the HTML source.
It took my following the code through the branching and while loops to see the problem. Do you see it too (spoiler follows)?
... form element loop ...
var currRadio = "";
if( currRadio != elem.name ) {
...
if( !checked ) {
if( elem.name = "residentState" ) {
setStateFocus();
} else {
setFirstErrorFocus();
}
}
}
Moral: JavaScript is not a strongly typed language; "objects" have a runtime state moving from boolean to string to number and back again (sounds like another popular scripting language, hmmm...). Be careful of assignment, as assignment will return true, and that evaluates as boolean.
Anybody think this is worth submitting to The Daily WTF?
