User Guide
ActionScript coding standards 89
}
}
Delete variables or make variables null when you no longer need them. Setting variables to null
can still enhance performance. This process is commonly called garbage collection. Deleting
variables helps optimize memory use during runtime, because unneeded assets are removed from
the SWF file. It is better to delete variables than to set them to
null. For more information on
performance, see “Performance and Flash Player” on page 114.
For information on naming variables, see “Variable names” on page 70. For more information on
deleting objects, see
delete in Flash ActionScript Language Reference.
Writing syntax and statements
There are several ways to format or write a piece of ActionScript. Differences can exist in the way
you form the syntax, such as the way you form it across multiple lines in the Actions panel (for
example, where you put brackets (
{}) or parentheses [()]).
There are several general guidelines for writing ActionScript syntax. Place one statement per line
to increase the readability of your ActionScript. The following example shows correct and
incorrect statement usage:
theNum++; //correct
theOtherNum++; //correct
aNum++; anOtherNum++; //incorrect
There are several ways you can assign variables. Do not embed assignments, which is sometimes
used to improve performance in a SWF file at runtime. Despite the performance boost, your code
is much harder to read and debug. Consider the following ActionScript example:
var myNum = (a = b + c) + d;
If you assign variables as separate statements, it improves readability, as the following example
shows:
var a = b + c;
var myNum = a + d;
The following sections describe the preferred ways to format your syntax and statements in
ActionScript:
• “Writing conditional statements” on page 90
• “Writing compound statements” on page 91
• “Using the for statement” on page 92
• “Using while and do-while statements” on page 92
• “Using return statements” on page 93
• “Writing switch statements” on page 93
• “Using try-catch and try-catch-finally statements” on page 93
• “Using listener syntax” on page 94