Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Best Practices and Coding Conventions for ActionScript 2.0 > Naming conventions > Naming variables | |||
Variable names can only contain letters, numbers, and dollar signs ($). Do not begin variable names with numbers. Variables must be unique and they are case-sensitive in Flash Player 7 and later. For example, avoid the following variable names:
my/warthog = true; // includes a slash my warthogs = true; // includes a space my.warthogs = true; // includes a dot 5warthogs = 55; // begins with a number
Use strict data typing with your variables whenever possible because it helps you in the following ways:
To add a data type to your variables, you must define the variable using the var keyword. In the following example, when creating a LoadVars object, you would use strict data typing:
var paramsLv:LoadVars = new LoadVars();
Strict data typing provides you with code completion, and ensures that the value of paramsLv contains a LoadVars object. It also ensures that the LoadVars object will not be used to store numeric or string data. Because strict typing relies on the var keyword, you cannot add strict data typing to global variables or properties within an object or array. For more information on strict typing variables, see About assigning data types and strict data typing.
|
NOTE |
|
Strict data typing does not slow down a SWF file. Type checking occurs at compile time (when the SWF file is created), not at runtime. |
Use the following guidelines when you name variables in your code:
Don't use, for example, firstname and firstName as different variables in your application. Although names are case-sensitive in Flash Player 7 and later, using the same variable name with a different case can be confusing to programmers reading your code and can cause problems in earlier versions of Flash that do not force case sensitivity.
In particular, never use keywords as instance names, because they cause errors in your code. Don't rely on case sensitivity to avoid conflicts and get your code to work.
Don't use language constructs if you are aware of them in other programming languages, even if Flash does not include or support these language constructs. For example, do not use the following keywords as variables:
textfield = "myTextField"; switch = true; new = "funk";
Also referred to as "using strict data types with your variables," or "strong typing your variables," adding type annotations to your variables is important in order to:
For information on adding type annotations, see About assigning data types and strict data typing.
Data type annotations should be precise to improve performance. Use an Object type only when there is no reasonable alternative.
Make sure your variable names are descriptive, but don't go overboard and use overly complex and long names.
Optionally, you can use single-character variables for temporary variables in loops (such as i, j, k, m, and n). Use these single-character variable names only for short loop indexes, or when performance optimization and speed are critical. The following example shows this usage:
var fontArr:Array = TextField.getFontList();
fontArr.sort();
var i:Number;
for (i = 0; i<fontArr.length; i++) {
trace(fontArr[i]);
}
Names with capital first letters are reserved for classes, interfaces, and so on.
For example, use myFont instead of myfont.
The exception to this rule is if acronyms or abbreviations represent the standard way to use a term (such as HTML or CFM). For commonly used acronyms, use mixed cases for improved readability, such as newHtmlParser instead of newHTMLParser.
For example, you might use complementary pairs to indicate a minimum and maximum game score, as follows:
var minScoreNum:Number = 10; // minimum score var maxScoreNum:Number = 500; // maximum score
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00001094.html