User Guide

General coding conventions 79
To strict data type 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 params_lv:LoadVars = new LoadVars();
Strict data typing provides you with code completion, and ensures that the value of params_lv
contains a LoadVars object. It also ensures that the LoadVars object is not 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 parameters within an Object or array. For more information on strict typing
variables, see “Strict data typing” on page 41.
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.
Adding suffixes to your variable names serves the following important functions:
It provides valuable code completion, which helps speed up the coding process.
It makes your code readable, so you can immediately identify a variables data type.
For example, the following code creates an array that contains the names of each font installed on
the client computer. This code demonstrates the use of code suffixes (
_array) and strict typing
(
:Array). Suffixes and strict typing let you use code completion, but strict typing ensures that a
variable maintains its data type. Suffixes improve the readability of your ActionScript; for
example, it is easier to understand what
font_array means, but font might not be as obvious in
the code. In the following example, if the code snippet tries to assign a string or numeric value to
font_array, an error is generated:
var font_array:Array = TextField.getFontList();
When you write instructional code, use suffixes to improve the codes readability for students.
Suffixes help users learn ActionScript because they clarify each variables data type, which helps
explain the codes structure.
You can also generate code completion by using a specific comment technique. If you want to add
code completion for an object within your ActionScript, add the following comment to your
code:
// Object siteParams_obj;
Whenever you enter siteParams_obj. (with the dot [.]) into the Actions panel, the code
completion menu for your Object appears. Using comments to provide code completion is the
most obscure method; use it only as a last resort for code completion, such as when you need to
create backward-compatible files. The best and recommended methods for code completion and
readability are strict typing and suffixes, respectively.
To use code completion with components, you must import the component’s class, or provide the
full path to the ActionScript class name. Format your ActionScript in one of the following ways:
You can specify its fully qualified class name, as the following example shows:
var myScrollPane:mx.containers.ScrollPane; //code completion works
You can also use the import statement to reference the class, as the following example shows:
import mx.containers.ScrollPane;
var myScrollPane:ScrollPane; //code completion works