Patrick M Brennan
Home | Writings | Resume | Links | RSS Feed
A Proud Member of the Reality-Based Community
About Me : I'm a grownup nerd living in the Boston burbs. I write computer programs for a living and plays for fun. I'm married to a wonderful woman, and we share a nice little house with our daughter and our cats. I'm a humanist, a technologist, an artist, and an idealist. I believe in reason, freedom, love, equality, and democracy. (Did I mention that I'm an idealist? I did, OK.) I'm also a pragmatist and an empiricist. I reject ideology and dogma, especially when they conflict with practical facts (i.e., pretty much always). I particularly hate willful ignorance, which tends to go hand-in-hand with ideology and dogma.
Like the alignment of the planets, this blog gets updated as I have the time, inspiration, and inclination to do so.

Wednesday, January 28, 2004

Still More Stupid ActionScript

(is that redundant?)

I just spent several hours struggling to fix a bug in my Flash code. In the end, I was left shaking my head in disbelief that I can get into this much trouble so easily, and the ActionScript compiler will do nothing to help me out.

Let me show you what's happening here. I had written the following code:

myObject.someFunction(arg1, arg2)
{
// ... body
}


My movie compiled and executed flawlessly. Flawlessly, that is, except that for some reason, the function in question didn't seem like it was getting called. When I bolstered the instrumentation on the code, I was able to verify that it wasn't getting called. But how could that be? I had declared the function, I hadn't created any syntax errors, I verified that the caller was trying to call into the function correctly. All along, Flash hadn't issued a single complaint about my programming, but by now I've learned that's not a surprise.

Want to see what the change is that made it work? Here it is:

myObject.someFunction = function(arg1, arg2)
{
// ... body
}


See the difference? At no point did Flash issue a warning, much less an error, because the first code block is perfectly legal ActionScript. It just means, "this one time, in this one frame, call this object method that doesn't exist, using these variables that don't exist as arguments; then execute this block of statements which I have conveniently placed inside curly brackets." The second block means "create a function which takes these arguments and executes these statements, and assign the function to this object property as a method." I suppose there are circumstances under which one might want to execute the first block, but every time it has occured in our code, it has occured as an accident, because we are mostly used to Java and C++, and the first block's syntax is closer to how we think of a class method definition than the second.

Now bear in mind that I had to scrupulously examine hundreds of lines of code in order to determine that this was where my error hid. This one typo cost me hours. A simple warning in the compiler would have saved me those hours.

posted by Patrick M Brennan 11:39 AM | link

Patrick M Brennan Programmer, Playwright, Righteous Geek