views:

193

answers:

3

I thought that AS3 now has private abilities added. So why should I still preface private variables with an underscore?

private var _privVar:String;
+1  A: 

You don't have to. It's something that encourages readability, but is by no means mandatory. Entirely a personal preference.

heavilyinvolved
+3  A: 

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.

TandemAdam
If the getter and setter aren't doing anything special, it would be faster to just make the variable public. It's not going to hurt anyone.
LiraNuna
exactly! My example was just a very simple one to express my point. It is quite rare that you would actually use getters or setters in AS3. Most of the time you make a public property or a public method.
TandemAdam
Quite rare? Hardly. I guess it depends on how you code, but I code against interfaces quite a bit and getter/setter is the only way to achieve this.
Joel Hooks
To me, using getters and setters seems like a bad design decision. But I guess it makes sense to use them to enforce Interfaces. Anything other reason and getters and setters would be a lazy and messy choice.
TandemAdam
A: 

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?

Amarghosh