Typically, you spend 80% of your development time debugging, troubleshooting, and practicing general maintenance, especially on larger projects. Even when you work on small projects, you'll spend a significant amount of time analyzing and fixing code. The readability of your code is important for your benefit and the benefit of your team members. When you follow naming conventions, you increase readability, which increases workflow and enables you to find and fix any errors in your code. All programmers follow a standardized way of writing code; this improves the project in many ways.
Using naming conventions for your variable names can serve the following important functions:
The following sections contain naming guidelines for writing ActionScript code, such as naming files, variables, constants, components, and so on. The section "Formatting ActionScript Syntax" discusses formatting conventions that are specific to ActionScript, and common in other programming languages. The section "ActionScript Coding Conventions" discusses coding conventions that are specific to writing ActionScript and developing with Flash 8.
Note: Flash Player 7 and 8 loosely follow the ECMAScript (ECMA-262) edition 3 language specification. It is useful to see this specification for information on how the language works.
This section reviews naming guidelines for writing ActionScript code. Naming conventions are important for writing logical code. The primary purpose is to improve the readability of your ActionScript 2.0 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:
myPelican rather than mypelican.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.The following sections offer more detail on naming items such as variables, classes, packages, and constants in your code.
When naming instances and variables, avoid using reserved words, which can cause errors in your code. Reserved words include keywords in the ActionScript language.
Also, do not use any word in the ActionScript 2.0 languages (called a language construct) as an instance or variable name. ActionScript constructs include class names, component class names, method and property names, and interface names.
Warning: Never use different cases to avoid conflicting with reserved words. For example, naming an instance of the textfield TextField class (which doesn't conflict with TextField because Flash is case-sensitive) is a poor coding practice.
Table 1 lists reserved keywords in ActionScript 2.0 that cause errors in your scripts when used as variable names.
add |
and |
Break |
case |
catch |
class |
continue |
default |
delete |
do |
dynamic |
else |
eq |
extends |
false |
finally |
for |
function |
ge |
get |
gt |
if |
ifFrameLoaded |
implements |
import |
in |
instanceof |
interface |
intrinsic |
le |
it |
ne |
new |
not |
null |
on |
onClipEvent |
or |
private |
public |
return |
set |
static |
super |
switch |
tellTarget |
this |
throw |
try |
typeof |
var |
void |
while |
with |
|
|
Table 2 lists words that are reserved for future use in Flash, from the ECMAScript (ECMA-262) edition 4 draft language specification. Avoid using these words because they might be used in future releases of Flash.
as |
abstract |
Boolean |
bytes |
char |
const |
debugger |
double |
enum |
export |
final |
float |
goto |
is |
long |
namespace |
native |
package |
protected |
short |
synchronized |
throws |
transient |
use |
volatile |
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 in Flash Help or the Flash 8 LiveDocs (Learning ActionScript 2.0 in Flash > Data and Data Types > About Data Types > 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:
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 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:
For information on adding type annotations, see About Assigning Data Types and Strict Data Typing in Flash Help or the Flash 8 LiveDocs (Learning ActionScript 2.0 in Flash > Data and Data Types > About Data Types > About Assigning Data Types and Strict Data Typing).
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]);
}
myFont instead of myfont.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; // minimum score var maxScoreNum:Number = 500; // maximum score
You can use constants for situations in which you need to refer to a property whose value never changes. This helps you find typographical mistakes in your code that you might not find if you used literals. It also lets you change the value in a single place.
Variables should be lowercase or mixed-case letters; however, use the following guidelines for naming static constants (variables that do not change):
You can see these guidelines at work in the following ActionScript code snippet:
var BASE_URL:String = "http://www.adobe.com"; // constant var MAX_WIDTH:Number = 10; // constant
Do not directly code numerical constants unless the constant is 1, 0, or –1, which you might use in a for loop as a counter value.
Start Boolean variables with the word "is" (because a Boolean value either "is" or "is not" because of its nature). Therefore, you might use the following for whether a baby is a girl or not (which is a Boolean value):
isGirl
Or for a variable indicating whether a user is logged in (or not), you might use the following:
isLoggedIn
Use the following guidelines when you name functions and methods in your code. For information on writing functions and methods, see Functions and Methods in Flash Help or the Flash 8 LiveDocs (Learning ActionScript 2.0 in Flash > Functions and Methods).
singLoud().getCurrentSong().Name methods as verbs. You might concatenate the name, but it should contain a verb. You use verbs for most methods because they perform an operation on an object. Examples of method names include the following:
sing(); boogie(); singLoud(); danceFast();
When you create a new class file, use the following guidelines when you name the class and ActionScript file. For proper formatting, see the following examples of class names:
class Widget; class PlasticWidget; class StreamingVideo;
You might have public and private member variables in a class. The class can contain variables that you do not want users to set or access directly. Make these variables private and allow users to access the values only by using getter/setter methods.
The following guidelines apply to naming classes:
NewMember.NewMember or OldMember.NewHtmlParser instead of NewHTMLParser for improved readability.NewMember or OldMember.Witches or BaldPirates). In most cases, it is better to leave the words as qualified nouns instead. A qualifier describes the noun or phrase. For example, instead of "cat" or "buckaneer," you might qualify the noun by using BlackCat or OldBuckaneer.Cat.catWhiskers. Instead, Cat.whiskers is much better.Running, or Gardening. Using these nouns might lead to confusion with methods, states, or other application activities.For more information on interfaces, see Interfaces in Flash Help or the Flash 8 LiveDocs (Learning ActionScript 2.0 in Flash > Interfaces).
It's common for package names to use "reverse domain" naming convention. Examples of reverse domain names include com.adobe for adobe.com, and org.yourdomain for yourdomain.org.
com, mx, or org.com.adobe.projectName to maintain consistency. Another example would be com.adobe.docs.learnAS2.Users for the Learning ActionScript 2.0 book in Flash Help.Pentagons, which is responsible for using the Flash drawing API to draw various kinds of pentagons in documentation examples; its name would be com.adobe.docs.as2.PentagonspackageName is an example of a compound, concatenated package name. Remember to use all lowercase letters for the prefix (com, org, and so on).Starting interface names with an uppercase "I" helps you distinguish an interface from a class. The following interface name, IEmployeeRecords, uses an initial uppercase letter and concatenated words with mixed case, as follows:
interface IEmployeeRecords{}
The following conventions also apply:
Printable is a good example.For more information on interfaces, see Interfaces in Flash Help or the Flash 8 LiveDocs (Learning ActionScript 2.0 in Flash > Interfaces).
Component names have an uppercase first letter, and any concatenated words are written in mixed case. For example, the following default user-interface component set uses concatenated words and mixed case:
Components that do not use concatenated words begin with an uppercase letter. If you develop custom components, use a naming convention to prevent naming incompatibilities with Macromedia components. The names of your components must be different from those of the default set that is included with Flash. If you adopt your own consistent naming convention, it helps you prevent naming conflicts.
Remember that the naming conventions in this section are guidelines. It is most important to use a naming scheme that works well for you and to use it consistently.