Guide to High quality C++/C programming-3rd-naming rules (1)

Source: Internet
Author: User
Tags naming convention

3rd Chapter naming rules
A more famous naming convention when pushing Microsoft's "Hungarian" law, the main idea of the naming convention is "to add prefixes to variables and function names to improve understanding of the program." For example, all character variables are prefixed with CH, and if the pointer variable appends the prefix p. If a variable starts with a ppch, it indicates that it is a pointer to a character pointer. The biggest drawback of the "Hungarian" law is that it is cumbersome, for example

int I, j, K;

float x, y, Z;

If the "Hungarian" naming convention 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

Such cumbersome procedures will make the vast majority of programmers unbearable.

According to research, there is no naming convention that allows all programmers to agree that programming textbooks generally do not specify naming rules. Naming rules are not a matter of "success or failure" for software products, and we should not try to invent the best naming conventions in the world, but should develop a naming convention that satisfies most project members and implements them in the project.

3.1 Common rules
The common rules discussed in this section are adopted by most programmers, and we should expand specific rules, such as Section 3.2, on the premise of following these common rules.


L "rule 3-1-1" identifiers should be intuitive and can be spelt out, which is expected to be understood without the need for "decoding".

Identifiers are best used in English words or their combination to facilitate memory and reading. Avoid using Hanyu Pinyin to name them. The English words in the program are generally not too complicated, and the words should be accurate. For example, don't write CurrentValue as Nowvalue.


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

A few decades ago, old ANSI C stated that the name must not exceed 6 characters, and today's c++/c no longer has this restriction. In general, long names can better express meaning, so the function name, variable name, class name up to more than 10 characters is not surprising. So, is the name longer? Not! For example, variable name maxval is more useful than maxvalueuntiloverflow. Given's name is also useful, common such as i,j,k,m,n,x,y,z, which can often be used as local variables within a function.


The "Rule 3-1-3" naming convention is as far as possible consistent with the style of the operating system or development tools used.

For example, the identifier for a Windows application is usually in a "case-by-case" style, such as Addchild. Identifiers for UNIX applications usually take the form of "lowercase underline", such as Add_child. Don't mix these two types of styles together.


L "rule 3-1-4" programs do not show similar identifiers that are case-sensitive only.

For example:

int x, x; Variable x is easy to confuse with X

void foo (int x); function Foo is easily confused with Foo

void FOO (float x);


L "rule 3-1-5" programs do not appear the exact same identifier of local and global variables, although the scope of the two is not a syntax error, but misleading.


L The name of the "rule 3-1-6" variable should use "noun" or "adjective + noun".

For example:

float value;

float OldValue;

float NewValue;


The name of the "rule 3-1-7" global function should use "verb" or "verb + noun" (moving object phrase). A member function of a class should use only "verbs", and the omitted noun is the object itself.

For example:

Drawbox (); Global functions

Box->draw (); member functions of a class

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.