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.
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.
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".
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.
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.
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
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.
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
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).
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
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 02:09 PM.
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
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.
Bookmarks