FiftyOneReasons

Interactive Media Development + Exploration

The Case for Standardized AS3 Formatting

I will admit it – I’m fickle. I can’t stand opening up a class I’ve downloaded from great developers and finding a drastically different code format/style then the one we employ here. Granted, ours is slightly unique, but we have our reasons.

So I’d like to start a collaborative effort to standardize AS3 code formatting, so no one as anal as I am has to spend half an hour reformatting classes just so you feel you can read it.

Here’s the A51 formatting:

/**
 * Builds the ....
 *
 * @author      Joe Foobar
 * @version     0.1
 * @see         AnotherClass
 */
 
/**
 * We like to place brakets on a new line.
 * That way, they line up with their
 * corresponding closing bracket.
 * Class names are camel case.
 */
 
package
{
 
    class StandardAS3Format
    {
 
        import com.as3.pack.clss;
 
        /**
         * This is unusual, but it works well. Grouping by
         * access modifiers and delimiting with a comma
         * cleans up code nicely.
         * We also prefix global var names with
         * an underscore so they are easily identifiable
         * and user lowercase only.
         */
 
        private var
            _varone    :String,
            _vartwo    :String,
            _varthree  :int;
 
        public var
            _varfour    :uint,
            _varfive    :Object;
 
        public function StandardAS3Format()
        {
            //do something here
        }
 
        /**
         * Arguments passed to a method are prefixed with $.
         * Method names are lower camel case.
         * We always try and have a comment block describing
         * the method and any refences - required for ASDoc.
         */
 
        /**
         * Performs the ....
         *
         * @param       $arg1    description
         * @param       $arg2    description
         * @see         AnotherClass, anotherMethod
         */
 
        public function doSomething($arg1:String, $arg2:int):int
        {
            /**
             * Again, we have vars, each on a new line
             * seperated by a comma. Cleaner and faster!
             */
 
            var
                s1      :String = 'Hello World',
                s2      :String = 'Goodbye World',
                num     :int;
 
            for(num = 0; num < 10; num++)
            {
                //do something
            }
 
            return num;
        }
    }
}

All comments and suggestions welcome – this is obviously a work in progress.

Comments

Adam Jowett

Not a huge fan of the way the vars are formatted, but not a biggy. Main thing for me would be having the public vars with an underscore, considering not all of us let flash automatically instance name our stage assets, we have to declare them in the class, and having to instance name them with an underscore so that it fits with the class schema would be very odd.

So for me, lose the underscore for public vars.

Matt Fisher

Hey Adam,

Let me try and clear up some of the characteristics of our A51 syntax for you as well as the reason behind them.

We use an _varname (underscore) for ANY variable that is global to the class, be it public or private.

We use a $varname (dollar sign) for ANY param variable being passed into a method. Any variable created within a method, we be a normal variable name with no special characteristics.

The reason for this is to allow us to see just by the variable name, what we can do with it.

The underscores really have no impact on stage assets. For instance if I had a ball in my library and made it a movie clip with a linkage of ‘Ball’, I would just initialize it as follows:

If it was global to the class:
public var _ball:Ball = new Ball();
or
private var _ball:Ball = new Ball();

And if it was in a method:
private function foobar($ball:Ball):void {}

Finally if we initialize a ball from inside a method it would be as follows:
var ball:Ball = new Ball();

So as you can see the only usage of the $ and _ is in the ActionScript files not in any FLA.

I hope this clears up our ideas a little more, or maybe I am just misunderstanding what you mean by “flash automatically instance name our stage assets”. Either way, thanks for your comment and feel free to post any reccomendations or your personal syntax ideas, we would love to see them :) .

- Matt Fisher
A51 Integrated

Adam Jowett

I think there is a slight mis-understanding, but thanks for the detailed response.

I am all for using the $var method for variables passed to a method, use that currently so follow completely, I also use _var methodology for variables declared that are internal to a class, i.e. private vars, and use getters and setters if they need to be made available externally.

However if you do not declare instance names automatically in your flash settings, you need to declare anything with an instance name in it’s associated class, for example;

If you have a Form.as class associated to a contact form, and on stage there is a submit button with an instance name of submitButton. If I have flash not naming instance names automatically, I am required to declare that submit button in the class as a public variable to make it available. So I would declare something like:

public var submitButton:GenericButton;

it is this scenario that I am not a huge fan of declaring public vars with the underscore notation. Just my 2c, but keep up the good work.


Leave Your Mark




Feel free to use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">