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?
Some options I've tried so far...
Obj(int foo) : foo(foo) { }
void set_foo(int foo) { this->foo = foo; }
Obj(int _foo) : foo(_foo) { }
void set_foo(int _foo) { foo = _foo; }
Obj(int a_foo) : foo(a_foo) { } // a for "argument"
void set_foo(int a_foo) { foo = a_foo; }
Obj(int init_foo) : foo(init_foo) { }
void set_foo(int new_foo) { foo = new_foo; }
I always go for a Param or Arg suffix but only when disambiguation is necessary.
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.
I'm going with
in my programs. For copy constructors and operator=, i tend to call it
For operators, i'm going with
Because those are the left hand side and the right hand side of it.
I follow the Google C++ Style Guide
For classes:
For structs:
Setter:
I'm with litb on the use of
lhs
andrhs
for operator calls.I use
camelCase
for class member functions, andnames_with_underscores
for struct fields and methods.I always do this:
I used to play games with appending funny symbols until one time I got hit by this:
Can you see the mistake? (Yes, b_ is a private member reference variable). It compiled without a warning. Cost me 3 days of debugging (I was a green programmer then).
Now I always use the same name to avoid typos (and subsequent crashes) like that. There is no ambiguity inside the initialization list. This part of the language was designed just for this case, so take advantage of it.