We recently had a bug report that the debugger started issuing a warning in code where it had not before:
The initial fix proposed was to simply change:
But in LZX script, there is one other subtle difference. When you use
We did this because we wanted a way to help users discover type-ohs. We wanted to be able to warn users that they were trying to access a non-existent property — perhaps because the spelled the property incorrectly, or perhaps because the object they were trying to get the property from wasn’t the right object. But we also recognized that there were certain cases where the user needed to test if a property existed or not, without eliciting a warning. There needed to be a way for the user to say “I know what I am doing”. But, if you do this, you really do need to know what you are doing!
As an open source project, OpenLaszlo has lots of people working on it, and sometimes, despite the best intentions, one person working on the code may not fully understand what another person’s code is trying to do. In this case, a well-intentioned change had been mis-applied. A developer who was fixing a dangerous array iteration idiom mistakenly applied it to a value that was an object (being used as a map or hashtable), not an array. Hence the warning about no ‘length’. (They can be forgiven: the code was a doubly-nested loop with no comments and variables with names that gave no clue what they were being used for.)
If we had taken the simple fix — silencing the warning by using the
There are several lessons to be learned:
1) When you get a warning appearing in code that previously worked, you really want to understand why that warning started showing up all of a sudden. The simplest way of silencing the warning may not be the correct fix.
2) When working on a piece of code you are unfamiliar with, you really need to take the time to understand the code before you make changes.
3) When writing a complex piece of code, use mnemonic names for your variables (local names are ‘free’, you don’t make your code smaller or faster by using short names), and if it’s really tricky code, throw in a comment!
 There are a number of other ways to look at object properties, and we used to recommend asking:
So if you need to make a test for the existence of a property that
might be dynamic or might be an instance property, the only way to
cover your bases is to use ‘[‘. In the AS3 compiler, you will get an
error at compile time if you use
 Lots of people take a shortcut in iterating over an array by saying:
Post a comment