• Advertisement
Sign in to follow this  

Coding Style Question

This topic is 4493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering if it is a good idea to prefix private variables in a class, and global variables with something so that they are easily recognizable as such. I was thinking of using an underscore (_) before private variables in a class, and two underscores (__) before global variables, but I'm not sure how good of an idea this would be. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Assuming this is C++ you're talking about, using double underscore prefixes is a very bad idea. Identifiers with double underscores are reserved for use by the compiler implementation and can have very very bad consequences if you use them. Also, identifiers beginning with a signle underscore followed by a capital letter are also reserved for the compiler implmentation, so if you have a member variable names that begin with an underscore followed by a capital you can run into troubles. I prefer to use a single trailing underscore for private and protected member variables.

Share this post


Link to post
Share on other sites
Discussion of coding styles is a flame war. Everyone has their own opinion. The best I can say is "find what works for you, and use it."

Why wouldn't that be a good idea? Well, there isn't THAT much of a difference between "_" and "__", visually at least. So maybe something a little different, but if it works for you, then who cares? Code should be written for maximum readability.


EDIT: Except for the reasons SiCrane said, which I had no idea about. Oh well.

Share this post


Link to post
Share on other sites
Aahhh, that's right. I am talking about C++, and I though I had heard about those at one point. Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Assuming this is C++ you're talking about, using double underscore prefixes is a very bad idea. Identifiers with double underscores are reserved for use by the compiler implementation and can have very very bad consequences if you use them. Also, identifiers beginning with a signle underscore followed by a capital letter are also reserved for the compiler implmentation, so if you have a member variable names that begin with an underscore followed by a capital you can run into troubles. I prefer to use a single trailing underscore for private and protected member variables.

What SiCrane said. Also, some libraries reserve other capitalized terms as well. For instance, "ERR_" is a reserved prefix if you use libavl, so declaring your own identifier called "ERR_myErrorVariable" is inadvisable if you also use that library.

You seem to want to differentiate between global variables and others. Some people have adopted the convention of prefixing global variables with "g_", which is generally safe (in C++).

Hope that helps,
-- k2

Share this post


Link to post
Share on other sites
I prefix all member variabels of a class with m_VariableName. The same for any global variables I use, g_VariableName. Use what works best for you but stay consistent!

Share this post


Link to post
Share on other sites
Heh, even better, make your 'global' variables only available from the compilation unit they are declared in. If it needs to be more accessable then that, think hard on your design, to see if it can't be improved. Typically, I would declare a variable in the .cpp file in question, in an Internal namespace. So far, That has been global enough for me, although if I were going to make global variables that needed to be accessable from anywhere, I'd probably stick them in a Global namespace. Namespaces are man's best friend [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement