User Guide

102 Chapter 3: Using Best Practices
Programming classes
There are several general guidelines for programming classes. These guidelines help you write
well-formed code, but also remember to follow the guidelines provided in “General coding
conventions” on page 69 and ActionScript coding standards” on page 82. When you program
classes, follow these guidelines:
Do not use objects to access static methods and variables. Do not use myObj.classMethod();
use a class name, such as
MyClass.classMethod().
Do not assign many variables to a single value in a statement, because it is difficult to read, as
the following ActionScript shows:
play_btn.onRelease = play_btn.onRollOut = playsound;
or:
class User {
private var m_username:String, m_password:String;
}
Have a good reason for making public instance or public static, class, or member variables.
Make sure that these variables are explicitly public before you create them this way.
Do not use too many getter/setter functions in your class files, and access them frequently in
your application.
Using prefixes in classes
Whenever possible, use the
this keyword as a prefix within your classes for methods and member
variables. It is easy to tell that a property or method belongs to a class when it has a prefix;
without it, you cannot tell if the property or method belongs to the superclass.
For an example of using prefixes in classes, see the UserClass in “Creating and organizing classes
on page 100.
You can also use a class name prefix for static variables and methods, even within a class. This
helps qualify the references you make, which makes code readable. Depending on what coding
environment you are using, using prefixes might also trigger code completion and hinting.
You do not have to add these prefixes, and some developers feel it is unnecessary. Adding the
this
keyword as a prefix is recommended, because it can aid readability and helps you write clean code
by providing context.
Using comments in classes
Using comments in your classes and interfaces is an important part of documenting them. Start
all your class files with a comment that provides the class name, its version number, the date, and
your copyright. For example, you might create documentation for your class that is similar to the
following comment:
/**
User class
version 1.2
3/21/2004
copyright Macromedia, Inc.
*/