PART 2 - Variable Naming Rules and Conventions - Introduction to Flash AS3 Variables
Tutorials

PART 2 - Variable Naming Rules and Conventions - Introduction to Flash AS3 Variables



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.

Examples:
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:
var myV@riable:String;

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:
var 1_variable:Number;

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:
var variable_1:Number;

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:
var myvariable:Number;

Consider using:
var myVariable:Number;

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.

Examples:
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

Fun with Variables: Complete the Story - Introduction to Flash AS3 Variables - PART 3: NEXT>>




- Distance Learning - Cs179.11 A - Sem 01 Sy 2012-13
July 24, 2012 Hi, everyone. So for this session, we will be starting with ActionScript. The learning resources below will teach you what ActionScript is, and how to add some ActionScript code to a Flash project - that's going to be what's covered...

- Local And Global Variables In As3
In the example below, you will see two variable declarations and one function: var outsideVar:String = "outside of the function"; function myFunction():void { var insideVar:String = "inside of the function"; } myFunction(); In the example above, we...

- Part 3 - Fun With Actionscript 3 Variables: Complete The Story - Introduction To Flash As3 Variables
PART 1: Creating ActionScript 3 Variables and Assigning Values to themPART 2: Variable Naming Rules and ConventionsPART 3: Fun with Variables - Complete the Story Create a new Flash ActionScript 3.0 document. Then copy the code below and paste it in the...

- Actionscript 3: Convert Strings To Numbers
In this lesson, we are going to learn how to convert strings to numbers in AS3. You can convert strings that are made up of numerical characters into actual Number data using the Number() constructor. The way it works is that you pass the String value...

- Actionscript 3 Number To String Conversion
In this lesson, we're going to learn how to convert numbers to strings in AS3. If you you'd like to do ActionScript 3 Number to String conversions, then you can use the toString() method of the Number class. Here is an example: var myNumber:Number...



Tutorials








.