- PART 1: Creating ActionScript 3 Variables and Assigning Values to them
- PART 2: Variable Naming Rules and Conventions
- PART 3: Fun with Variables - Complete the Story
Variable names are author-defined. This means that you, as the author of the Flash ActionScript 3.0 project, will decide which names to give your variables. But there are still some rules that you have to follow. In this part of the Introduction to Flash AS3 Variables tutorial series, we'll take a look at what those rules are. We'll also take a look at some commonly used naming conventions when it comes to working with variables.
Variable Naming Rules
Rule #1: Do not use any ActionScript 3.0 reserved words (also referred to as keywords) to name a variable.
var var:Number; var this:String; var new:Boolean;
The examples above are unacceptable because var, this, and new are all ActionScript 3.0 reserved words.
Rule #2: Variable names must be unique.
The example below will generate an error message:
var myVariable:Number; var myVariable:String;
Even if each variable is assigned a different data type, the names are the same. This will create a conflict between the two variables.
Rule #3: Variable names are case-sensitive
The following variables are considered two different variables:
var myVariable:Number; var myvariable:Number;
In the examples above, one variable name has an uppercase V, while the other one has a lowercase v. Because of this, these two variables are considered entirely different from each other. They will not create a conflict. And it does not matter that they both have the same data type; they are still two separate variables.
Rule #4: Variable names can only use letters, numerical characters, the underscore, and the dollar sign.
The following example is unacceptable because it uses the @ sign in the variable name:
The underscore and the dollar sign are the only special characters that can be used when naming variables. No other special characters can be used.
Rule #5: A Variable name can only start with either a letter, an underscore or the dollar sign.
The following example is unacceptable:
Although this variable name does not use any unacceptable characters, it does begin with a numerical character. Variable names can only begin with either a letter, an underscore or the dollar sign.
An acceptable version of the example above would be:
Here, since it does not use any unacceptable characters and it DOES NOT START with a numerical character, then the variable name is acceptable.
Rule #6: Variable names cannot contain spaces.
The following example is unacceptable because the variable name contains a space:
var my variable:Boolean;
Some Variable Naming Conventions
The following items are not rules used in naming variables, but are more of commonly-used practices and styles. You do not have to follow these suggestions, but they might help make your code more organized and easier to understand.
#1 Use variable names that are descriptive of the type of data they will hold or the purpose they will serve.
Instead of using generic variable names such as:
var string1:String = "John"; var string2:String = "Smith"; var number1:Number = 2012;
Consider using more descriptive alternatives:
var firstName:String = "John"; var lastName:String = "Smith"; var year:Number = 2012;
#2 If you want to combine different words in one variable name, you can differentiate these words by using uppercase and lowercase letters.
Instead of using:
This is a practice referred to as camel casing, because the visual bumps created by the uppercase letters are similar to the humps on a camel's back.
#3 You can prefix your variable names with a character that is descriptive of its data type, such as s for a String variable or n for a Number variable.
var sTitle:String = "Learning ActionScript 3.0"; var nScore:Number = 99; var bActive:Boolean = false;
This is a convention know as the Hungarian notation.
And that concludes this tutorial series on an Introduction to Flash ActionScript 3.0 Variables.
<<PREV: Creating ActionScript 3 Variables and Assigning Values to them - Introduction to Flash AS3 Variables - PART 1