Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3: should private variables have an _

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

private var _privVar:String;
like image 232
Dan Avatar asked Nov 28 '09 19:11

Dan


2 Answers

I make it a general rule in ActionScript 3 to follow Adobe's style.

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 abbreviate your variable or method names. So instead of calling my variable privVar, I would call it privateVariable. This is especially true if you are using an IDE with autocomplete/suggest such as FlashBuilder(Flex Builder) or FlashDevelop.

Take a look at Adobe - coding conventions and best practices for more information.

like image 90
Adam Harte Avatar answered Sep 19 '22 11:09

Adam Harte


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

like image 41
heavilyinvolved Avatar answered Sep 16 '22 11:09

heavilyinvolved