Poll: What to name constructor variables

Started by
47 comments, last by pex22 19 years, 7 months ago
Quote:Original post by JohnBolton
I haven't decided how to prefix variables declared in a namespace.


I'd say n_, but that's quite close to m_. How about ns_? Should be clear enough.
Advertisement
Quote:Original post by JohnBolton
I haven't decided how to prefix variables declared in a namespace.
With the namespace name followed by the scope resolution operator?
In C++ you shouldn't prefix anything with an underscore (ex. _var). Postfixing an underscore is fine (ex. var_).
Quote:Original post by Russell
In C++ you shouldn't prefix anything with an underscore (ex. _var). Postfixing an underscore is fine (ex. var_).

Prefixing something with only an underscore implies that its compiler specific (for those who were going to say "why not" :P), but so long as its clear, I don't see a paroblem with it. For something as simple as a constructor, it should be fine.#

Quote:Original post by Sneftel
Quote:Original post by JohnBolton
I haven't decided how to prefix variables declared in a namespace.
With the namespace name followed by the scope resolution operator?

Rofl
So, what about prefixing private methods?

m_DoSomething( radius );
Quote:Original post by Machinoid
So, what about prefixing private methods?

m_DoSomething( radius );

I don't tend to do that, because to me the m_ prefix implies a variable. I don't find that a prefix is nessecary for functions, although I'm sure others probably use prefixes.
Quote:Original post by Sneftel
C'mon, guys. Remember your C++. It's all about initializer lists.

*** Source Snippet Removed ***
Does that actually work? With the " : x(x), y(y)" initializer list thing going on? If so, yet another thing I've learned from this wonderful board...
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
m_ is a bit of Hungarian notation. There is a very divisive running argument regarding Hungarian notation and its relative merits and boons.

I prefer undecorated arguments so that autodocumenting programs can publish the parameter names without arg_ or in_ or any other decoration in the documentation.

Apart from that, please make your variable names pronouncable. This makes globbing them in my head easier and therefore I can retain more of the code in my head. I haven't seen this one published anywhere -it is my own invention and I am quite pleased with it. It is also why I am a naysayer to rote adherence to Hungarian.
Quote:Original post by Evil Steve
Quote:Original post by Russell
In C++ you shouldn't prefix anything with an underscore (ex. _var). Postfixing an underscore is fine (ex. var_).

Prefixing something with only an underscore implies that its compiler specific (for those who were going to say "why not" :P), but so long as its clear, I don't see a paroblem with it. For something as simple as a constructor, it should be fine.

Specifically, any name prefixed with an underscore is reserved for the implementation. As far as I understand, this means that your compiler may, for instance, use any name beginning with an underscore for, say, some horrible macro—though I am no expert. For this reason (or for fear of this being true), I avoid prefixing anything with underscores.
Quote:Original post by Agony
Does that actually work? With the " : x(x), y(y)" initializer list thing going on?
Yes it does. Since initializer lists only initialize member variables, those are the only variables in scope when resolving the target to be initialized. However, the argument to each constructor follows normal resolution rules, so local variables take precedence over member variables.

This topic is closed to new replies.

Advertisement