User Guide

ActionScript coding standards 91
The shortened if statement syntax is known as the conditional operator (?:). It lets you convert
simple
if-else statements into a single line of code. In this case, the shortened syntax reduces
readability, and so it is not preferable. Do not use this syntax for complex code, because it is more
difficult to spot errors. For more information on using conditional operators, see “Writing
conditional statements” on page 90 and “Writing compound statements” on page 91.
When you write complex conditions, it is good form to use parentheses [()] to group conditions.
If you do not use parentheses, you (or others working with your ActionScript) might run into
operator precedence errors.
For example, the following code does not use parentheses around the condition:
if (fruit == apple && veggie == leek) {}
The following code uses good form by adding parentheses around conditions:
if ((fruit == apple) && (veggie == leek)) {}
If you prefer using conditional operators, place the leading condition (before the question mark
[
?]) inside parentheses. This helps improve the readability of your ActionScript. The following
code is an example of ActionScript with improved readability:
(y >= 5) ? y : -y;
Writing compound statements
Compound statements contain a list of statements within braces ({}). The statements within
these braces are indented from the compound statement, as the following ActionScript shows:
if (a == b) {
//this code is indented
trace("a == b");
}
In this example, the opening brace is placed at the end of the compound statement. The closing
brace begins a line, and aligns with the beginning of the compound statement.
Place braces around each statement when it is part of a control structure (
if-else or for), even if
it contains only a single statement. This good practice helps you avoid errors in your ActionScript
when you forget to add braces to your code. The following example shows code that is written
using poor form:
if (numUsers == 0)
trace("no users found.");
Although this code validates, it is considered poor form because it lacks braces around the
statements. In this case, if you add another statement after the
trace statement, it is executed
regardless of whether the
numUsers variable equals 0, which can lead to unexpected results. For
this reason, add braces so the code looks like the following example:
if (numUsers == 0) {
trace("no users found");
}