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

Started by
22 comments, last by freeworld 12 years, 7 months ago
Just curious
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
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.

throw table_exception("(? ???)? ? ???");

Bleh, prefixes.

If the standards of my group dictate them, I'll argue against and then conform. Otherwise they're a waste.
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

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Personally I use no decoration for public member variables, trailing underscore for private, and ban protected.
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..
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.

throw table_exception("(? ???)? ? ???");


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.

throw table_exception("(? ???)? ? ???");


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.
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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement