Naming convention for params of ctors and setters

前端 未结 12 1820
轻奢々
轻奢々 2021-01-05 00:42

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?

相关标签:
12条回答
  • 2021-01-05 01:29

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

    0 讨论(0)
  • 2021-01-05 01:31

    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.

    0 讨论(0)
  • 2021-01-05 01:34

    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.

    0 讨论(0)
  • 2021-01-05 01:36

    I'm using foo_, it's better than _foo since it won't conflict with implementation specific function names and keywords.

    0 讨论(0)
  • 2021-01-05 01:36

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

    0 讨论(0)
  • 2021-01-05 01:40

    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.

    0 讨论(0)
提交回复
热议问题