Naming convention for params of ctors and setters

前端 未结 12 1890
轻奢々
轻奢々 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:22

    I avoid (by avoid mean never use) underscore as the first character of any identifier. I know its overkill but worth the effort.

    Read this: What are the rules about using an underscore in a C++ identifier?

    Though not a rule I limit the use of underscore and prefer camel case to make my variables readable. But that's just a personal preference and I don't mind reading code that uses it.

    Additionally I never name parameters the same as my member variables. The compiler will not help you catch the kind of errors this can generate (and this is all about getting the compiler to do the real work so you can do the expressive work the compiler can't do).

    int X::setWork(int work)
    {
        this->work = work;  // OK. But more Java like.
    
        work = work;        // Compiler won't see that problem.
    }
    
    int X::setWork(int newWork)
    {
        work = newWork;    // A bit harder to get wrong.
    }
    

提交回复
热议问题