5 shorthand Actionscript 3.0 snippets

Published on Sat 04 Sep 2010 by Will


Writing Actionscript 3.0 can be a little long winded at times. Here are 5 shorthand examples of ways to write AS3 using less code.

Ternary operator

Use the ternary operator to write a short if/else statement. A ternary is used when assigning a value to a variable in a way that requires some conditional logic. Take the following if statement for example:

if (y) {
   x = 100;
} else {
   x = 0;
}

Written as a ternary it would be:

x = y ? 100 : 0;

In english the above can be thought of as “if y is true then x equals 100, otherwise x equals 0”. You can also write expressions within the ternary like:

x = z > 500 ? 100 : 0;

Boolean quick toggle

Changing the value of a boolean from true to false or vice verca is a common task in any programming language. You can write an if statement to find the current value of your boolean and then set it to the opposite:

if (myBoolean) {
   myBoolean = false;
} else {
   myBoolean = true;
}

Or even better, you can simply write:

myBoolean = !myBoolean

Multiple property assignment

Assign a value to multiple properties at once in a single line by stringing together your property assignments like so:

myDisplayObject.x = myDisplayObject.y = 0;

Single line if statement

Did you know you don’t necessarily need curly braces { } when writing an if statement? If you require a single operation consider putting it on a single line:

if (bool) doSomething();

Alternatively, you can put your operation on the following line, just make sure to indent for better readability:

if (bool)
    doSomething();

Array creation

Not necessarily a life changing tip, but I’m often surprised to see developers creating arrays using the new operator when there is a shorter way. This also goes for assigning content to the array at the time of creation. Take the following:

var myArray:Array = new Array();
myArray.push(“cat”);
myArray.push(“dog”);
myArray.push(“fish”);

This could be written much shorter as:

var myArray:Array = [“cat”, “dog”, “fish”];

Got a shorthand snippet?

Do you have any shorthand tips for writing Actionscript 3.0? Let us know by leaving a comment below!

Did you enjoy this post?

Why not subscribe via RSS or follow Ahrooga on Twitter for alerts on new posts :)

You may also like:

Comments

erik - 05 September, 2010

While all your tips are neat and compact, most of them are bad practices. The boolean toggle and array creation are like they should be, but I'd advise as coders to chose verbosity and clarity over compactness.

Will Dady - 05 September, 2010

Thanks for the comment Erik. I take issue with saying most of them are bad practices. The only one I would suggest people use sparingly is multiple property assignment due to pointing variables to the same place in memory.

MicroAngelo - 05 September, 2010

I'd agree with Erik: the ternary operator is so much less clear that it can't be recommended.

I think you can get away with bracketless ifs though.

My snippet is using switch (true) and then case: x == y, case:x == z etc. as an alternative to large/complex if-elses.

Andreas Renberg - 05 September, 2010

I enjoy using the logic operators (|| and &&) to on a single line set default values if none are set.

For example the following code:
if (align) //Check if the align string is set
{
return align;
}
else
{
return "left";
}

It can be summed up into with the ternary operator, or it can be written like this:
return align || "left";

A similar method can be used with the && operator, and you can also use the assignment operators together with them (not sure of the name though...)
align |= "left";
align &= lowercase(align);

squeedee - 05 September, 2010

|| and ||= are great for initialising variables:

a = b || c;

Where a = b unless b evaluates to false then a = c;

a ||= c;

Where a = c if a evaluates to false.

I'm careful to mention 'evaluates to false' - null evaluates to false, which is where this is useful. Quite a few non-null scalars also evaluate to false, so be careful when using this. My rule of thumb is to only use it with non-intrinsic typed 'evaluatees'.

Burton - 09 December, 2010

My understanding has been the fastest way to instantiate an Array object is:
var myArray:Array = [];

@erik
I completely agree with you. If someone cannot come behind and read your code, then you should tread cautiously.

I usually try to balance speed with readability. I grab code from all sorts of old projects. Having to rewrite it because I did not think things through carefully will eventually ingrain a smart, forward-thinking approach into your brain.

Paul - 31 December, 2010

Personally I prefer the approach of performance rules, for example the var myArray:Array = [] syntax is quicker in benchmark tests than the new Array() method and as such I would opt for that. I believe readability of code is important, but we are programming to achieve a goal and that goal is paramount.

Free - 12 February, 2011

I like to put my entire program on one line then it runs faster!

viaria - 12 April, 2011

i think in development part we should write long version which is readable, after that for performence improvement we should write sorthands. i believe shorthands runs faster.