Naming convention - underscore in C++ and C# variables

前端 未结 19 2137
春和景丽
春和景丽 2020-11-28 01:11

It\'s common to see a _var variable name in a class field. What does the underscore mean? Is there a reference for all these special naming conventions?

相关标签:
19条回答
  • 2020-11-28 01:29

    Many people like to have private fields prefixed with an underscore. It is just a naming convention.

    C#'s 'official' naming conventions prescribe simple lowercase names (no underscore) for private fields.

    I'm not aware of standard conventions for C++, although underscores are very widely used.

    0 讨论(0)
  • 2020-11-28 01:29

    From my experience (certainly limited), an underscore will indicate that it is a private member variable. As Gollum said, this will depend on the team, though.

    0 讨论(0)
  • 2020-11-28 01:30

    Actually the _var convention comes from VB not C# or C++ (m_,... is another thing).

    This came to overcome the case insensitivity of VB when declaring Properties.

    For example, such code isn't possible in VB because it considers user and User as the same identifier

    Private user As String
    
    Public Property User As String
      Get
        Return user
      End Get
      Set(ByVal Value As String)
        user = value
      End Set
    End Property
    

    So to overcome this, some used a convention to add '_' to the private field to come like this

    Private _user As String
    
    Public Property User As String
      Get
        Return _user
      End Get
      Set(ByVal Value As String)
        _user = value
      End Set
    End Property
    

    Since many conventions are for .Net and to keep some uniformity between C# et VB.NET convention, they are using the same one.

    I found the reference for what I was saying : http://10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices

    Camel Case with Leading Underscore. In VB.NET, always indicate "Protected" or "Private", do not use "Dim". Use of "m_" is discouraged, as is use of a variable name that differs from the property by only case, especially with protected variables as that violates compliance, and will make your life a pain if you program in VB.NET, as you would have to name your members something different from the accessor/mutator properties. Of all the items here, the leading underscore is really the only controversial one. I personally prefer it over straight underscore-less camel case for my private variables so that I don't have to qualify variable names with "this." to distinguish from parameters in constructors or elsewhere where I likely will have a naming collision. With VB.NET's case insensitivity, this is even more important as your accessor properties will usually have the same name as your private member variables except for the underscore. As far as m_ goes, it is really just about aesthetics. I (and many others) find m_ ugly, as it looks like there is a hole in the variable name. It's almost offensive. I used to use it in VB6 all the time, but that was only because variables could not have a leading underscore. I couldn't be happier to see it go away. Microsoft recommends against the m_ (and the straight _) even though they did both in their code. Also, prefixing with a straight "m" is right out. Of course, since they code mainly in C#, they can have private members that differ only in case from the properties. VB folks have to do something else. Rather than try and come up with language-by-language special cases, I recommend the leading underscore for all languages that will support it. If I want my class to be fully CLS-compliant, I could leave off the prefix on any C# protected member variables. In practice, however, I never worry about this as I keep all potentially protected member variables private, and supply protected accessors and mutators instead. Why: In a nutshell, this convention is simple (one character), easy to read (your eye is not distracted by other leading characters), and successfully avoids naming collisions with procedure-level variables and class-level properties.class-level properties.

    0 讨论(0)
  • 2020-11-28 01:33

    It's simply means that it's a member field in the class.

    0 讨论(0)
  • 2020-11-28 01:34

    It is best practice to NOT use UNDERSCORES before any variable name or parameter name in C++

    Names beginning with an underscore or a double underscore are RESERVED for the C++ implementers. Names with an underscore are reserved for the library to work.

    If you have a read at the C++ Coding Standard, you will see that in the very first page it says:

    "Don't overlegislate naming, but do use a consistent naming convention: There are only two must-dos: a) never use "underhanded names," ones that begin with an underscore or that contain a double underscore;" (p2 , C++ Coding Standards, Herb Sutter and Andrei Alexandrescu)

    More specifically, the ISO working draft states the actual rules:

    In addition, some identifiers are reserved for use by C ++ implementations and shall not be used otherwise; no diagnostic is required. (a) Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use. (b) Each identifier that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

    It is best practice to avoid starting a symbol with an underscore in case you accidentally wander into one of the above limitations.

    You can see it for yourself why such use of underscores can be disastrous when developing a software:

    Try compiling a simple helloWorld.cpp program like this:

    g++ -E helloWorld.cpp
    

    You will see all that happens in the background. Here is a snippet:

       ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
       try
         {
           __streambuf_type* __sb = this->rdbuf();
           if (__sb)
      {
        if (__sb->pubsync() == -1)
          __err |= ios_base::badbit;
        else
          __ret = 0;
      }
    

    You can see how many names begin with double underscore!

    Also if you look at virtual member functions, you will see that *_vptr is the pointer generated for the virtual table which automatically gets created when you use one or more virtual member functions in your class! But that's another story...

    If you use underscores you might get into conflict issues and you WILL HAVE NO IDEA what's causing it, until it's too late.

    0 讨论(0)
  • 2020-11-28 01:34

    It's just a convention some programmers use to make it clear when you're manipulating a member of the class or some other kind of variable (parameters, local to the function, etc). Another convention that's also in wide use for member variables is prefixing the name with 'm_'.

    Anyway, these are only conventions and you will not find a single source for all of them. They're a matter of style and each programming team, project or company has their own (or even don't have any).

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