Pushing the boundaries

…couldn’t be any simpler!

ActionScript code tricks

Posted by vijayram on January 24, 2007

Ok! This post will be ever growing as I come across tricks used by the geek community to write code. I was always baffled by code which didn’t stick to the book. Hoping this post will clear out those myths that if you use this structure whilst coding, you are “god” of the geek land….maybe you are!!!..whatever.

Compound Assignment Operators:
Compound assignment operators provide two benefits. Firstly, they produce more compact code.Saves the trouble of over exercising those fingers. Secondly, the variable being operated upon, or operand, only needs to be evaluated once in the compiled application. This can actually make the code more efficient when executed.In contrast, the statement using the simple operators makes a copy of the variable A, performs the operation on the copy, and then assigns the resulting value back to A, temporarily using extra memory.

Lets take the case of arithmetic operators (+ – * / %).
var += expression; // var = var + expression
var -= expression; // var = var – expression
var *= expression; // var = var * expression
var /= expression; // var = var / expression
var %= expression; // var = var % expression

This statement adds 1 to value :
value ++;
This statement subtracts 1 from value :
value –;

You can use the increment and decrement operators (– and ++) either before or after the variable or property they operate upon. If used before the operand, they are called prefix operators. If used after the operand, they are called postfix operators.
var value :Number = 5;
trace(value ++); // 5
trace(value ); // 6

var value :Number = 5;
trace(++value ); // 6
trace(value ); // 6

Advertisements

One Response to “ActionScript code tricks”

  1. Prakaz said

    hi vijay, this is some good basic programming stuff. Use of these expressions always makes the code very readable and compact.

    This reminded me of a post in MXNA which had a problem like this:

    i=1;
    val=++i+i++;

    val=?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: