Accessibility

TechNote (Archived)

Naming strategies for Flash

Introduction
Although Flash 5 offers flexibility in naming objects and freedom in coding practices, following some basic naming conventions will help ensure proper playback. The use of naming conventions when coding also helps make scripts easier to understand, troubleshoot, and share with others. This document lists some common naming conventions when programming in ActionScript and other languages.

Naming strategies:

Avoid spaces or special characters. Spaces, periods, or special characters often have special meaning to the Flash Player, signifying the end of a statement or part of an expression, for example. Names can include underscores, letters, and numbers. Punctuation or other special characters should be avoided.

Note:For an example list of special characters, refer to the TechNote URL Encoding: Reading special characters from a text file (TechNote 14143).

Start with a letter. Using a variable or object name that starts with a number can cause confusion. For example, instead of using the expression '3frame' to identify a frame label, use'frame3'. Variable, function, and object names can contain numerals, however the first character should be a letter.
Use unique names. Avoid giving two different objects the same variable name. For example, if a movie clip instance and a text field both have the same name, problems may arise when changing properties of one or the other. Giving each object a unique name can help avoid this issue.

Use a system for identifying type and scope of variables. For example, use 'mC' as a prefix for movie clip instance name, or 'txt' as a prefix to a variable name given to dynamic text field. In a movie with both local and global variables you might use the word "global" or the letter "g" to prefix global variables that you intend to use throughout the movie, and the word "local" or the letter "l" for local variables that cannot be accessed later in the movie. What is referred to as Hungarian notation is widely used across scripting languages.

Here are some more examples. The left column contains the data or symbol type and the right column contains an example of a variable or instance name for that type:

 Number: nMyNumber Movie Clip: mcMyMovie Button: btnMyButton Graphic: grMyGraphic Date: dtMyDate Sound: sndMySound 

Use multi-word descriptive names. For example:firstNumber, myDate, timeCounter,lastName. This naming practice is useful when troubleshooting or sharing scripts with others. Generically named variables such as x, i or j don't give much information as to their use and can be difficult to keep track of when troubleshooting. The variable lastName, however, instantly gives clues as to what the variable is for.

Capitalize the first letter of subsequent words. When using multi-word names for variables, capitalize the first letter of all words but the first. Some examples of this are: lastAddress,currentAddress, and lastFrameVisited. This practice can make code easier to read. If you share code or collaborate on projects, others will benefit from the readability..

Match cases. Be sure to be consistent with capital and lower case letters. If an object is named myTextFader, be sure to refer to it throughout the movie as myTextFader, and not 'mytextfader' or 'MYTEXTFADER'.

Avoid reserved words. Words like 'date', 'this', and 'goto' have special meaning for the Flash Player. Using these words as variables may cause problems when the script is executed. One way to avoid this is by using multi-word descriptive names for variables, as suggested above. For example, use birthDate as the name for a variable holding a date. In addition to avoiding conflicts with the reserved word 'date', the result is a more descriptive variable name.

Note:This issue is more thoroughly explained in the TechNote Variable names conflicting with object names (TechNote 14677).

Declare variables with var or set variable. Although Flash does not require using var or set variable to assign a value to a variable, it's a good practice. For example, it's perfectly acceptable to declare a variable and assign it a value in one step, as in thefollowing:

 userName ="Mary"; 

However, the code is more readable if the variable is declared before being assigned a value:

 var userName ="Mary"; This clearly identifies the statement as a variable assignment. 

Include a legend. This is especially helpful in complex projects.
For example, if you use certain prefixes to identify different types of variables and objects, it is helpful to define your naming system. Use the comment statement in ActionScript, or create a "ReadMe" or "legend" symbol in the library with text that explains what each prefix means.

Additional information
For another perspective, similar strategies are outlined in the TechNote Naming strategies for Dreamweaver, UltraDev, and various interpreters (TechNote 14610).

AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Creative Commons License

Search Support


Document Details

ID:tn_14727

Products Affected:

flash