Do I have to break this java habit when using C++?

Started by
45 comments, last by Zahlman 18 years, 5 months ago
Quote:Original post by Shannon Barber
Quote:Original post by scgrn
Why exactly is this a bad habit? I do this all the time and see no problems with it.


I use it for ctor's in particular. vector(x,y,z):x(x),y(y),z(z){} makes sense to me.


/Sign

For non constructors, I tend to prefix the argument name. E.g.:

std::vector::resize( size_t new_size )
Advertisement
Quote:Original post by Promit
Scope based prefixing, however, is an entirely different matter, however, and remains in the .NET Naming Guidelines. Specifically, private and protected members of a class should be prefixed with a single underscore, e.g. int _privateData;.

Can you point out where in the guidelines it says that?

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Quote:Original post by Arild Fines
Quote:Original post by Promit
Scope based prefixing, however, is an entirely different matter, however, and remains in the .NET Naming Guidelines. Specifically, private and protected members of a class should be prefixed with a single underscore, e.g. int _privateData;.

Can you point out where in the guidelines it says that?

No, and actually on a second pass I might be mistaken. This page mentions it (and muer or Washu or somebody told me), but it sounds like they made some modifications to the original guidelines. I can't find any guidelines at all in the .NET docs for naming member fields.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
I hate having double names (this.d verses d), and I hate initializer lists. *quickly yanks neck back in*
Quote:Original post by evanofsky
and I hate initializer lists.

Hate or not, they're the most efficient way of initializing variables (as if it matters), and you will sometimes be forced to use initializer lists, like when initializing const members and references.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Promit
Quote:Original post by evanofsky
and I hate initializer lists.

... they're the most efficient way of initializing variables.


That plus the fact it's the only method of initialization since:

foo::foo() {  bar = ...;}


Is not initialization it is an assignment statement and assignment != initialization

*sigh*... okay, okay. I know when I'm beat.
Quote:Original post by Promit
Quote:Original post by eedok
isn't prefixing a bad thing?


Type based prefixing is a terrible thing. Scope based prefixing, however, is an entirely different matter, however, and remains in the .NET Naming Guidelines. Specifically, private and protected members of a class should be prefixed with a single underscore, e.g. int _privateData;. I use the same convention myself, although with m_ for largely historical (and now consistency) reasons.


Actually, prefixing with an underscare is a very bad idea, since many identifiers with leadings underscores are technically reserved for use by the implementions, as are any identifiers with double leading underscores.

What you'll see in modern C++ books a fair bit is training underscores. Also, the "Writing Unmaintainable Code" essay makes the very good point that m_ could just as easily mean method :P ( Not to mention that STL naming conventions use lowercase_with_underscores and no abbreviations )

Agreed, BTW, about type-based prefixing, at least in strongly-typed languages. Sense the compiler prevents you from modifying a const variable, for example, you don't need a c prefix. As for purpose-based prefixing, it's acceptable, although it's better if it's done with just a different type so that the compiler enforces it.

Oh, and you should be using a better variable name than "d" ;)
I like (ie., it gives my eyes an orgasm) to use trailing underscores for my private or protected data members, and to always initialize them by using initializer-lists. Such things so nice and fluffy.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Quote:Original post by me22
Oh, and you should be using a better variable name than "d" ;)

I thought that you only had to do that when the program actually did something?

This topic is closed to new replies.

Advertisement