-
May 3rd, 2012, 08:22 PM
#16
Re: Why start variable names with _?
Originally Posted by TheGreatCthulhu
I personally tend to use m_ out of habit, and also because variables starting with _ just "pokes" my eyes.
I definitely do share this feeling.
And just to contribute my own $.02...
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 [...]
IDEs may do, online platforms where code is published, like CG, for instance, AFAICT usually don't. Neither do books, at least as long as they're made out of ordinary paper.
[...] (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.
Though distinguishing between locals (and parameters) and members of the same name perhaps IMO is the only legitimate reason to explicitly dereference this, I still simply don't like it. Simply looks too javaish to me and also seems to be somewhat common in C#. Personal taste, admittedly.
Personally, I use m_ for class members and s_ for statics, together with hungarian notation. I experimented with decorating parameters but dropped that since I found it too cluttering together with hungarian notation. For locals and parameters I use hungarian notation without any further decoration except when they're static. (And except for some really obvious things like for loop variables which often simply are named i or something. Some sort of "hungarian only" notation.)
And for completeness, I use Pascal case for methods (and .NET properties, at least most of the time).
Last edited by Eri523; May 3rd, 2012 at 10:53 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
May 3rd, 2012, 08:48 PM
#17
Re: Why start variable names with _?
Originally Posted by monarch_dodra
I have been using for the past 6 months:
The o_ prefix for a function's out parameter sounds interesting to me. However, is there really a point for the i_ and a_ prefixes when they are for local variables that should be easily determined by the declaration context?
Originally Posted by Eri523
Personally, I use m_ for class members and s_ for statics, together with hungarian notation. I experiented with decorating parameters but dropped that since I found it too cluttering together with hungarian notation. For locals and parameters I use hungarian notation without any further decoration except when they're static. (And except for some really obvious things like for loop variables which often simply are named i or something. Some sort of "hungarian only" notation.)
I'm allergic to Hungarian notation, except for mild forms of Apps Hungarian.
-
May 4th, 2012, 04:29 AM
#18
Re: Why start variable names with _?
Originally Posted by laserlight
The o_ prefix for a function's out parameter sounds interesting to me. However, is there really a point for the i_ and a_ prefixes when they are for local variables that should be easily determined by the declaration context?
I'm allergic to Hungarian notation, except for mild forms of Apps Hungarian.
The use of the _i is mostly for symmetry with _o. Also, it is to give the option of writing _o or _io (eg pure output, or input/output).
What I have found that is also helpful with a_ (and _i), is that it allows to name your variable the same as your class. This avoids having to think too hard about the names, and I find it actually makes the naming better. If your object is a string and nothing more, then why not call it a string?
Code:
void to_upper_case(const string& i_string, string& o_string);
Code:
std::vector<int> a_vector;
When I have local variables with explicit names though, I don't prefix with a_:
Code:
std::string tommy = "Tommy"
If find the problem with Hungarian is that it was invented for C, and then leaked into C++.
Is your question related to IO?
Read this C++ FAQ 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.
-
May 4th, 2012, 04:35 AM
#19
Re: Why start variable names with _?
Originally Posted by monarch_dodra
If your object is a string and nothing more, then why not call it a string?
Yeah, I can see where you are coming from especially with generic code. That said, in your specific first example, why not:
Code:
void to_upper_case(const string& subject, string& result);
-
May 4th, 2012, 05:02 AM
#20
Re: Why start variable names with _?
Originally Posted by laserlight
Yeah, I can see where you are coming from especially with generic code. That said, in your specific first example, why not:
Code:
void to_upper_case(const string& subject, string& result);
Well, mostly because I was trying to illustrate my convention
You should notice though, that with your names, you have basically lost the info that the variables are strings, and only retained their i_ and o_ property. I could mostly trim your example down to my scheme, and write it as:
Code:
void to_upper_case(const string& i_, string& o_);
Just sayin'...
Is your question related to IO?
Read this C++ FAQ 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.
-
May 4th, 2012, 05:02 AM
#21
Re: Why start variable names with _?
Originally Posted by monarch_dodra
If your object is a string and nothing more, then why not call it a string?
because you're spreading redundant informations already evident from the variable type ... it's like those comments stating the obvious. At best, they serve no purpose; at worst, they make mantainance harder ( in general, what happens if you change the type ? you'll need to change the variable name too ... ). IMHO, the purpose of that function is immediately evident from the function name and signature alone.
-
May 4th, 2012, 05:24 AM
#22
Re: Why start variable names with _?
Originally Posted by superbonzo
because you're spreading redundant informations already evident from the variable type ... it's like those comments stating the obvious. At best, they serve no purpose; at worst, they make mantainance harder ( in general, what happens if you change the type ? you'll need to change the variable name too ... ). IMHO, the purpose of that function is immediately evident from the function name and signature alone.
Of course the purpose of the function is obvious, but you still have to give the arguments names. Maybe the "string" suffix is indeed not the best choice (and I will take it into account in the future), but that's the best I could come up with in the 5 seconds I gave myself to chose a variable name.
As for the i & o, I have (personally) found that they help. Even if you can ascertain this info from the signature, having the prefix means I don't have to re-read the signature to remember which is which.
EDIT: From signature alone though, it is not possible to distinguish an input-output (eg, a vector you append to), from an output only (a vector you clear before writing to).
Last edited by monarch_dodra; May 4th, 2012 at 05:26 AM.
Is your question related to IO?
Read this C++ FAQ 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|