« Which Storage Devices Are Considered Removable? | Main | Four New Properties Added to Socket »
December 2, 2009
Labels in ActionScript 3
There's a little-known feature of ActionScript 3 called labels which can give you better control over nested loops. In particular, labels give you control over the depth of your continue and break statements.
Consider the following piece of code which compares two arrays to find which elements exist in outerArray that do not exist in innerArray:
var outerArray:Array = ["coffee", "juice", "water", "beer"];
var innerArray:Array = ["liquor", "beer", "wine", "juice"];
for each (var foo:String in outerArray)
{
var found:Boolean = false;
for each (var bar:String in innerArray)
{
if (foo == bar)
{
found = true;
}
}
if (!found)
{
trace(foo);
}
}
(Note: I know there are better ways of doing this particular comparison; this technique is for demonstration purposes only.)
The same code could be written using labels like this:
var outerArray:Array = ["coffee", "juice", "water", "beer"];
var innerArray:Array = ["liquor", "beer", "wine", "juice"];
outerLoop: for (var i:uint = 0; i < outerArray.length; ++i)
{
var foo:String = outerArray[i];
innerLoop: for (var j:uint = 0; j < innerArray.length; ++j)
{
var bar:String = innerArray[j];
if (foo == bar)
{
continue outerLoop;
}
}
trace(foo);
}
Notice how the labels outerLoop and innerLoop precede my for loops, and how I can reference the outerLoop label in my continue statement. Without specifying a label, continue would have continued from the top of innerLoop rather than outerLoop.
Labels will also work with break statements, and can even be used to create their own blocks as in the following example:
dateCheck:
{
trace("Good morning.");
var today:Date = new Date();
if (today.month == 11 && today.date == 25) // Christmas!
{
break dateCheck;
}
trace("Time for work!");
}
Some things to keep in mind about labels:
- You can't use a
continuestatement in the context of a labeled code block since the result would be an infinite loop. This will be caught by the compiler. - You obviously can't reference labels that don't exist. The compiler will catch your mistake and let you know that the target wasn't found.
- In using labels yesterday for new application I'm working on, I discovered that they don't play well with
for each..inloops. If you're going to use labels, you'll want to make sure you're using regularfororwhileloops. (A bug has been filed and will hopefully be fixed soon.)
Posted by cantrell at December 2, 2009 6:23 AM
Comments
Wow - I had no idea this feature was in AS3. Cool! But, kind of terrifying in terms of code cleanliness. :)
Posted by: Ben Garney at December 2, 2009 10:36 AM
Wow, I didn't know about labels in AS3. It looks like goto instruction from others, older, programming languages ex. pascal, but it's not recommended to use it if i remember. It looks good and simplier in your example but i'm not sure what will happen when someone start using this construction in the whole project in more complicated loops. I'm convinced it affect code readability.
Posted by: Dawid Górny at December 2, 2009 11:03 AM
Wow, that's really awesome! It's amazing that this managed to slip under the radar for so long. Thanks for the post on it!
Posted by: JTtheGeek at December 2, 2009 11:16 AM
wow, this is very cool! thanx a lot!
Posted by: xero at December 2, 2009 11:19 AM
Reminds me of goTo Statements, which i consider pretty ugly :)
Posted by: Timo at December 2, 2009 11:29 AM
Did you do any performance test with that?
What kind of bytecode is it creating when using labels?
Is it a low-level API or a "flash shortcut"?
Posted by: jpauclair at December 2, 2009 1:23 PM
I almost wish you hadn't blogged about this particular topic. The ratio of ActionScript programmers who can responsibly use this tool to those that cannot is dangerously low.
I'd argue that the only acceptable case for these is in a doubly nested for loop that cannot be extrapolated into its own function (thus using return instead of goto) where you're testing for a specific condition. This is a relatively rare construct in modern code.
That said, in two years, all the AS freelancers out there will be gutting and fixing spaghetti goto statements for great profit. :)
Posted by: Cory Petosky at December 2, 2009 1:44 PM
Why?
Seems goto-ey.
Posted by: Vadim P. at December 2, 2009 3:50 PM
I wish you did not write this post. I am scared somebody can use it and I'd not like to be the one who has to fix that code :)
Posted by: Cesare at December 4, 2009 1:09 PM
Pointless.
Posted by: Stat at December 4, 2009 10:51 PM
Cory Petosky:
One good case would be for doing iterative implementation of recursive algotithms.
(ex: iterative sorting)
So instead of pusing and poping scope, you use labels.
But then again I have no idea of the performances in flash!
Posted by: jpauclair at December 7, 2009 4:23 AM
Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).