You are on page 1of 16

Vendor Coding Guidelines

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.

In order for code to be considered acceptable:


ƒ Code must fully meet functional requirements
ƒ Code must be written in Actionscript 3.0 to attached coding standards, including full
ASDoc-compatible commenting (See Actionscript 3 Style Guide).
ƒ Code must compile correctly under Flash CS3.
ƒ Code must include Unit test code per the attached guidelines.
ƒ Code must be delivered per the schedule attached to the statement of work.
ƒ Code will be reviewed by Pearson to ensure conformance to all guidelines.

Actionscript Coding standards—Page 1


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
ActionScript 3 Basic Style Guide

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.

1. Example: import fl.display.Sprite;


2. Avoid: import fl.display.*;

4. Class or interface definition


5. static variables (in alphabetical order, if any)
6. Instance variables
7. Constructor
8. Getters and setters. Note: use sparingly; there’s no point using getter/setter in cases where a plain
property variable would suffice). Example:

private var isEnabled:Boolean = true;


private var enabledChanged:Boolean = false;

public function get enabled():Boolean {


return isEnabled;
}

public function set enabled(value:Boolean):void {


isEnabled = value;
enabledChanged = true;
}
9. Methods (Grouped by functionality, not by scope.)

Actionscript Coding standards—Page 2


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
General rules
• Indent using tabs.
• The reference tab size is 4 spaces. Your code editor should be configured this way.
• Code lines should not exceed 100 characters.
• Separate functions/methods by one or more empty lines.
• Don’t be afraid of using whitespace.

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;

Initialize the variable, if you can.

Right:
public var isAdmin:Boolean = false;
Wrong:
public var isAdmin:Boolean;
//false is the default in AS3 for Boolean, not undefined!

Actionscript Coding standards—Page 3


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
Curly braces and parentheses
Spaces should not be placed between the object name and type.

Example:
public class Example extends MovieClip {

private var ball:Object;

public function addBall(b:Object):void {


ball = b;
...
}

public function anotherMethod():void {


while (true) {
...
}
}
}

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);

Actionscript Coding standards—Page 4


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
Conditional if, else if, else
if (condition) {
simpleStatement;
}

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 for each..in


for each (var iterator:String in someObject) {
statements;
}

Actionscript Coding standards—Page 5


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
Loop while
while (condition) {
statements;
}

Loop do..while
do {
statements;
} while (condition);

Error handling try..catch..finally


try {
statements;
} catch (e:Event) {
statements;
}

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

Actionscript Coding standards—Page 6


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
Blank spaces
Use blank spaces to separate a keyword from its parentheses and don’t use a space to separate
the method name from its parentheses.
Example:
while (true) {
getSomething();
}

A blank space must exist after every comma in an arguments list.


Example:
addSomething(data1, data2, data3);
All binary operators must be separate from its operands by a space. Don’t use a space with
unary operators (++, --, etc.).
Example:
a += (5 + b) / c;
while (d as int < f) {
i++;
}

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++) {

Actionscript Coding standards—Page 7


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
Commenting

ASDoc Generated Documentation


Comments will include ASDoc tags in them, which allows HTML documentation to be built from
the source code, using your comments (similar to JavaDocs). As long as the comments are
properly formatted, the ASDoc tool can run and build documentation off of your source.

Example of ASDoc HTML Documentation:

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:

Actionscript Coding standards—Page 8


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
//bail if we have no columns to display
if (!visibleColumns || visibleColumns.length == 0) {
return false;
}

Wrong:
//sets column numbers variables to zero
colNum = 0;

ASDoc Commenting Guidelines


In the following examples, note the extra * in the opening of the comment block. Also, note that
several HTML tags can be used in the comments, such as <listing>,<img>,<table>. <p> is
required for paragraphs after the first. For more information, see:

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 {}

Actionscript Coding standards—Page 9


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
Property:
/** A private property String. */
private var privateProperty:String = "Keep Off: Private
Property";

Naming Conventions for Code

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

Actionscript Coding standards—Page 10


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
UpperCamelCase. Examples:

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

Although, Boolean variables should start with can, is or has.

private var isListeningForRender:Boolean = false;


private var canEditUsers:Boolean = true;
private var hasAdminPrivileges:Boolean = false;

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.

for (var i:int = 0; i < 10; i++) {

Actionscript Coding standards—Page 11


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
Catch variables should be named e, no matter what the error type.

catch (e:Error) {
hasFieldName = false;
...
}

Constants
Constants must be all uppercase, using underscores to separate words:

public static const DEFAULT_MEASURED_WIDTH:Number = 160;


public static const AUTO:String = "auto";

Actionscript Coding standards—Page 12


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
General Practices
• Use the keyword FIXME inside comments to flag something that’s broken and should be
fixed.
• Use TODO to flag something that works but can be improved by refactoring or for
something that needs to be done later.
• Assign the iterator value to a variable before using it if the performance improvement will
be significant (e.g., in simple arrays it isn’t necessary).

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) {

Actionscript Coding standards—Page 13


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
• Use constants if available.

Right:
myButton.addEventListener(MouseEvent.CLICK, myHandler);

Wrong:
myButton.addEventListener("click", myHandler);

• Use specific types if available.

Right:
private function mouseMoveHandler(event:MouseEvent):void
{

Wrong:
private function mouseMoveHandler(event:Event):void {

• Anonymous functions are permissible for relatively simple situations. Example:

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

Actionscript Coding standards—Page 14


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
to
transient
true
try
type
typeof
use
var
virtual
void
volatile
while
with

Actionscript Coding standards—Page 15


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.
UNIT TESTING / ASUNIT

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.

Following is a sample list of objectives to test for:


• All functions and methods that return a value
• Test that the proper data gets loaded
• Test that media loads (if activity is designed to load images, videos, etc.)

Resources
The ASUnit package includes Actionscript 2 and Actionscript 3 versions. Download the framework at:

A good tutorial is located here:

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.

Actionscript Coding standards—Page 16


The information in this document is privileged and confidential. It is intended solely for the use of authorized recipients.

You might also like