Good Habits:Variable Names

Writing your AS becomes easier if you develop a few good habits. Defining and naming variables is something you will find yourself doing often. Here some conventions that I always follow:

I always name private variables beginning with an underscore. For example:

private var _radius:Number;

I never define the value for a variable in the declaration. Instead I define initial values in the constructor of the class or a function that is called from the constructor. If I am passing an argument variable that will set the value of a class variable I use the same name, without the underscore. For example, the following defines a simple class with two private variables which are passed as arguments in the constructor:

package {
	import flash.display.*;

	public class Ball extends Sprite {
		private var _radius:Number;
		private var _color:uint;

		public function ball( radius:Number, color:uint ) {
			_radius = radius;
			_color = color;
		}
	}
}

Here the class defines both _radius and _color. These are private so they begin with the underscore (_). They get their values from the two argument (or parameter) variable radius and color (note that these names lack the underscore). Then I set _radius = radius and _color = color in the constructor.

You can come up with your own system, this is what I use. Best is to have a system and use it the same way every time. I like this system because the naming system is very clear.

Leave a Reply

Your email address will not be published. Required fields are marked *