Search code examples
c#c++variablesnaming-conventionsconventions

C++ Naming Conventions for C# Developers


C# has a tool called StyleCop written by Microsoft which gives you some default rules that I find very useful to end any bickering among teams about code style. The default rules state that all class variables must use the following syntax to refer to class variables:

this.classVariable  // C# Default StyleCop Naming Convention

The main alternative in C# is to use a leading underscore instead:

_classVariable      // C# Alternative StyleCop Naming Convention

C++ seems to have a large number of naming restrictions which rule out the leading underscore as this is reserved for libraries and operating systems. What about using the 'this' keyword like this, is this a common syntax?

this->classVariable // C++

I don't want this post to descend into a fight about style. I am not asking your opinion on coding style. I just want to know a short concise list of what the most common styles are and if the above example is used commonly, so I can pick one and move on.

UPDATE

I'm not sure why this question was put on hold. Specific questions were asked and we got one very good answer. No opinion was expressed anywhere. Admittedly, this is a touchy subject but I think all went well in the end.


Solution

  • There are 3 styles, as far as I know.

    First, use underscore:

    class Person
    {
    private:
        int _age;
        ...
    

    You're worried about naming restrictions, but don't worry - first underscore + non-uppercase on non-global names is not illegal.

    (first underscore + uppercase is illegal; you must not use names like this: _Age.)

    Second, use m_. (m means member.)

    class Person
    {
    private:
        int m_age;
        ...
    

    This style is used in MFC, and I also prefer this.

    Third, use underscore, on the last part of name.

    class Person
    {
    private:
        int age_;
        ...
    

    It isn't seen many times, but I've seen it before >o<


    Well, you can also name your all class variable normally and use this->.

    class Person
    {
    private:
        int age;
        ...
    public:
        void SetAge(int age) { this->age = age; }
        ...
    

    However, this isn't used widely; above all, it's so inconvenient. (Also I have hardly seen this.var style in C# except tool-generated source - is it really widely-used in C#??)

    As you see, this-> syntax is used to avoid such a ambiguous situation:

    void Person::SetAge(int age)
    {
        age = age; // what does it mean?
    }
    

    (In addition, in the situation like this, age means the parameter; because it's more local than Person::age, so the local age hides Person::age.)

    But it isn't used widely as I said. In this situation, It's much better to edit the name of one of ages, than to use this->.

    this pointer just means myself; like any other languages which has class, including C#, It is used like this:

    void SomeUtilFunction(Person *pPerson);
    
    void Person::Foo()
    {
        ...
        SomeUtilFunction(this); // refer to myself
        ...
    }