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.
  • 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
                      • G Gizzo

                        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 Offline
                        C Offline
                        Chris Losinger
                        wrote on last edited by
                        #30

                        Gizzo wrote:

                        What's wrong with it?

                        it's not cool any more. the smart people have decreed that we all need to use a different kind of notation now. Cleek | Image Toolkits | Thumbnail maker

                        G 1 Reply Last reply
                        0
                        • G Gizzo

                          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.

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

                          Gizzo wrote:

                          What's wrong with it?

                          It spent too long as a fad, which means lots of people think they know how to use it while in reality having no clue, most of them not even grasping the problem it was intended to solve. Sorta like "object-oriented programming"...

                          ---- 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

                          G 1 Reply Last reply
                          0
                          • M Michael Dunn

                            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 Offline
                            S Offline
                            Shog9 0
                            wrote on last edited by
                            #32

                            Either way gets the job done, so long as everyone understands *why* it's being done and does it the same way. I still shudder every time i see someone prefixing a local variable with m_ in a sad attempt to get copy-paste code working... :sigh:

                            ---- 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
                            • C Chris Losinger

                              Gizzo wrote:

                              What's wrong with it?

                              it's not cool any more. the smart people have decreed that we all need to use a different kind of notation now. Cleek | Image Toolkits | Thumbnail maker

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

                              Chris Losinger wrote:

                              the smart people have decreed that we all need to use a different kind of notation now.

                              the smart people? and what did they decide it's better? I'm not been ironic, i just want to know.

                              C 1 Reply Last reply
                              0
                              • S Shog9 0

                                Gizzo wrote:

                                What's wrong with it?

                                It spent too long as a fad, which means lots of people think they know how to use it while in reality having no clue, most of them not even grasping the problem it was intended to solve. Sorta like "object-oriented programming"...

                                ---- 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

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

                                so... I'm courious, which notation do you use now? Anyway, to use or not to use the hungarian notation it's not my decision. I (we) have to do it to mantain the code consistent. But i like it

                                1 Reply Last reply
                                0
                                • G Gizzo

                                  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.

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #35

                                  Its crap. The hungarian notatin is a pile of shit that make windows documentation a joke. eg from MSDN: LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window WM_COMMAND, // the message to send WPARAM wParam, // notification code and identifier LPARAM lParam // handle to control (HWND) ); why? it is 32 bit. Why not dwParam. etc etc etc. Hungarian notation is an unmaintainable pile of crap. Nunc est bibendum

                                  G 1 Reply Last reply
                                  0
                                  • L Lost User

                                    Its crap. The hungarian notatin is a pile of shit that make windows documentation a joke. eg from MSDN: LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window WM_COMMAND, // the message to send WPARAM wParam, // notification code and identifier LPARAM lParam // handle to control (HWND) ); why? it is 32 bit. Why not dwParam. etc etc etc. Hungarian notation is an unmaintainable pile of crap. Nunc est bibendum

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

                                    But do you agree with me that is necesary to use some kind of notation? Overall if you are working in a team. When I have to revise some old code I feel lost trying to guest the types and everything...Imagine you wouldn't have Intellisense...

                                    L 1 Reply Last reply
                                    0
                                    • G Gizzo

                                      But do you agree with me that is necesary to use some kind of notation? Overall if you are working in a team. When I have to revise some old code I feel lost trying to guest the types and everything...Imagine you wouldn't have Intellisense...

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #37

                                      No, I dont. My notation differentiates between pointers, non pointers, and between global, local and member variables. p for pointer, g for global, m for member and that is it. Any distinction between data types is in the naming, so, for example, I might use NumberOfBytesWritten++; If it aint big enough I will make it unsigned, 32 but, etc. If I am using a string or an array, I will like wies use a meaningful name. BTW, if variable names are full descriptive names (and the same for functions too) then code commenting is less important. And I have had my code maintained very effectively by other coder who find it very easy to follow and read. Nunc est bibendum

                                      G 1 Reply Last reply
                                      0
                                      • L Lost User

                                        No, I dont. My notation differentiates between pointers, non pointers, and between global, local and member variables. p for pointer, g for global, m for member and that is it. Any distinction between data types is in the naming, so, for example, I might use NumberOfBytesWritten++; If it aint big enough I will make it unsigned, 32 but, etc. If I am using a string or an array, I will like wies use a meaningful name. BTW, if variable names are full descriptive names (and the same for functions too) then code commenting is less important. And I have had my code maintained very effectively by other coder who find it very easy to follow and read. Nunc est bibendum

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

                                        fat_boy wrote:

                                        My notation differentiates between pointers, non pointers, and between global, local and member variables.p for pointer, g for global, m for member and that is it.

                                        :doh: That's a notation itself. And it's derived from hungarian... I think.

                                        L 1 Reply Last reply
                                        0
                                        • G Gizzo

                                          fat_boy wrote:

                                          My notation differentiates between pointers, non pointers, and between global, local and member variables.p for pointer, g for global, m for member and that is it.

                                          :doh: That's a notation itself. And it's derived from hungarian... I think.

                                          L Offline
                                          L Offline
                                          Lost User
                                          wrote on last edited by
                                          #39

                                          doh: WTF? what is doh about it? I didnt say I dont use any notation, I just dont use the hungarian notation because it is crap. For the afore mentioned rasons. Nunc est bibendum

                                          G 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