Jump to content
  • Advertisement
Sign in to follow this  
Waaayoff

[C++] Which prefix do you use for member variables?

This topic is 2631 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

Advertisement
Just 'm' here, 'g' for the few globals that might pop in (I try to avoid them, obviously) and I add an 's' when either are static. Basically I use prefixes to denote scope, with locals and parameters having no prefix.

Share this post


Link to post
Share on other sites
Bleh, prefixes.

If the standards of my group dictate them, I'll argue against and then conform. Otherwise they're a waste.

Share this post


Link to post
Share on other sites
this-> where appropriate because it makes it explicit to anyone reading the code that yes, this is a member.

I despise g_ and m_ primarily on account of the fact that they make autocomplete lists look like a wall of too-similarly-named variables. Combine them with hungarian notation and you may have to jump up to 6 or so characters into the variable name before you get to the meaningful stuff that uniquely identifies it. That's a pretty damn poor signal-to-noise ratio in my book. Only a business manager type who's never actually sat down and written code using this stuff could truly love it.

dvhdgm.png

Share this post


Link to post
Share on other sites
Personally I use no decoration for public member variables, trailing underscore for private, and ban protected.

Share this post


Link to post
Share on other sites
I don't like prefix, neither in globals (and I avoid global non-const variables). I use "this" when necessary or when it makes the code easier to read..

Share this post


Link to post
Share on other sites
Personally, I find *very* minimal prefixes like I use to increase "at a glance" readability. prefixes which denote scope provide information that intellisense doesn't, unlike wrongly-used hungarian type prefixes -- though correctly-used, hungarian semantic prefixes actually can increase readability, but I don't use them myself.

Using either scope-based prefixes or proper, semantic Hungarian actually can aid intellisense if used consistently -- pressing 'm' delivers a list of all members that are in scope for example, or 'km' might deliver a list of variables which are measured in kilometers in a semantic Hungarian, as another.


I want to like the idea of using "this->" however its 5 extra (relatively common) keystrokes, and also the compiler will do nothing to enforce consistent usage, and neglecting it can make unintentional aliasing possible (however uncommon).

Basically, if you choose to use prefixes, don't duplicate information that your IDE will already give you with a mouse-over. If your prefixes, whatever they may be, don't add real value to understanding, then they're just wasted keystrokes.

Share this post


Link to post
Share on other sites

Personally I use no decoration for public member variables, trailing underscore for private, and ban protected.


Also, +this

publics should not be decorated, because they are basically hoisted into the enclosing scope, and no longer "member"s in the sense of having limited scope.

Share this post


Link to post
Share on other sites

Personally I use no decoration for public member variables, trailing underscore for private, and ban protected.

+1

The trailing underscore doesn't mess with autocomplete, barely affects the look of your code, and provides important information at a glance.

You didn't vote, though! We need to win this.


Using either scope-based prefixes or proper, semantic Hungarian actually can aid intellisense if used consistently -- pressing 'm' delivers a list of all members that are in scope for example, or 'km' might deliver a list of variables which are measured in kilometers in a semantic Hungarian, as another.

VAX can sort/filter/highlight members by scope without that. Though, it won't get you all members in kilometers.

Share this post


Link to post
Share on other sites
I use no any decoration except some internal namespace (start with _).
But I prefix EACH member access with this-> to indicate it's a member explicitly.
Not only C++, I keep same style in Java and other appropriate language also.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!