views:

141

answers:

4

What is the best practice/coding standard with regard to the "this" scope is AS3? Is there one? I feel it really helps with standardization and my readability, but sometimes it seems like "too much".

For instance, is the use of "this" in the following really necessary (I know it works without "this")?:

private var _item:Object;

private var selectedItem:Object;

public function set item(value:Object):void
{
    this._item = value;

    if (this._item["label"] == "doodad")
        this.selectedItem = value;
}

public function set item(value:Object):void
{
    return this._item;
}
+1  A: 

It certainly isn't necessary, but I agree that it can help with readability. Since I work more in more dynamic languages (e.g. Perl and Python), such conventions can be vital for quickly determining where variables and functions are scoped/located. If this convention works for you, I don't think it's a bad thing, per se.

Thus said, I've spent hours reformatting code which contained awkward conventions which impeded readability.

For example: one person I worked with wrote all assignments like this:

var foo:String= "bar";

This was irritating (I prefer " = " so I can clearly see the operator), and I spent a lot of time cleaning up thousands of lines of code I had to maintain. His convention (which, though we argued about several times, he refused to compromise on) tended to impede my work.

Strive for unity w/others working with you. If they need to support your code and find this aggravating, it's likely not worth it to leave it in. If you don't expect anyone to work directly with the source, use conventions which help you understand your code and document (somewhere) what they mean.

bedwyr
+1  A: 

If you're working in a team, stick with the coding conventions of the team.

But personally, I find explicit use of "this", when not required for disambiguation, overkill that negatively affects readability in a statically typed language like AS3 (dynamic languages are another story!).

A class should only really have one responsibility so generally there shouldn't be too many properties on it. Inside a method you generally deal with three types of variables: temporary local variables, method parameters, and properties. Methods shouldn't be too long, so it should be easy to spot the difference between the three types - if it's not defined locally and hasn't been passed as a parameter, then it's a property. If the whole method doesn't fit on your screen then it's probably too long!

I only use "this" when needed to disambiguate between a property and a parameter with the same name.

Darscan
Yes, I think it's more important to follow the standard of not giving variables/properties the same names in different scopes.
Eric Belair
A: 

I prefer not to use "this" too much, but sometimes do in Eclipse, just to get autocompletion (probably the worst reason to do it!)

Would make more sense if your example was:

public function set item(_item:Object):void
{
    this._item = _item;

    if (this._item["label"] == "doodad")
        this.selectedItem = this._item;
}
Iain
I use Flex Builder 3 mostly and simply use CTRL+SPACE for autocomplete. Not sure how it works with the Eclipse plug-in though.
Eric Belair
Yes, found this the other day!
Iain
+3  A: 

"this" is not required unless you want to prevent naming conflicts between locally scoped variables (method params for instance) and instance variables.

In your example you are already using an underscore to mark a private variable, so it's an extra reason not to use "this" since you are really saying twice the same thing.

Christophe Herreman
Yeah, I've pretty much decided to avoid using "this", because it only ENCOURAGES developers to reuse variable names across scopes, which in itself is a bad practice.
Eric Belair