I thought that AS3 now has private abilities added. So why should I still preface private variables with an underscore?
private var _privVar:String;
I thought that AS3 now has private abilities added. So why should I still preface private variables with an underscore?
private var _privVar:String;
You don't have to. It's something that encourages readability, but is by no means mandatory. Entirely a personal preference.
I make it a general rule in ActionScript 3 to follow how Adobe writes there code.
Don't use underscores for private varibles unless your using a getter or setter. For example:
private var _foo:String;
public function get foo():String
{
return _foo;
}
public function set foo(value:String):void
{
_foo = value;
}
This example getter/setter is a little useless, as you could just make a public property that does the same thing. Only use a getter or setter when you need to do something special when you get or set the property. Even then, it's usually best just to create a public method instead.
Also another point. Personally I don't think it's a good idea to abreviate your variable or method names. So instead of calling my variable privVar, I would call it privateVariable. This is expecially true if you are using a coding IDE with autocomplete/suggest such as FlashBuilder(Flex Builder) or FlashDevelop.
Take a look at Adobe - coding conventions and best practices for more information.
Using an underscore is just a convention. And I try to avoid them because it messes with my intellisense. I'm used to typing obj.va
and hitting ctrl-space in flex builder to get obj.variableName
- this doesn't work well with _variableName
Btw, does earlier versions of ActionScript require you to preface "private" variable names with an underscore?