-
January 12, 2009
AS3: What were they thinking?
CodeI had to use as3 extensively in the last days and I am amazed by its mixture of great improvements (over as2, that is) and extremely bad design choices. One of the new “features” is that blocks (everything inside “{}”) do not have their own scope (except function blocks, of course). Which means that the following
for( var i:int = 0; i < 100; ++i ) { // do something... } for( var i:int = 0; i < 20; ++i ) { // do something else... }will issue an “Warning: 3596: Duplicate variable definition”. Great. Instead one is expected to write
for( var i:int = 0; i < 100; ++i ) { // do something... } for( i = 0; i < 20; ++i ) { // do something else... }thus declaring i only once. While I skimmed various blogs and sites for a solution (I was thinking of some kind of compiler-flag to use normal scopes) I found precious advices like this that told me that the above was “good code”… did I miss some kind of paradigm shift? Since when are sloppy scopes “good code”?! While we’re at it, why don’t we dismiss this whole scope-thing and make every variable global? Yeah, sounds like a great idea! And GOTOs! Let’s have them, too!
But seriously, I got badly annoyed when the following situation occured:
for( var i:int = 0; i < someArray.length; ++i ) { var model:A= someArray[i] as A; // do something with model } for( var i:int = 0; i < otherArray.length; ++i ) { var model:B= someArray[i] as B; // do something with model }I like to write “parallel code”, that is: code that does something similar should looks similar. It helps me memorize my code and it appeals to my aesthetic sense (yes, I think code should look nice). But in AS3 it is simply not possible: the variable model has the type A as of the first declaration and cannot be re-declared in the second loop. This forced me to come up with variable names like modelA and modelB (only that A and B are types with real names like SystemView or Markable) — déjà-vu?
The only thing that I could do to minimize the agony was to turn of warnings (File->Publish->Publish Settings->ActionScript3 Settings) and use the above bastardization of hungarian postfixes. Yuck.
We received one comment. Write the second one!
Right! I want to write a comment. »RSS feed for comments on this post. TrackBack URL


Felix,
Thanks for the link about the precious advice received from http://www.curtismorley.com. It does make sense that code blocks should have there own scope and at the same time I can understand why Adobe created it this way.
I have changed/added some clarity and examples on the site concerning AS3 Error 3596 on my site.
Thanks,
Comment written January 12, 2009 @ 6:11 pmCurtis J. Morley