For those of you who name you member variables with no special notation like m_foo
or foo_
, how do you name parameters to your ctors and setters?
I follow the Google C++ Style Guide
Variable names are all lowercase, with underscores between words. Class member variables have trailing underscores. For instance: my_exciting_local_variable, my_exciting_member_variable_.
I'm going with
Obj(int foo) : mFoo(foo) { }
void setFoo(int foo) { mFoo = foo; }
in my programs. For copy constructors and operator=, i tend to call it
Obj(Obj const& that):mFoo(that.mFoo) { }
For operators, i'm going with
Obj operator+(Obj const& lhs, Obj const& rhs) { ... }
Because those are the left hand side and the right hand side of it.
For classes:
Obj(int foo) : _foo(foo) {};
For structs:
obj_t(int foo_) : foo(foo_) {};
Setter:
Obj.setFoo(int foo) { _foo = foo; }
I'm with litb on the use of lhs
and rhs
for operator calls.
I use camelCase
for class member functions, and names_with_underscores
for struct fields and methods.
I'm using foo_, it's better than _foo since it won't conflict with implementation specific function names and keywords.
I do it like this:
obj(int foo) : _foo(foo) { }
int foo() const { return _foo; }
void foo(int value) { _foo = value; }
The only trick here is to make sure that the letter following the underscore is lowercase. However, I avoid uppercase in identifier names everywhere, as it is inconsistent with conventions used by the standard library (which uses foo_bar_baz
for all identifiers).
Number two has problems as a convention, although in your case it could be harmless. A name that has a leading underscore followed by an uppercase character is reserved for the implementation, and all names with leading underscores are reserved in a global context. If you never have class members beginning with uppercase letters (I don't), you're safe with the convention as shown (using _foo only as a function argument), but I dislike naming conventions that skirt anywhere near the limits.