6 Replies Latest reply on Jan 13, 2010 12:38 AM by pauland

    renaming instance variable VS use of "this."

    Bertrand G.

      Open Topic:

       

      Do you prefer this code?

      private var _myVar:int;
      
      public function myClassConstructor(value:int) {
           _myVar = value;
      }
      

       

      or this code? (JAVA style)

      private var myVar:int;
      
      public function myClassConstructor(myVar:int) {
           this.myVar = myVar;
      }
      

       

      Discuss the pros and cons, especially if performance is involved.

      You have 10 minutes... I will be collecting the papers

        • 1. Re: renaming instance variable VS use of "this."
          pauland Level 4

          The use of underscore is a naming convention favoured by many to denote private variables and is not particularly a Java thing or an Actionscript thing.

           

          The same is true of "this". It clarifies the scope of the variable but isn't really required in either language. Most often I don't use "this" unless I want to be absolutely clear or where it has to be used to get the right scope.

           

          It's very much a matter of taste and not Actionscript v. Java.

          • 2. Re: renaming instance variable VS use of "this."
            jake_flex Level 2

            I use the underscore version with ActionScript just because I started using it for defining private varialbes in Flex mxml files. It's more of habit. As a matter of fact, when I did more work with Python (where 'this' is always required for anything other than globals) and then switched back to Java, I noticed myself using 'this' in places where it was not even needed with Java.

             

            My usage is about the same as paul above stated, but with certain habits as mentioned. When working on a project with Flex frontend and Java backend, I tend to use the underscore also in the Java code.

             

            What about using both. I've sometimes seen code like

            this._value = var;
            

             

            To me it sounds silly or having really odd conventions. Any comments on this?

            • 3. Re: renaming instance variable VS use of "this."
              pauland Level 4

              this._value = someVar;

               

              is a mixture of two concepts.

               

              This specifies the scope in which the variable _value exists,

              _value indicates that the variable is private to the class.

               

              No problem with that at all.

               

              Paul

              • 4. Re: renaming instance variable VS use of "this."
                jake_flex Level 2

                pauland wrote:

                 

                this._value = someVar;

                 

                is a mixture of two concepts.

                 

                This specifies the scope in which the variable _value exists,

                _value indicates that the variable is private to the class.

                 

                No problem with that at all.

                 

                Paul

                Yep, you're right. After thinking, it makes perfect sense.

                 

                Edit: But then again, private variables are always in the class scope, so using 'this' is unnecessary.

                • 5. Re: renaming instance variable VS use of "this."
                  Bertrand G. Level 1

                  So no one is concerned about any performance issues using 'this'... I have seen somewhere that using 'this' in C++ is not as efficient as renaming the variable, because it makes an object instantiation or something like that.

                   

                  I personally prefer using this for the setters and getters, this way I can have the same variable name for both the method parameters and the class variable.

                   

                  Just my 2 cents...

                  • 6. Re: renaming instance variable VS use of "this."
                    pauland Level 4

                    Bertrand G. wrote:

                     

                    So no one is concerned about any performance issues using 'this'... I have seen somewhere that using 'this' in C++ is not as efficient as renaming the variable, because it makes an object instantiation or something like that.

                     

                     

                    Most people will go for clarity of purpose in the code in preference to performance, simply because performance is not a critical factor for MOST purposes.

                     

                     

                    I personally prefer using this for the setters and getters, this way I can have the same variable name for both the method parameters and the class variable.

                     

                    Just my 2 cents...

                     

                    I think the usual practice for getters and setters is to have a private variable for the instance variable and to name it with an underscore prefix to indicate that the variable is private. In that way references to the private variable are distinct to the variable name used in the setter and getter. In any event, in my setter I invariably name the parameter with the new value "newValue" or "newWhatever", some people just use "pWhatever" to indicate that variables have been passed as parameters, so the use of "this" is not required.

                     

                    I don't see the use of "this" as particularly relevant to performance, or getters and setters, and I would recommend naming private variables with an underscore prefix to indicate the use of a private instance variable, though not everyone follows this convention and may use other conventions, or indeed no convention at all.

                     

                    Paul