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

前端 未结 19 2138
春和景丽
春和景丽 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:36

    The Microsoft naming standard for C# says variables and parameters should use the lower camel case form IE: paramName. The standard also calls for fields to follow the same form but this can lead to unclear code so many teams call for an underscore prefix to improve clarity IE: _fieldName.

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

    I use the _var naming for member variables of my classes. There are 2 main reasons I do:

    1) It helps me keep track of class variables and local function variables when I'm reading my code later.

    2) It helps in Intellisense (or other code-completion system) when I'm looking for a class variable. Just knowing the first character is helpful in filtering through the list of available variables and methods.

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

    As far as the C and C++ languages are concerned there is no special meaning to an underscore in the name (beginning, middle or end). It's just a valid variable name character. The "conventions" come from coding practices within a coding community.

    As already indicated by various examples above, _ in the beginning may mean private or protected members of a class in C++.

    Let me just give some history that may be fun trivia. In UNIX if you have a core C library function and a kernel back-end where you want to expose the kernel function to user space as well the _ is stuck in front of the function stub that calls the kernel function directly without doing anything else. The most famous and familiar example of this is exit() vs _exit() under BSD and SysV type kernels: There, exit() does user-space stuff before calling the kernel's exit service, whereas _exit just maps to the kernel's exit service.

    So _ was used for "local" stuff in this case local being machine-local. Typically _functions() were not portable. In that you should not expect same behaviour across various platforms.

    Now as for _ in variable names, such as

    int _foo;

    Well psychologically, an _ is an odd thing to have to type in the beginning. So if you want to create a variable name that would have a lesser chance of a clash with something else, ESPECIALLY when dealing with pre-processor substitutions you want consider uses of _.

    My basic advice would be to always follow the convention of your coding community, so that you can collaborate more effectively.

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

    A naming convention like this is useful when you are reading code, particularly code that is not your own. A strong naming convention helps indicate where a particular member is defined, what kind of member it is, etc. Most development teams adopt a simple naming convention, and simply prefix member fields with an underscore (_fieldName). In the past, I have used the following naming convention for C# (which is based on Microsofts conventions for the .NET framework code, which can be seen with Reflector):

    Instance Field: m_fieldName
    Static Field: s_fieldName
    Public/Protected/Internal Member: PascalCasedName()
    Private Member: camelCasedName()

    This helps people understand the structure, use, accessibility and location of members when reading unfamiliar code very rapidly.

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

    You can create your own coding guidelines. Just write a clear documentation for the rest of the team.

    Using _field helps the Intelilsense to filter all class variables just typing _.

    I usually follow the Brad Adams Guidelines, but it recommends to not use underscore.

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

    The first commenter (R Samuel Klatchko) referenced: What are the rules about using an underscore in a C++ identifier? which answers the question about the underscore in C++. In general, you are not supposed to use a leading underscore, as it is reserved for the implementer of your compiler. The code you are seeing with _var is probably either legacy code, or code written by someone that grew up using the old naming system which didn't frown on leading underscores.

    As other answers state, it used to be used in C++ to identify class member variables. However, it has no special meaning as far as decorators or syntax goes. So if you want to use it, it will compile.

    I'll leave the C# discussion to others.

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