ActionScript guidelines for better C/C++

28 February 2013

To help make bringing ActionScript code to C/C++ and mobile there are some simple recommendations that make this easier and also better ActionScript code.

  • Prefer strong typing
  • Prefer passing arguments to functions over property bags
  • Prefer typed values over strings, e.g. var level:String = “10”;
  • Prefer vector to Array
  • Prefer named functions to anonymous/lamba functions
  • Prefer adding comments on type for Arrays and Dictionaries, e.g. var dict:Dictionary = new Dictionary(); // dictionary of string to int
  • Prefer commenting if you’re using one type to emulator another, .e.g if you’re using a Dictionary to emulate a set or a Vector to emulate a list.
  • Prefer int in for loops, avoid for (var i:Number = 0; i < 10; ++i)
  • Prefer simple events, avoid custom bubbling and propagation behavior
  • Prefer grouping data by scope, e.g. all publics, all protected, all private
  • Prefer fewest number of imports
  • Prefer single statements per line, avoid sprite1.visible = sprite2.visible = sprite3.visible = false;
  • Prefer timer() to Date() for short times
  • Prefer classes that perform operations on their own data, avoid providing accessor to class data that is modified by third-parties, e.g. HighScores::getHighScores.erase(30);
  • Prefer your own seed able random number generator
  • Prefer multi-platform frameworks to Adobe supplied/ActionScript specific frameworks
  • Prefer standard communications protocols to Adobe suppled/ActionScript specific protocols, e.g. AMF
  • Prefer anything else to regexp
  • Prefer ActionScript 3 to ActionScript 2

  • Avoid property bags
  • Avoid literals that share a name in the C/C++ namespace, e.g. var class:int; var union:int; var bool:Boolean;
  • Avoid hasOwnProperty
  • Avoid getClassDefinition
  • Avoid undefined
  • Avoid allocating memory, allocating a Point() and passing it to a function when you could pass x:Number, y:Number
  • Avoid deep inheritance hierarchies
  • Avoid multiple inheritance
  • Avoid returning multiple values/property bags
  • Avoid language specific features
  • Avoid instantiating through variable names, e.g. var thing:* = new Class() as Thing;
  • Avoid * in imports
  • Avoid embedding XML in source files
  • Avoid embedding assets
  • Avoid functions with set/get prefix and have members of the same name, as accessors are implemented explicitly. e.g. accessor for x becomes getX(), avoid functions called getX()
  • Avoid overloading events, i.e. keep the number of events and especially property bags with them low
  • Avoid array notation for calling methods, e.g. myClass[“myMethod”]();
  • Avoid movie clips

  • Prefer atomic binary files, e.g. no proprietary metadata
  • Prefer standard file formats