A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Do you use "this" or use "_" ?

  1. #1
    Member
    Join Date
    Sep 2009
    Posts
    88

    Do you use "this" or use "_" ?

    Hi, while playing around in CS4 and coding with as3, on some custom made class I came across a pro... an annoyance.

    First is the reserved keywords, which, if you want to follow some good coding guidelines, you will not use the such as "height" "width" "size" as a property name.

    Second, if a parameter variable holds the same name as a class property (which occurred as I wanted to keep variable names simple and self-descriptive).

    So to distinct the 2 variable names above, the use of "this" or altering the name of the property by adding an underscore "_" is required.

    So it got me wondered if I should just keep my property names as I see fit, simple and relevant, and force myself to make the use of "this" all the time as a coding practice, or, not bother with using "this" but bother with always using the "_" instead.

    I'd like to hear the point of view that you veterans have here.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Personally, I use "this." to distinguish class member properties from parameter names when they would otherwise collide. Many people use a preceding underscore to indicate that a variable is private, and other people always use the underscores for function parameter names. Pick a style and stick to it. I don't believe there is an established standard for function parameter naming schemes.

  3. #3
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    i'll occasionally let arguments bump property names
    PHP Code:
    private var wide:Number;
    private var 
    tall:Number;
    public function 
    Box(wide:Number,tall:Number):void{
      
    this.wide wide;
      
    this.tall tall;

    but would never let a variable share a name as a class member. i use underscore only for private variables that're going to have a getter/setter
    PHP Code:
    private var _wide:Number;
    public function 
    get wide():Number{
      return 
    _wide;
    }
    public function 
    set wide(value:Number):void{
      
    _wide value;
      
    // do other stuff

    other than that, i don't use underscores or dollar signs or anything like that... even at the cost of verbosity, i'll preserve the uniqueness of members

    but like 5tons said, it's really just preference

  4. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    15
    Another vote for the "this." prefix.

    It should be noted that the "this." prefix is implicitly added whenever you access any member variable or member function from within the object. The only time it makes a difference is when a class member is shadowed, so you don't have to use it unless that's happening.

    My personal preference is to always use the prefix, even when unnecessary. The prefix tends to indicate a side-effecting operation and, at the same time, a member function which never uses a class member is a red flag, so adherence to such a rule makes those cases more obvious, etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center