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");
}
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...