768 Best Practices and Coding Conventions for ActionScript 2.0
You can write a conditional statement that returns a Boolean value in two ways. The second
example is preferable:
if (cartArr.length>0) {
return true;
} else {
return false;
}
Compare this example with the previous one:
// better
return (cartArr.length > 0);
The second snippet is shorter and has fewer expressions to evaluate. It’s easier to read and to
understand.
The following example checks if the variable
y is greater than zero (0), and returns the result
of
x/y or a value of zero (0).
return ((y > 0) ? x/y : 0);
The following example shows another way to write this code. This example is preferable:
if (y>0) {
return x/y;
} else {
return 0;
}
The shortened if statement syntax from the first example 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.
If you must use conditional operators, place the leading condition (before the question mark
[
?]) inside parentheses to improve the readability of your code. You can see an example of this
in the previous code snippet.
Writing compound statements
Compound statements contain a list of statements within braces ({}). The statements within
these braces are indented from the compound statement. The following ActionScript code
shows an example of this:
if (a == b) {
// This code is indented.
trace("a == b");
}