I'm programming C++ using the underscore naming style (as opposed to camel case) which is also used by the STL and boost. However, since both types and variables/functions are named all lower case, a member variable declaration as follows will lead to compiler errors (or at least trouble):
position position;
A member variable named position which is of type position. I don't know how else to name it: It's generally a position, but it is also the position of the object. In camel case, this would be fine with the compiler:
Position position;
But in C++ it causes problems. I don't want to switch to camel case, use Hungarian notation or add a trailing underscore because of that, so I'm wondering: Is it a good practice to name a member like a type anyways?
In C, it is pretty common to use cryptic one-letter variables for this:
int i;
But I find that a bit, well, cryptic:
position p;
Are there any rules of thumb for variable naming I can use to avoid this?
There are more examples in my code if you need something to work on:
mouse_over(entity entity) // Returns true if the mouse is over the entity
manager &manager; // A reference to the framework's manager
audio audio; // The audio subsystem
Edit:
I was curious to see if Bjarne Stroustrup himself has something to say on this issue. Apparently, he hasn't, but he suggests coding conventions that would work around my compiler problems:
For example, capitalize nonstandard library user-defined types and start nontypes with a lowercase letter
That'd be consistent with STL and boost, so I might use that. However, most of you agree that this naming should be avoided whether it compiles or not. So does Stroustrup:
it is unwise to choose names that differ only by capitalization.
Go variable naming rules: A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ )
Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name.
A good variable name should: Be clear and concise. Be written in English. A general coding practice is to write code with variable names in English, as that is the most likely common language between programmers.
The local meaning is rarely a good unique global description of the type:
cartesian_point_2d position; // rectangular, not polar coordinates
mouse_over(ui_entity entity); // not a business layer entity
xyz_manager& manager; // what's a manager without something to manage?
audio_system audio;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With