Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Best practices question: Do you use the this keyword when using instance members in a method?

Best practices question: Do you use the this keyword when using instance members in a method?

Scheduled Pinned Locked Moved The Lounge
questioncsharpc++javavisual-studio
42 Posts 18 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • realJSOPR realJSOP
    1. I use the first thing, and have never had to worry about "name clashes". 2) In your third code block, "this." is not going to compile. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
    V Offline
    V Offline
    Vikram A Punathambekar
    wrote on last edited by
    #10

    C# Cheers, Vikram.


    "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.

    1 Reply Last reply
    0
    • V Vikram A Punathambekar

      [EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]

      class Car
      {
      private int speed;
      // ...
      }

      If you have a class Car with an instance member speed, how do you refer to it in Car's methods?

      public void SpeedUp(int delta)
      {
      speed += delta;
      }

      or

      public void SpeedUp(int delta)
      {
      this.speed += delta;
      }

      I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.


      "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #11

      NO! Marc Pensieve

      V 1 Reply Last reply
      0
      • realJSOPR realJSOP

        Is that C++? If it is, what exactly does that code do? I've never seen "this->!CMyClass();" before, and neither have the other programmers in our shop. If it's not C++, don't respnd to this post. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #12

        I think it is possible if you overload the () and ! operators in your class. -------- "I say no to drugs, but they don't listen." - Marilyn Manson

        1 Reply Last reply
        0
        • N Nish Nishant

          John Simmons / outlaw programmer wrote:

          Is that C++? If it is, what exactly does that code do? I've never seen "this->!CMyClass();" before, and neither have the other programmers in our shop. If it's not C++, don't respnd to this post.

          That's C++/CLI which is still C++ :-) It's the new syntax available in VC++ 2005 that lets you write managed code. Regards, Nish

          My blog : Nish’s thoughts on MFC, C++/CLI and .NET

          V Offline
          V Offline
          Vikram A Punathambekar
          wrote on last edited by
          #13

          [edit] This was a reply to Nish. :suss: [/edit] *whew* I thought it was regular C++ and went "What on earth is that? :wtf: Have I forgotten C++ syntax in just a year?" I also posted a question on your blog. :-O Cheers, Vikram.


          "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006

          1 Reply Last reply
          0
          • D Dan Neely

            I use this. because of intesence. I tend toward long descriptive variable/method names, and with intelisence can generally get away with typing 7-10 chars instead of 15-25.

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #14

            dan neely wrote:

            because of intesence.

            In VS2005, intellisense kicks in without needing "this.". :-D Marc Pensieve

            D N 2 Replies Last reply
            0
            • M Marc Clifton

              NO! Marc Pensieve

              V Offline
              V Offline
              Vikram A Punathambekar
              wrote on last edited by
              #15

              An unqualified denial, eh? :) Cheers, Vikram.


              "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.

              M 1 Reply Last reply
              0
              • V Vikram A Punathambekar

                [EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]

                class Car
                {
                private int speed;
                // ...
                }

                If you have a class Car with an instance member speed, how do you refer to it in Car's methods?

                public void SpeedUp(int delta)
                {
                speed += delta;
                }

                or

                public void SpeedUp(int delta)
                {
                this.speed += delta;
                }

                I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.


                "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006

                G Offline
                G Offline
                Gizzo
                wrote on last edited by
                #16

                I used this just to diferenciate between members variables and the rest of them, ...until someone told me about the hungarian notation :rolleyes:

                C 1 Reply Last reply
                0
                • V Vikram A Punathambekar

                  [EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]

                  class Car
                  {
                  private int speed;
                  // ...
                  }

                  If you have a class Car with an instance member speed, how do you refer to it in Car's methods?

                  public void SpeedUp(int delta)
                  {
                  speed += delta;
                  }

                  or

                  public void SpeedUp(int delta)
                  {
                  this.speed += delta;
                  }

                  I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.


                  "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006

                  S Offline
                  S Offline
                  Shog9 0
                  wrote on last edited by
                  #17

                  Former. Always. If i have to worry about name clashes, the code isn't going to be readable anyway, since either i'm using way too many global variables, or the method has become so long that i cannot see at a glance which local variables have been defined. Either way, i have bigger problems.

                  ---- Scripts i've known... CPhog 0.9.9 - make CP better. Forum Bookmark 0.2.1 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    dan neely wrote:

                    because of intesence.

                    In VS2005, intellisense kicks in without needing "this.". :-D Marc Pensieve

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #18

                    Marc Clifton wrote:

                    In VS2005, intellisense kicks in without needing "this.".

                    .net2.0 won't run on NT4, so I can't upgrade until the client retires all it's relics :(

                    1 Reply Last reply
                    0
                    • M Marc Clifton

                      dan neely wrote:

                      because of intesence.

                      In VS2005, intellisense kicks in without needing "this.". :-D Marc Pensieve

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #19

                      Marc Clifton wrote:

                      In VS2005, intellisense kicks in without needing "this.".

                      Only for C# and possibly VB. It doesn't do that for C++ :-( Regards, Nish

                      My blog : Nish’s thoughts on MFC, C++/CLI and .NET

                      1 Reply Last reply
                      0
                      • V Vikram A Punathambekar

                        [EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]

                        class Car
                        {
                        private int speed;
                        // ...
                        }

                        If you have a class Car with an instance member speed, how do you refer to it in Car's methods?

                        public void SpeedUp(int delta)
                        {
                        speed += delta;
                        }

                        or

                        public void SpeedUp(int delta)
                        {
                        this.speed += delta;
                        }

                        I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.


                        "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006

                        M Offline
                        M Offline
                        Member 96
                        wrote on last edited by
                        #20

                        Since no one gave my own personal answer on this: I use this in c# when I don't feel like typing out the whole variable name. So if you look in my code you will find it randomly about 30% of the time. It's a bit of a non-issue really. If intellisense didn't require it (and it sounds like it might not in vs 2k5) I wouldn't use it at all, why type more than necessary.

                        J 1 Reply Last reply
                        0
                        • M Member 96

                          Since no one gave my own personal answer on this: I use this in c# when I don't feel like typing out the whole variable name. So if you look in my code you will find it randomly about 30% of the time. It's a bit of a non-issue really. If intellisense didn't require it (and it sounds like it might not in vs 2k5) I wouldn't use it at all, why type more than necessary.

                          J Offline
                          J Offline
                          Judah Gabriel Himango
                          wrote on last edited by
                          #21

                          Same here. A few months ago I discovered the CTRL+J trick, which has cut down on my 'this' usage more yet.

                          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

                          M 1 Reply Last reply
                          0
                          • J Judah Gabriel Himango

                            Same here. A few months ago I discovered the CTRL+J trick, which has cut down on my 'this' usage more yet.

                            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

                            M Offline
                            M Offline
                            mitooki
                            wrote on last edited by
                            #22

                            CTRL+[space] does a similar thing and is easier to hit in a hurry.. ;)

                            1 Reply Last reply
                            0
                            • V Vikram A Punathambekar

                              An unqualified denial, eh? :) Cheers, Vikram.


                              "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.

                              M Offline
                              M Offline
                              Marc Clifton
                              wrote on last edited by
                              #23

                              Vikram A Punathambekar wrote:

                              An unqualified denial, eh?

                              YES!!! ;P It's too much clutter. The only reason I see programmers do this is for Intellisense. Geez, these kids nowadays. (Besides, with VS2005, Intellisense kicks in as you start typing). Actually, the only time I use "this." is if I have a parameter with the same name as the field, and the ONLY time I do that is in a constructor, when the parameters in the constructor are initializing fields. Elsewhere, I use more descriptive names if there's going to be a collision, both in parameters and in local variables. Marc Pensieve

                              1 Reply Last reply
                              0
                              • V Vikram A Punathambekar

                                [EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]

                                class Car
                                {
                                private int speed;
                                // ...
                                }

                                If you have a class Car with an instance member speed, how do you refer to it in Car's methods?

                                public void SpeedUp(int delta)
                                {
                                speed += delta;
                                }

                                or

                                public void SpeedUp(int delta)
                                {
                                this.speed += delta;
                                }

                                I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.


                                "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006

                                C Offline
                                C Offline
                                Colin Angus Mackay
                                wrote on last edited by
                                #24

                                I use the this keyword. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

                                T 1 Reply Last reply
                                0
                                • G Gizzo

                                  I used this just to diferenciate between members variables and the rest of them, ...until someone told me about the hungarian notation :rolleyes:

                                  C Offline
                                  C Offline
                                  Colin Angus Mackay
                                  wrote on last edited by
                                  #25

                                  Gizzo wrote:

                                  I used this just to diferenciate between members variables and the rest of them, ...until someone told me about the hungarian notation

                                  :omg: You still use that! :-D ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

                                  G 1 Reply Last reply
                                  0
                                  • C Colin Angus Mackay

                                    Gizzo wrote:

                                    I used this just to diferenciate between members variables and the rest of them, ...until someone told me about the hungarian notation

                                    :omg: You still use that! :-D ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

                                    G Offline
                                    G Offline
                                    Gizzo
                                    wrote on last edited by
                                    #26

                                    Colin Angus Mackay wrote:

                                    You still use that!

                                    what? the hungarian notation or this keyword? I use the hungarian notation. What's wrong with it? We have to use one, anyway.

                                    C S L 3 Replies Last reply
                                    0
                                    • C Colin Angus Mackay

                                      I use the this keyword. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

                                      T Offline
                                      T Offline
                                      toxcct
                                      wrote on last edited by
                                      #27

                                      Colin Angus Mackay wrote:

                                      I use the this keyword.

                                      so do i !!! :doh:


                                      TOXCCT >>> GEII power
                                      [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                      1 Reply Last reply
                                      0
                                      • V Vikram A Punathambekar

                                        [EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]

                                        class Car
                                        {
                                        private int speed;
                                        // ...
                                        }

                                        If you have a class Car with an instance member speed, how do you refer to it in Car's methods?

                                        public void SpeedUp(int delta)
                                        {
                                        speed += delta;
                                        }

                                        or

                                        public void SpeedUp(int delta)
                                        {
                                        this.speed += delta;
                                        }

                                        I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.


                                        "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006

                                        M Offline
                                        M Offline
                                        Michael Dunn
                                        wrote on last edited by
                                        #28

                                        I'm lazy and haven't read the thread, but using "this." is just silly. Use "m_" and be done with it. ;) And before you gripe about Hungarian, think twice. How is "this." any different from a Hungarian prefix? You've just replaced a 2-character prefix with a 5-character prefix. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Laugh it up, fuzzball.

                                        S 1 Reply Last reply
                                        0
                                        • V Vikram A Punathambekar

                                          [EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]

                                          class Car
                                          {
                                          private int speed;
                                          // ...
                                          }

                                          If you have a class Car with an instance member speed, how do you refer to it in Car's methods?

                                          public void SpeedUp(int delta)
                                          {
                                          speed += delta;
                                          }

                                          or

                                          public void SpeedUp(int delta)
                                          {
                                          this.speed += delta;
                                          }

                                          I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.


                                          "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006

                                          M Offline
                                          M Offline
                                          Michael P Butler
                                          wrote on last edited by
                                          #29

                                          I had the bad habit of using a this prefix in my C# code, just for the intellisense. Now, I try and avoid it as it leads to sloppy coding. Michael CP Blog [^] Development Blog [^]

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups