This section covers how we format our codes.

Table of Contents

Line width

Wrap code to 80-character lines. This has the following advantages:

  • Developers with smaller screens don’t have to scroll horizontally to read long lines.
  • A comparison utility can display two versions of a file side-by-side.
  • The font size can be increased for projection before a group without requiring scrolling.
  • The source code can be printed without clipping or wrapping.

Indentation

Use tabs for indentation.

Separation of declarations

Use a single blank line as a vertical separator between constant, variable, or function declarations.

1
2
3
4
5
6
7
8
9
10
/**
* @private
* Holds something.
*/
var a:Number;
 
/**
* @private
*/
var b:Number

Array indexing

Don’t put any spaces before or after the left bracket or before the right bracket.

1
2
3
4
5
// Do this:
a[0]
 
// Not this:
a[ 0 ]

Commas

Follow a comma with a single space. This applies to argument lists, array literals, and object literals.

Array literals

Put a single space after the left bracket and a single space before the right bracket, and put a single space after (but none before) each comma.

1
2
3
4
5
6
// Do this:
[ 1, 2, 3 ]
 
// Not these:
[1, 2, 3]
[1,2,3]

An empty array is a special case.

1
2
3
4
5
// Do this:
[]
 
// Not this:
[ ]

Format lengthy array initializers requiring multiple lines with aligned brackets:

1
2
3
4
5
6
7
8
9
10
11
12
13
static var numberNames:Array /* of String */ =
[
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine"
];

Object literals

Put a single space after the left brace and a single space before the right brace, and put a single space after the colon separating the property name and value.

1
2
3
4
5
6
7
// Do this:
{ a: 1, b: 2, c: 3 }
 
// Not these:
{a: 1, b: 2, c: 3}
{a:1, b:2, c:3}
{a:1,b:2,c:3}

An empty Object is a special case.

1
2
3
4
5
// Do this:
{}
 
// Not this:
{ }

Format lengthy object initializers requiring multiple lines with aligned braces:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static var TextStyleMap:Object =
{
    color: true,
    fontFamily: true,
    fontSize: true,
    fontStyle: true,
    fontWeight: true,
    leading: true,
    marginLeft: true,
    marginRight: true,
    textAlign: true,
    textDecoration: true,
    textIndent: true
};

Type declarations

Don’t put any spaces before or after the colon that separates a variable, parameter, or function from its type.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Do this:
var n:Number;
 
// Not these:
var n : Number;
var n: Number;
 
// And this:
function f(n:Number):void
 
// Not these:
function f(n : Number) : void
function f(n: Number): void

Operators and assignments

Put a single space around the assignment operator.

1
2
3
4
5
// Do this:
a = 1;
 
// Not this:
a=1;

Put a single space around infix operators.

1
2
3
4
5
// Do this:
a + b * c
 
// Not this:
a+b*c

Put a single space around comparison operators.

1
2
3
4
5
// Do this:
a == b
 
// Not this:
a==b

Don’t put any spaces between a prefix operator and its operand.

1
2
3
4
5
//Do this:
!o
 
// Not this:
! o

Don’t put any spaces between a postfix operator and its operand.

1
2
3
4
5
// Do this:
i++
 
// Not this:
i ++

Statements

Start each statement on a new line, so that you can set a breakpoint on any statement.

1
2
3
4
5
6
7
// Do this:
a = 1;
b = 2;
c = 3;
 
// Not this:
a = 1; b = 2; c = 3;

Align the braces of statement blocks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Do this:
function f():void
{
    var n:int = numChildren;
    for (var i:int = 0; i < n; i++)
    {
        if ()
        {
            x = horizontalGap * i;
            y = verticalGap * i;
        }
    }
}
 
//Not this:
function f():void {
    var n:int = numChildren;
    for (var i:int = 0; i < n; i++) {
        if () {
            x = horizontalGap * i;
            y = verticalGap * i;
        }
    }
}

Function declarations

1
2
3
4
5
6
// Do this:
f(a, b)
 
// Not these:
f(a,b)
f( a, b )

If the parameters have to wrap, indent the subsequent lines after the left parenthesis. Put multiple parameters per line if they fit. Otherwise, put one per line. If even one won’t fit, put the first one on the second line, indented past the beginning of the function name.

1
2
public function foo(parameter1:Number, parameter2:String,
                              parameter3:Boolean):void

Function calls

1
2
3
4
5
6
// Do this:
f(a, b)
 
// Not these:
f(a,b)
f( a, b )

if statements

Follow the if keywords with a single space before the left parenthesis. Don’t put any spaces after the left parenthesis or before the right parenthesis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Do this:
if (a < b)
 
// Not these:
if(a < b)
if( a < b )
if ( a < b )
 
// Do this:
if (condition)
{
    statements;
}
else
{
    statements;
}
 
// With many if-else if
if (condition)
{
    statements;
}
else if (condition)
{
    statements;
}
else
{
    statements;
}

for statements

Follow the for keyword with a single space before the left parenthesis. Don’t put any spaces after the left parenthesis or before the right parenthesis.

1
2
3
4
5
6
7
// Do this:
for (var i:int = 0; i &lt; n; i++)
 
// Not these:
for(var i:int = 0; i &lt; n; i++)
for( var i:int = 0; i &lt; n; i++ )
for ( var i:int = 0; i &lt; n; i++ )

If the for clause needs to wrap, indent the subsequent lines after the left parenthesis.

1
2
3
for (var aLongLoopVariableName:int = aLongInitialExpression;
      aLongLoopVariableName &lt; aLongUpperLimit;
      aLongLoopVariableName++)

do…while statements

1
2
3
4
5
do
{
    statements;
}
while (condition);

while statements

1
2
3
4
while (condition)
{
    statements;
}

for…in statements

1
2
3
4
for (var iterator:type in someObject)
{
    statements;
}

switch statements

Follow the switch keyword with a single space before the left parenthesis. Don’t put any spaces after the left parenthesis or before the right parenthesis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Do this:
switch (n)
 
// Not these:
switch(n)
switch( n )
switch ( n )
 
// Do this:
switch (n)
{
    case 1:
    {
        a = foo();
        break;
    }
    case 2:
    {   a = bar();
        break;
    }
    default:
    {
        a = blech();
        break;
    }
}

try…catch..finally

1
2
3
4
5
6
7
8
9
10
11
12
try
{
    statements;
}
catch (e:Type)
{
    statements;
}
finally
{
    statements;
}

with

1
2
3
4
with (this)
{
    alpha = 0.5;
}