Why start variable names with _?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Why start variable names with _?

  1. #1
    Join Date
    Oct 2009
    Posts
    56

    Why start variable names with _?

    I am looking at some C++ code that has many variables that start with underscores. Why? What's the point? I know in Python its a way to keep them private, but I don't understand why we would do that in C++ when we can just make something private. So can someone please explain what the point of it is? Thanks for any help.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Why start variable names with _?

    Style, nothing more. The idea is that the name instantly tells you something about the variable.

    Some people start private members with _ (or, more commonly, m_ for "my"). Some people start function parameters with _. It doesn't serve any useful purpose except to the person maintaining the code.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: Why start variable names with _?

    Quote Originally Posted by Lindley View Post
    Style, nothing more. The idea is that the name instantly tells you something about the variable.

    Some people start private members with _ (or, more commonly, m_ for "my"). Some people start function parameters with _. It doesn't serve any useful purpose except to the person maintaining the code.
    I start class members with m_, but when I do it, m_ stands for "member".

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Why start variable names with _?

    Didn't we recently have a thread about how you are NOT supposed to do that because it should be reserved for library implementation?

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: Why start variable names with _?

    Quote Originally Posted by ninja9578 View Post
    Didn't we recently have a thread about how you are NOT supposed to do that because it should be reserved for library implementation?
    We always have threads about this.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Why start variable names with _?

    Quote Originally Posted by ninja9578 View Post
    Didn't we recently have a thread about how you are NOT supposed to do that because it should be reserved for library implementation?
    Technically that's only true in the global namespace. In general, _(capital letter) is reserved everywhere, and double __ is reserved everywhere.

    But yes, in general it's a good idea not to use a leading underscore. In those cases where I do use underscores, I tend to make them trailing rather than leading.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: Why start variable names with _?

    Quote Originally Posted by Lindley
    In those cases where I do use underscores, I tend to make them trailing rather than leading.
    However, if you copy Lindley, don't use double trailing underscores, because the rule concerning double (consecutive) underscores applies regardless of their position.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: Why start variable names with _?

    I used to use _+(lowercase) because while seemingly dangerous, it is legal.

    Then one day, I got tired of people saying I couldn't do it, and having to argue with them, and having the same discussion over and over. Now I just go with gcc's convention of m_. Now nobody bothers me anymore, and my code is just as clear.

    Now if only people would stop bothering me when I use gotos.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    Re: Why start variable names with _?

    I don't use m_ or anything similar anymore. After all, most things should be members anyway so if something should be marked in a special way it's things that are not members.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Why start variable names with _?

    Quote Originally Posted by S_M_A View Post
    I don't use m_ or anything similar anymore. After all, most things should be members anyway so if something should be marked in a special way it's things that are not members.
    I would say that depends. Most of the variables I use are temporary, thus local to the function I'm writing (i.e. not members).

    Viggy

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    Re: Why start variable names with _?

    True but then (in my opinion) the method or section of code where the locals are used should be small enough to not create any doubts on if the vars are members or not.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  12. #12
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Why start variable names with _?

    The two primary reasons for the (m_/_)-convention are, IMO & IIRC, the need to (1) quickly distinguish private class member variables from local or global variables while reading code, and more importantly, to (2) prevent name collisions when you have member functions with one or more parameters named the same as some of the member variables.

    And this convention has as many pros as it has cons.
    I personally tend to use m_ out of habit, and also because variables starting with _ just "pokes" my eyes.

    However, those arguing against this practice say that (1), most IDE's today will provide ways for you to distinguish member variables from other types of variables, for example, just by hovering your mouse over, and (2) there are comparatively few cases where you need, due to name collisions, to type in the extra "this.", as opposed to typing "m_" or "_" every single time.

    As for (2), can't really argue with that, except that some people like all their member variables clumped together when shown in tools like IntelliSense.
    As for (1): what if you're reading code snippets, on a forum, for example?

    But in the end, it's a matter of personal style and preference.
    Last edited by TheGreatCthulhu; May 2nd, 2012 at 03:09 PM.

  13. #13
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    Re: Why start variable names with _?

    All methods have their pros and cons and I can't say I have a problem with either one. Meaningful names on variables and methods is by far the most important thing for me. If they are camel cased, m_ or follow any other rule doesn't help a thing if they doesn't say anything.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  14. #14
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Why start variable names with _?

    True.

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: Why start variable names with _?

    Quote Originally Posted by S_M_A View Post
    I don't use m_ or anything similar anymore. After all, most things should be members anyway so if something should be marked in a special way it's things that are not members.
    "Most things are members" What do you develop on? If you have "small function building blocks", I tend to find that 50% of my variables are function arguments actually.

    ----
    IMO, a variable name should be enough to know what the variable is/represents, and I agree with that concept. However, I find that it is always a bit limited at telling you where the variable is from, and what its life cycle is.

    For example: list_of_names: Duh, its is an std::list of strings. But is it a member variable? an io argument?

    I have been using for the past 6 months:
    Code:
    m_ : member variable;
    i_ : function input variable;
    o_: function output variable;
    g_ : global/static variable
    a_ : stack variable
    I've found it to very useful in writing code without thinking to hard. I have also found the "acceptance factor" of people reading my code to be quite high.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center