| |
|
|
|
|
|
|
|
Naming conventions in actionscript (part 1)
It is never a bad idea to go back to the basics, even the most advanced developer might have a little bad coding habbit that could be improved. It is very important to build your programming knowledge on a solid ground not only because it will improve your coding capabilities and efficiency but because your code might be read by somebody else.
General naming guidelines
Naming conventions are important for writing logical code. The primary purpose is to improve the readability of your ActionScript code. Remember that all variables must have unique names. Names are case-sensitive in Flash Player 7 and later. Do not use the same name with a different case, because this can be confusing to programmers reading your code and can cause problems in earlier versions of Flash that do not force case sensitivity. Keep the following guidelines in mind when you name items such as variables, files, and classes in Flash:
- Limit your use of abbreviations. Use abbreviations consistently. An abbreviation must clearly stand for only one thing. For example, the abbreviation "sec" might represent "section" and "second."
- Concatenate words to create names.
Use mixed-cases (upper and lower case) when you concatenate words to distinguish between each word for readability. For example, select myPelican rather than mypelican.
- Name a file by describing the process or item, such as addUser.
- Don't use nondescriptive names for methods or variables. For example, if you retrieve a piece of data that is the visitor's user name, you might use the getUserName() method instead of the less descriptive getData() method. This example expresses what is happening rather than how you accomplish it.
- Keep all names as short as possible.
Remember to keep names descriptive.
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/littleVariable = true;
my littleVariable = true;
my.littleVariable = true;
5mylittleVariable = 55; |
Use strict data typing with your variables whenever possible because it helps you in the following ways:
- Adds code completion functionality, which speeds up coding.
Generates errors in the Output panel so you don't have a silent failure when you compile your SWF file. These errors help you find and fix problems in your applications.
- 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. 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:
- All variables must have unique names.
- Don't use the same variable name with different cases.
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.
- Don't use words that are part of the ActionScript 1.0 or 2.0 language as variable names.
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 variables that are parts of common programming constructs.
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"; |
- Always add data type annotations to your code. 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:
- Generate errors at compile time so your application doesn't silently fail.
- Trigger code completion.
- Helps users understand your code.
- Don't overuse the Object type.
Data type annotations should be precise to improve performance. Use an Object type only when there is no reasonable alternative.
- Keep variables as short as possible while retaining clarity. Make sure your variable names are descriptive, but don't go overboard and use overly complex and long names.
- Only use single-character variable names for optimization in loops. 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]);
} |
- Start variables with a lowercase letter. Names with capital first letters are reserved for classes, interfaces, and so on.
- Use mixed case for concatenated words. For example, use myFont instead of myfont.
- Don't use acronyms and abbreviations. 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.
- Use complementary pairs when you create a related set of variable names. For example, you might use complementary pairs to indicate a minimum and maximum game score, as follows:
| var minScoreNum:Number = 10; var maxScoreNum:Number = 500; |
|
|
|
If you think this page is providing useful information, don't hesitate to leave a comment.
|
|
|
|
|
|
|
|
|
Copyright ©2006-2008 flashvalley.com - All rights reserved |
|