Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Best Practices and Coding Conventions for ActionScript 2.0 > Formatting ActionScript syntax > General formatting guidelines | |||
When you use spaces, line breaks, and tab indents to add white space to your code, you increase your code's readability. White space enhances readability because it helps show the code hierarchy. Making your ActionScript 2.0 easier to understand by making it more readable is important for students as well as for experienced users working on complex projects. Legibility is also important when you are debugging ActionScript code, because it is much easier to spot errors when code is formatted correctly and is properly spaced.
You can format or write a piece of ActionScript 2.0 code several ways. You'll find differences in the way developers choose to format the syntax across multiple lines in the ActionScript editor (the Actions panel or Script window), such as where you put brackets ({}) or parentheses [()]).
recommends the following formatting points to help promote readability in your ActionScript code.
Paragraphs of ActionScript code are groups of logically related code. Adding a blank line between them helps users read the ActionScript code and understand its logic.
Use the same indentation style throughout your ActionScript code, and make sure that you align the braces ({}) properly. Aligned braces improve the readability of your code. If your ActionScript syntax is correct, Flash automatically indents the code correctly when you press Enter (Windows) or Return (Macintosh). You can also click the Auto Format button in the ActionScript editor (the Actions panel or Script window) to indent your ActionScript code if the syntax is correct.
You can format some statements, such as conditional statements, in several ways. Sometimes formatting statements across several lines rather than across a single line makes your code easier to read.
()].
The following ActionScript code shows an example of this:
do {
// something
} while (condition);
The following ActionScript code shows an example of this:
function checkLogin():Boolean {
// statements;
}
checkLogin();
or
printSize("size is " + foo + "\n");
Using spaces after commas makes it easier to distinguish between method calls and keywords, as the following example shows:
function addItems(item1:Number, item2:Number):Number {
return (item1 + item2);
}
var sum:Number = addItems(1, 3);
Using spaces makes it is easier to distinguish between method calls and keywords, as the following example shows:
//good var sum:Number = 7 + 3; //bad var sum:Number=7+3;
An exception to this guideline is the dot (.) operator.
For example, increment (++) and decrement(--), as shown in the following example:
while (d++ = s++) -2, -1, 0
The following ActionScript code shows an example of this:
//bad
( "size is " + foo + "\n" );
//good
("size is " + foo + "\n");
The following ActionScript code shows an example of this:
theNum++; // Correct theOtherNum++; // Correct aNum++; anOtherNum++; // Incorrect
Embedded statements are sometimes used to improve performance in a SWF file at runtime, but the code is much harder to read and debug. The following ActionScript code shows an example of this (but remember to avoid single-character naming in the actual code):
var myNum:Number = (a = b + c) + d;
The following ActionScript code shows an example of this (but remember to avoid single-character naming in the actual code):
var a:Number = b + c; var myNum:Number = a + d;
|
NOTE |
You can control auto-indentation and indentation settings by selecting Edit > Preferences (Windows) or Flash > Preferences (Macintosh), and then selecting the ActionScript tab. |
|
|
|
|