C + + variable naming conventions

Source: Internet
Author: User
Tags naming convention variable scope

In the software development process, the code naming specification is a very prominent content. A set of fully defined and clearly structured naming conventions will greatly improve the readability of the source code and the maintainability of the software.

According to textual research, no one naming rules can make all programmers satisfied, programming textbooks generally do not specify the naming rules. However, it is necessary to develop a set of naming rules to satisfy the development team members and implement them in the project.

I organize the naming conventions of my team's actual work as follows:

The first part: common rules

There are eight items. As a guide for members to adopt.

The "Rule 1" identifier should be simple and clear, looking at the text to understand.

Identifiers are in English words. Do not use Hanyu Pinyin to name. The English words in the program are generally not too complicated, the words should be accurate. For example, do not write C + + (C + + training) Urrentvalue as Nowvalue.

Try not to use word abbreviations or acronyms. You should only consider using the word abbreviation if the identifier is too long. When using abbreviations, do not create abbreviations and try to use widely accepted abbreviations.

The length of the "rule 2" identifier should conform to the "Min-length && max-information" principle.

Generally speaking, long names can better express meaning, so the function name, variable name, class name up to more than 10 characters is not surprising. But the name is not as long as possible. For example: variable name maxval is better than Maxvalueuntiloverflow. Single-character's name is also useful, as is common in i,j,k,m,n,x,y,z, which are often used as local variables within a function.

The rule 3 naming convention is as consistent as possible with the style of the operating system or development tool being used.

For example, the identifier for a Windows application is typically in a "case"-mixed manner, such as Addchild. The identifiers of Unix applications are usually "lowercase underlined", such as Add_child. Don't mix the two types of styles together.

The case-sensitive identifiers do not appear in the Rule 4 program.

For example: int X and int x;void foo () and void Foo (), and so on.

Rule 5 avoids duplicate names at different levels of scope.

In the program, you do not have local and global variables with identical identifiers, although they do not cause syntax errors due to different scopes, but they can be misleading.

"Rule 6" correctly names identifiers that have mutually exclusive meanings.

Use the correct antonyms group to name a variable with mutually exclusive meaning or a function of the opposite action.

such as: "MinValue" and "MaxValue", "GetName ()" and "SetName ()"

"Rule 7" try to avoid the number of numbers appearing in the name.

such as value1,value2, unless the number is logically required. This is to prevent the program from producing meaningless names and to reduce the readability of the program.

"Rule 8" uses library flags

When developing a dynamic library, to prevent some identifiers in the software library from conflicting with identifiers in other repositories, you can add prefixes that reflect the nature of the software for various identifiers.

For example, all library functions for the three-dimensional graphics standard OpenGL begin with GL, and all constants (or macro definitions) begin with GL.

Part II: Details

I used a part of the "Hungarian" nomenclature, but did not copy. The most important feature of the "Hungarian" law is the type prefix. For example:

int nnum = 0; n is the type information that indicates that nnum is an int type

Class Cuser; C is type information, indicating that Cuser is a class

But because of this, the "Hungarian" Code of nomenclature also gives people a tedious feeling. For example:

int I, j, K;

float x, y, Z;

If the "Hungarian" nomenclature is adopted, it should be written as:

int II, IJ, IK; prefix i denotes int type

Float FX, FY, FZ; Prefix f denotes float type

For this type of situation, I do not prefix the type. And I will add to the following situations:

int Iheigh;

Bool Bflag;

In summary, in my naming convention, the type prefix is an optional naming convention. The following categories are detailed specifications.

I. Classes and Interfaces

1. Naming: Class names begin with the capital letter "C" followed by one or more words. Capitalize the first letter of each word. The interface begins with an uppercase "I" and represents the interface.

2. Form of composition: recommend the form of "noun" or "adjective + noun", for example: "Canalyzer", "Cfastvector" ....

Second, function

1. Naming: The name of a function consists of one or more words. Capitalize the first letter of each word. The maximum length of the 20 characters is not exceeded.

2. Form: The global function should use the form of "verb" or "verb + noun" (dynamic-object phrase). For example: "Ggetname ()", "Gdrawbox ()".

The class member function should use only "verbs", and the omitted noun is the object itself. For example:

"Box->draw ();".

3. Global function: Start with the lowercase prefix "g".

4, Protection member function: The beginning should add an underscore "_" to show the difference, for example: "_setstate ()"

5, Private member function: The beginning should be added two underscore "__", for example: "__destroyimp ()"

6, virtual function: Habit to "do" beginning, such as: "Dorefresh ()", "_doencryption ()"

7, Back harmonic event handler function: The habit begins with the word "on". For example: "_ontimer ()", "OnExit ()"

Third, variable

Variables are the most frequently used identifiers in a program, and the naming conventions of variables are the most important part of a set of C + + naming conventions:

1. Name: The variable name consists of the scope prefix + type prefix + one or more words. A variable is combined with a word that starts with a lowercase letter, and the first letter of the second word is capitalized. For example: int ndrawmode. The variable cannot be longer than 20 characters.

Special: For some simple and straightforward local variables, you can also use simplified methods such as: I, J, K, X, Y, Z

2, form: The name of the variable should use "noun" or "adjective + noun". For example: "NCode", "M_nstate", "Nmaxwidth", "OldValue", "NewValue".

3. Scope prefix: The scope prefix indicates the visible range of a variable. There are several scopes that can be used:

Example of prefix description

No local variables

Member variables for the M_ Class (member) Int M_width

Static member variable of the Ms_ class (static member) static int ms_initvalue;

S_ static variable (static) static int s_initvalue;

G_ external global variable (global) int g_howmanypeople;

Sg_ static global variable (statically global)

Gg_

Shared data segment global variables shared between processes (World global)

Description: The scope prefix is different from the following type prefix and should be enforced resolutely. The reasons are:

1) Variable scope and link changes are rare, for example, in rare cases a member variable is changed to a static variable

2) tools used in programming often do not visually display the scope and linkage of variables

4. Type prefix: The scope prefix indicates the visible range of a variable. The type prefix indicates the type of a variable, as follows:

Example of prefix description

b boolean variable (bool, BOOL) benable

ch Character type variable (char TCHAR) chname

Lpsz LPSTR, LPCSTR, LPCTSTR lpszName

n Integer and bit-field variables (int, uint,__int32,__int64) nlength

L Long Loffset

by BYTE

W WORD Wpos

DW DWORD Dwrange

F floating-point variable (float)

D Double

P pointer-type variables and iterations (pointer) pDoc

LP FAR pointer

e enum-type variable (enumeration)

Pfn

Pointer variables and function object pointers, especially for pointers to functions (pointer of function)

G Array (GRID)

H handle Windows object handle HWND

Iv. Constants

Constant names consist of type prefixes + all uppercase letters, which are defined by underscores, such as: Cdelimiter, Nmax_buffer. The definition of a type prefix is the same as in a variable naming convention.

Structure, macro, enumeration variable, union

All are made up of prefix + uppercase letters, with underscores defined between words.

1. struct: Add lowercase prefix "tag", then start with capital letter.

Cases:

typedef struct TAGPOINT

{int x;

int y;

} point;

2, macro: The composition of capital letters, the use of underline between the words to define

Example: #define MAXNUMBER 100

3. Enumeration variable : Add lowercase prefix "enum".

Cases:

typedef enum _FILE_OPEN_MODE

{open_readonly,

Open_readwrite

}file_open_mode;

4, Union: Plus lowercase prefix "uni".

Cases:

typedef Union _variant

{char unichval;

int uninval;

float Uniftval;

} VARIANT;

C + + variable naming conventions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.