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. To "m_" or not to "m_"?

To "m_" or not to "m_"?

Scheduled Pinned Locked Moved The Lounge
csharpc++tutorialquestiondiscussion
33 Posts 24 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 Vagif Abilov

    In case you write code in C#, do you use prefix class member variables with "m_"? I inherited this habit from C++, and in the beginning I used it also in C#. However, later I decided that it is better to use built-in language features rather, so I started using "this.". Example: (old habit) string m_customerName; ... m_customerName = "Bill"; (new habit) string customerName; ... this.customerName = "Bill"; I find it more natural to C# and corresponding to .NET naming convention guidelines. I also find prefix "m_" a little Hungurian :-) However, Juval Lowy in his excellent C# coding guidelines (from his book "Programming .NET Components) suggests always prefix private member variables with "m_". My colleague justified this with the following arguments: When you don't use prefix with memeber variables, you have more changes to mix three different things: method argument name, member variable and property. Properties are often named like variables but have capitalized first letter. When using Intellisence, it is easy to confuse property with the variable. I find this arguments to be practical, probably sufficient to start using "m_" again. But what do you think? Вагиф Абилов MCP (Visual C++) Oslo, Norway If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them. Jack Handey.

    V Offline
    V Offline
    vincent reynolds 0
    wrote on last edited by
    #22

    Nothing hungarian about the "m_" prefix, other than the mere fact that it is a prefix. Hungarian notation denotes logical type (not storage type, however), whereas the "m_" prefix denotes scope. I have for years used "m_" for module-scope variables, and "p_" for parameters. Using "this." for class member variables would certainly serve the same purpose as "m_", but, as others have stated, relies upon the discipline of the programmer to enforce, a practice that, in my experience, is nearly always a mistake.

    1 Reply Last reply
    0
    • K krism42

      I always used to use "m_fooname," but at my new employer the coding standard says just "_variableName", no M. So, I've switched to that, even for my personal stuff.

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #23

      But that violates the C++ standard. You can't have identifiers starting with '_'. Those are reserved. Tim Smith I'm going to patent thought. I have yet to see any prior art.

      R 1 Reply Last reply
      0
      • V Vagif Abilov

        In case you write code in C#, do you use prefix class member variables with "m_"? I inherited this habit from C++, and in the beginning I used it also in C#. However, later I decided that it is better to use built-in language features rather, so I started using "this.". Example: (old habit) string m_customerName; ... m_customerName = "Bill"; (new habit) string customerName; ... this.customerName = "Bill"; I find it more natural to C# and corresponding to .NET naming convention guidelines. I also find prefix "m_" a little Hungurian :-) However, Juval Lowy in his excellent C# coding guidelines (from his book "Programming .NET Components) suggests always prefix private member variables with "m_". My colleague justified this with the following arguments: When you don't use prefix with memeber variables, you have more changes to mix three different things: method argument name, member variable and property. Properties are often named like variables but have capitalized first letter. When using Intellisence, it is easy to confuse property with the variable. I find this arguments to be practical, probably sufficient to start using "m_" again. But what do you think? Вагиф Абилов MCP (Visual C++) Oslo, Norway If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them. Jack Handey.

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #24

        Just don't do it. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        1 Reply Last reply
        0
        • R Ryan Roberts

          *shudder* I cant abide the MyCutiePieLittleObject style. Are you a VB programmer? :P Personaly I keep the manly underscore for private fields and ditch the hungarian. Ryan

          O fools, awake! The rites you sacred hold Are but a cheat contrived by men of old, Who lusted after wealth and gained their lust And died in baseness—and their law is dust. al-Ma'arri (973-1057)

          G Offline
          G Offline
          Giles
          wrote on last edited by
          #25

          Yep, I find myself using just the _ for private stuff. Handy thing is, all the privates are then grouped together in the IDE's suggestions as well. No confusing something with Something as well when my eyes are tired.


          "Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table. Shameless Plug - Distributed Database Transactions in .NET using COM+

          1 Reply Last reply
          0
          • T Tim Smith

            But that violates the C++ standard. You can't have identifiers starting with '_'. Those are reserved. Tim Smith I'm going to patent thought. I have yet to see any prior art.

            R Offline
            R Offline
            Ryan Roberts
            wrote on last edited by
            #26

            Thought that was "__" not "_"? Ryan

            O fools, awake! The rites you sacred hold Are but a cheat contrived by men of old, Who lusted after wealth and gained their lust And died in baseness—and their law is dust. al-Ma'arri (973-1057)

            T 1 Reply Last reply
            0
            • R Ryan Roberts

              Thought that was "__" not "_"? Ryan

              O fools, awake! The rites you sacred hold Are but a cheat contrived by men of old, Who lusted after wealth and gained their lust And died in baseness—and their law is dust. al-Ma'arri (973-1057)

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #27

              __ can not appear anywhere in an identifier. _ can not be the first character of an identifier. I don't have the specific rules, but those cases are reserved for the compiler vendor and the language. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              1 Reply Last reply
              0
              • V Vagif Abilov

                In case you write code in C#, do you use prefix class member variables with "m_"? I inherited this habit from C++, and in the beginning I used it also in C#. However, later I decided that it is better to use built-in language features rather, so I started using "this.". Example: (old habit) string m_customerName; ... m_customerName = "Bill"; (new habit) string customerName; ... this.customerName = "Bill"; I find it more natural to C# and corresponding to .NET naming convention guidelines. I also find prefix "m_" a little Hungurian :-) However, Juval Lowy in his excellent C# coding guidelines (from his book "Programming .NET Components) suggests always prefix private member variables with "m_". My colleague justified this with the following arguments: When you don't use prefix with memeber variables, you have more changes to mix three different things: method argument name, member variable and property. Properties are often named like variables but have capitalized first letter. When using Intellisence, it is easy to confuse property with the variable. I find this arguments to be practical, probably sufficient to start using "m_" again. But what do you think? Вагиф Абилов MCP (Visual C++) Oslo, Norway If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them. Jack Handey.

                J Offline
                J Offline
                Joe Woodbury
                wrote on last edited by
                #28

                I agree with your original reasoning and did exactly the same thing when I moved to C#. One problem with "always prefix[ing] private member variables with "m_"" is that they may not always be private. If you change the member variable to protected, you now have to refactor your code. (This is essentially the same reason I've always opposed Hungarian notation in C++ for anything but "p" for pointers [knowing something is a pointer is rather critical in C++].) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                1 Reply Last reply
                0
                • X Xiangyang Liu

                  Colin Angus Mackay wrote:

                  I think these areguments are valid. However, in C# is isn't so common to use "m_". The coding standards at my work don't allow it so I don't use it any more.

                  Someone (not the boss) at work tried to start a coding standard that disallow "m_", I kept using it in my code, they gave up once they realized how much existing code already had "m_". ;) The main problem, I think, is that if you are looking at someone else's code that does not use "m_", it can be very hard to determine which variables are member variables, especially when the method is long and there is a long chain of inheritance for the class.[

                  My articles and software tools

                  ](http://mysite.verizon.net/XiangYangL/index.htm)

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

                  Xiangyang Liu wrote:

                  especially when the method is long

                  Sounds like it is time to refactor if the method is long.


                  My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                  X 1 Reply Last reply
                  0
                  • V Vagif Abilov

                    In case you write code in C#, do you use prefix class member variables with "m_"? I inherited this habit from C++, and in the beginning I used it also in C#. However, later I decided that it is better to use built-in language features rather, so I started using "this.". Example: (old habit) string m_customerName; ... m_customerName = "Bill"; (new habit) string customerName; ... this.customerName = "Bill"; I find it more natural to C# and corresponding to .NET naming convention guidelines. I also find prefix "m_" a little Hungurian :-) However, Juval Lowy in his excellent C# coding guidelines (from his book "Programming .NET Components) suggests always prefix private member variables with "m_". My colleague justified this with the following arguments: When you don't use prefix with memeber variables, you have more changes to mix three different things: method argument name, member variable and property. Properties are often named like variables but have capitalized first letter. When using Intellisence, it is easy to confuse property with the variable. I find this arguments to be practical, probably sufficient to start using "m_" again. But what do you think? Вагиф Абилов MCP (Visual C++) Oslo, Norway If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them. Jack Handey.

                    O Offline
                    O Offline
                    ogrig
                    wrote on last edited by
                    #30

                    have a look on Joel on Software for what the Hungarian notation really is, why and how to use it: Making Wrong Code Look Wrong [^] OGR

                    1 Reply Last reply
                    0
                    • L L_u_r_k_e_r

                      Alsvha wrote:

                      We also signify the type of the variable by a 3 letter abbriviation such as for exampel mStrSomeName.

                      Isn't that adding a lot of work when you change types for the variable.

                      A Offline
                      A Offline
                      Alsvha
                      wrote on last edited by
                      #31

                      It is adding some additional work, yes, but not much. But if you change the type definition first, it is easy to find the remainder usages of this variable, and with some copy/past work it is quickly done. The benefit when reading the code is increased in my personal opinion. --------------------------- 127.0.0.1 - Sweet 127.0.0.1

                      1 Reply Last reply
                      0
                      • C Colin Angus Mackay

                        Xiangyang Liu wrote:

                        especially when the method is long

                        Sounds like it is time to refactor if the method is long.


                        My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                        X Offline
                        X Offline
                        Xiangyang Liu
                        wrote on last edited by
                        #32

                        Colin Angus Mackay wrote:

                        Sounds like it is time to refactor if the method is long.

                        And using "m_" would make refactoring of a long method easier. :)[

                        My articles and software tools

                        ](http://mysite.verizon.net/XiangYangL/index.htm)

                        1 Reply Last reply
                        0
                        • V Vagif Abilov

                          In case you write code in C#, do you use prefix class member variables with "m_"? I inherited this habit from C++, and in the beginning I used it also in C#. However, later I decided that it is better to use built-in language features rather, so I started using "this.". Example: (old habit) string m_customerName; ... m_customerName = "Bill"; (new habit) string customerName; ... this.customerName = "Bill"; I find it more natural to C# and corresponding to .NET naming convention guidelines. I also find prefix "m_" a little Hungurian :-) However, Juval Lowy in his excellent C# coding guidelines (from his book "Programming .NET Components) suggests always prefix private member variables with "m_". My colleague justified this with the following arguments: When you don't use prefix with memeber variables, you have more changes to mix three different things: method argument name, member variable and property. Properties are often named like variables but have capitalized first letter. When using Intellisence, it is easy to confuse property with the variable. I find this arguments to be practical, probably sufficient to start using "m_" again. But what do you think? Вагиф Абилов MCP (Visual C++) Oslo, Norway If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them. Jack Handey.

                          R Offline
                          R Offline
                          Rocky Moore
                          wrote on last edited by
                          #33

                          Everyone knows the "_" underscore died back in Assembler, time to move on ;) "this." works perfectly fine when needed! Guess that is why they put it in ;) Rocky <>< Latest Post: SQL Server 2005, Major Enhancements! Blog: www.RockyMoore.com/TheCoder/[^]

                          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