752 Best Practices and Coding Conventions for ActionScript 2.0
Guidelines for creating a class
Remember the following guidelines when you create a class file:
■ Place only one declaration per line.
■ Don’t place multiple declarations on a single line.
For example, format your declarations as shown in the following example:
var prodSkuNum:Number; // Product SKU (identifying) number
var prodQuantityNum:Number; // Quantity of product
This example shows better form than putting both declarations on a single line. Place
these declarations at the beginning of a block of code.
■ Initialize local variables when they are declared.
A class’s properties should only be initialized in the declaration if the initializer is a
compile-time constant.
■ Declare variables before you first use them.
This includes loops.
■ Avoid using local declarations that hide higher-level declarations.
For example, don’t declare a variable twice, as the following example shows:
var counterNum:Number = 0;
function myMethod() {
for (var counterNum:Number = 0; counterNum<=4; counterNum++) {
// statements;
}
}
This code declares the same variable inside an inner block, which is a practice to avoid.
■ Don’t assign many variables to a single value in a statement.
Follow this convention because otherwise your code is difficult to read, as the following
ActionScript code shows:
playBtn.onRelease = playBtn.onRollOut = playsound;
or
class User {
private var m_username:String, m_password:String;
}
■ Make a method or property public only if it needs to be public for a reason. Otherwise,
make your methods and properties private.