Code Conventions

Coding standards for writing code in ActionScript 3

Warning: This document is not complete.

Packages

Packages have lower.case.names separated by periods if needed. Names using mixedCase (starting with lower case) are ok, but short package names are preferred so that you aren’t tempted to mixCase.

Classes

Classes have MixedCase names starting with a capital letter (and no hyphens).

Constants

Constants have CAPITALIZED_NAMES with words separated by underscores ‘_’.

Properties

Properties have mixedCase names starting with a lower case letter.

Events

Events have mixedCase names starting with a lower case letter.

Event Classes

While Apache Flex had lots of event classes, Royale strives to have as few as possible since each class has download overhead. Royale has an Event which has an event name in the “type” field and no other payload. A ValueEvent contains one item that might be of interest to the listener. ValueChangeEvent has oldValue/newValue. There may be a StringEvent and other ‘typed’ event classes.

Event constants go in the class that will dispatch the event. Thus the developer only has to import one class and write the code that uses it.

The reason behind this change is partly that JavaScript runtimes don’t really type-check event instances. Royale has code that will do the type-checking, but it is pretty rare to have to discriminate between event classes. So it is best to turn off type-checking code in Royale.

To compare:

Flex

public class TimerEvent {
  public const TIMER:String = "timer";
}

public class Timer {
   dispatchEvent(new TimerEvent(TimerEvent.TIMER));
}

Usage:

import TimerEvent;
import Timer;

var timer:Timer = new Timer();
timer.addEventListener(TimerEvent.TIMER, timerHandler);

function timerHandler(event:TimerEvent):void
{
}

Royale

public class Timer {
   public const TIMER:String = "timer";
   dispatchEvent(new Event(TIMER));
}

Usage:

import Timer;

var timer:Timer = new Timer();
timer.addEventListener(Timer.TIMER, timerHandler);

function timerHandler(event:Event):void
{
}