ActionScript 3.0 Naming Conventions

Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it’s a constant, package, or class-which can be helpful in understanding the code In a nutshell it’s about injecting as much meaning into your code as possible, while still remaining terse and concise. I could just suggest you meditate on the phrase, semantic richness.

Packages

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

The second part of your top level package should be named after your organisation type, or the organisations name itself, eg. package name tv.simplymedia for simplymedia.tv. then your package begins to describe the code sections themselves, eg. app, view, controller, model etc..

example package names

com.mydomain.app
jp.co.domain.viewer.app
edu.mit.logger

Classes

Class names should be nouns, in mixed case with the first letter of each internal word capitalised. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations, waive this rule if the abbreviation is much more widely used than the long form, such as URL or HTML, in these cases use the form Url, Html etc…

example class names

class Raster {...}
class ImageSprite {...}

Interfaces

Interface names should be capitalised like class names but given a “I” prefix. They should also describe the set of methods (behaviours) which they enforce.

example interface names

interface IDisplayable
interface IStoring

Methods

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

example method names

run();
runFast();
getBackground();

Variables

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter, subsequent words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed. However private and protected class properties should be prefixed with _(underscore) if they are to be used with public get/set functions.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary “throwaway” variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters. However, avoid over use of these single letter vars for anything other than iterators.

i:int;
name:String;
baseWidth:Number;
private var _height:Number;

public function get height():Number {
    return _height;
}

public function set height(value:Number):void {
    _height = value;
}

Constants

The names of variables declared class constants should be all uppercase with words separated by underscores (“_”).

public static const MIN_WIDTH:Number = 4;
public static const MAX_WIDTH:Number = 999;

 
 
 

5 Responses to “ActionScript 3.0 Naming Conventions”

  1. Anonymous
    23. January 2010 um 02:25

    Thanks for an informative article.

    No mention was made of conventions for name Symbols. I’ve seen “mc” used in the naming of MovieClips. Is that for the instance name or the symbol name? Also, should the mc be placed at the front or back of the symbol’s name?

  2. Jason Milkins
    23. January 2010 um 08:08

    Symbol names should be like a regular Class name for a component, a camel case, capitalised noun based name.

    The mc convention no longer applies, generally the 2 letter prefix / suffix is no longer practical, and would only appear in modern AS3 code due to a developers habitual style.

    Any strongly typed instance field/property will show it’s data type within Flash Builder (as a code tool tip) or will ‘code assist’ with the available properties, Flash CS5’s AS editor will work along these lines too.

  3. DEAN
    18. April 2010 um 07:57

    I don’t name libraries using the domain convention,

    A domain name can change, run out, get bought by squatters, etc, etc.
    But a good library will last forever.

    I prefer to use something like -

    package deansmith.as3.math3d.core {
    ….
    }

  4. Jason Milkins
    18. April 2010 um 08:44

    Good point, I think as long as the package name is identifiable to a team / company / individual developer, whatever the case requires.

  5. jase21
    21. June 2010 um 22:02

    Agrees with Dean.

Leave a Reply