Professional Documents
Culture Documents
This document is a description of development guidelines that are required for all code
production for the Pearson School Curriculum Technology Group. Actionscript 3 and Object
Oriented programming are the two major design standards which all development must follow.
Filenames
All suffixes for ActionScript code is .as. Filenames should not contain spaces, punctuation, or any
other special characters. Files should be named according to their content type:
• Classes and Interfaces use UpperCamelCase; (example: CrosswordGame.as)
• Interfaces always start with an upper case I: IUpperCamelCase: (example IButton.as)
• Includes use lowerCamelCase; (example: someInclude.as)
File Organization
An ActionScript file must contain the following structure:
1. Initial Comment
2. Package definition
3. Import statements (alphabetical). Use fully qualified imports, i.e., without the asterisk, unless most of a
package is used. Otherwise, you get unnecessary file bloat.
Declarations
Only one declaration should be done per line.
Right:
var a:int = 10;
var b:int = 20;
var c:int = 30;
Wrong:
var a:int = 10; b:int = 20; c:int = 30;
Right:
public var isAdmin:Boolean = false;
Wrong:
public var isAdmin:Boolean;
//false is the default in AS3 for Boolean, not undefined!
Example:
public class Example extends MovieClip {
Statements
Simple Statements
Use one statement per line, even for simple statements and always end with a semicolon.
Right:
i++;
resetBalls();
Wrong:
i++; resetBalls();
Compound Statements
Compound statements (the ones that require { and }, like switch, if, while, etc.) must have the
code inside the statement must be indented by one level. Curly braces are used in all applicable
statements, even if it’s only a single line.
Return
Parentheses should only be used in returns if they make the code more readable.
Example:
return;
return getPreviewImage();
return (phase ? phase : initPhase);
if (condition) {
statements;
} else {
statements;
}
Switch
switch (condition) {
case ABC:
statements;
// continue, without break
case DEF:
statements;
break;
case JKL:
case XYZ:
statements;
return false;
default:
statements;
}
Loop for
for (initialization; condition; update) {
statements;
}
Loop for..in
for (var iterator:String in someObject) {
statements;
}
Loop do..while
do {
statements;
} while (condition);
With finally:
try {
statements;
} catch (e:Event) {
statements;
} finally {
statements;
}
With
with (this) {
alpha = 0.5;
}
Spaces
Wrapping lines
Line breaks make the code cleaner by creating logical groups. Use a line break in the following
situations:
• Between functions
• Between the method local variable and its first statement
• Before a block
• Before a single line comment or before a multi-line comment about a specific code
passage
• Between a code’s logical section to make it clearer
Ternary operators must be separated by blank spaces and broken in more than one line if they
are really long. Parentheses should be used to maintain readability.
Example:
Right:
a = (expression) ? expression : expression;
For expressions must be separated by blank spaces:
for (var i:Number; i < 5; i++) {
Requirements
All classes and functions should be commented. Variables should be commented, unless their
use is plainly obvious. Comments need to follow the ASDoc Commenting Guidelines. Comments
should not merely restate code into English or pseudo-code. Instead, they should describe the
functionality of the code and how it fits into a class, module, or project.
Right:
Wrong:
//sets column numbers variables to zero
colNum = 0;
Class:
/**
* First sentence of a multi-paragraph description of a
class.
* <p>Second paragraph.</p>
*
* @langversion ActionScript 3.0
* @playerversion Flash 9
*
* @author "Ray Koch"
*/
class SomeThing() {
Method:
/**
* Typical simple multiline documentation
* comment. This text describes the myFunction
* function.
*
* @langversion ActionScript 3.0
*
* @param a Describe param a here.
* @param b Describe param b here.
*
* @see someOtherFunction
*
* @example myFunction("daddy-o", 3234);
*/
public function myFunction(a:String, b:Number):void {}
General rules
• Acronyms: avoid acronyms unless the abbreviation form is more usual than its full form
(like URL, HTML, etc). Project names can be acronyms if this is the way it’s called;
• Only ASCII characters, no accents, spaces, punctuations or special characters;
• Don’t use the name of a native Flash Player component (flash package, like IOError,
Bitmap, etc);
• Never use Index to a component name since it conflicts with ASDoc tool generated docs.
Language
The code itself must be in English, except for verbs and nouns that are part of the business
domain (specific expertise area the software is meant to address, i.e., the real world part that is
relevant to the system).
Right:
DEFAULT_CATEGORY
addPlayer();
getProductsByCategory();
changeState();
Wrong:
quesoCombo();
mudarEstado();
UsuarioObjetoDeTransferencia
Packages
The package name must be written in lowerCamelCase, starting with small caps and
other initials in upper case. The first element in a package name is the first level domain (com,
org, mil, edu, net, gov). The next element is the company or client that owns the package
followed by as3, then project’s name and module:
com.pearson.as3.project.module
Examples:
com.pearson.as3.digitalPaths.math2011.uploadModule
com.apple.quicktime.v2
Classes
Classes names should prefer nouns, but can use adjectives as well. Always use
class LinearGradient
class DataTipRenderer
Interfaces
Interface names must follow the classes naming rules, with a starting uppercase “I”. Examples:
interface ICollectionView
interface IButt
Methods
Methods must start with a verb and are written in lowerCamelCase. If the method is
called on an event it should end with Handler: Examples:
makeRowsAndColumns()
getObjectsUnderPoint()
carClickHandler()
Variables
Variables must use camelCase and clearly describe what they are. No prefixes or suffixes.
ActionScript is strongly-typed, therefore an object’s name doesn’t need to and a clear and
concise name is more important than the object’s type.
Right:
private var isButtonEnabled:Boolean = true;
private var buttonLabels:Array = [”A”,”B”,”C”];
public var textOnScreenNode:XMLNode = new XMLNode();
Wrong:
private var data:Object = {}; //ambiguous
private var arr_data:Array = [”A”,”B”,”C”]; //goofy
prefix
public var ball_mc:MovieClip = new MovieClip();
//unnecessary suffix
public var _addBall = true; //unnecessary prefix
Temporary variables (like the ones used in loop statements) can be a single character.
The most commonly used are i, j, k, m, n, c, d.
catch (e:Error) {
hasFieldName = false;
...
}
Constants
Constants must be all uppercase, using underscores to separate words:
Right:
var maxPhase:Number = slowMethod();
for (var i:Number = 0; i < maxPhase; i++) {
statements;
}
Right:
var maleBabyNames:Array = ['Aaron', 'Ace', 'Alex'…];
var maleBabyNamesLength:Number = maleBabyNames.length;
for (var i:Number = 0; i < maleBabyNamesLength ; i++) {
trace(maleBabyNames[i]);
}
Right:
var daysOfWeek:Array = ['Mon', 'Tues', 'Wed', 'Thu',
'Fri'];
for (var i:Number = 0; i < daysOfWeek.length(); i++) {
trace(daysOfWeek[i]);
}
Wrong:
for (var i:Number = 0; i < slowMethod(); i++) {
statements;
}
• Create/use loosely coupled components. The less a component knows about another,
the greater the reuse possibilities.
• In Boolean evaluations, place the fastest ones before. If the fast one fails, the evaluation
is short-circuited and we don’t have to run the slow method.
Right:
if (isAdmin && slowMethod(item)) {
Wrong:
if (slowMethod(item) && isAdmin) {
Right:
myButton.addEventListener(MouseEvent.CLICK, myHandler);
Wrong:
myButton.addEventListener("click", myHandler);
Right:
private function mouseMoveHandler(event:MouseEvent):void
{
Wrong:
private function mouseMoveHandler(event:Event):void {
myButton.addEventListener(MouseEvent.CLICK,
function(event:MouseEvent):void {
simpleStatement;
}
);
dynamic is
Reserved Words each long
else namespace
Don’t name things with
enum native
them.
export native
extends new
abstract
false null
as
final override
boolean
finally package
break
float private
byte
for protected
case
function prototype
cast
get public
catch
goto return
char
if set
class
implements short
const
import static
continue
in super
debugger
include switch
default
instanceof synchronized
delete
interface this
do
internal throw
double
intrinsic throws
Overview
In order to reduce debugging time and deliver more stable software, all activities must be unit tested.
ASUnit is a robust and mature framework for testing Flash Actionscript projects. The extent of testing
needed is variable. Each facet of functionality will need a test written for it.
Resources
The ASUnit package includes Actionscript 2 and Actionscript 3 versions. Download the framework at:
Asynchronous Tests
Sometimes asynchronous code must be tested, such as an XML loading function. ASUnit can also
handle this type of code. You must subclass AsynchronousTestCase instead of TestCase. You must also
setup your listeners/callbacks so that your data is completely loaded before the run() method is called to
start the testing process. Essentially, you will be interrupting the constructor to wait for your data to load
and then continuing on to execute run() afterwards.