Skills related to C ++ identifier naming rules

Source: Internet
Author: User

As a programmer in the field of melee and development, mastering more than two programming languages is a secret to their success. C ++ is an important choice. Here we will introduce some concepts of the C ++ identifier naming rules.

The famous naming rules are Microsoft's "Hungary" method, the main idea of this naming rule is to "add a prefix to the variable and function name to enhance people's understanding of the program ". For example, all character variables are prefixed with ch, and if it is a pointer variable, the prefix p is appended. If a variable starts with ppch, it indicates that it is a pointer to a character pointer. The biggest disadvantage of the "Hungary" law is that it is cumbersome, for example

 
 
  1. int i, j, k;   
  2. float x, y, z; 

If the "Hungary" Naming rule is used, it should be written as: int iI, iJ, ik; // prefix I indicates int float fX, fY, fZ; // The prefix f indicates that the float type is so cumbersome that the vast majority of programmers cannot bear it. According to the investigation, there is no naming rule that can be approved by all programmers. Generally, No naming rule is specified in the programming textbooks. Naming rules are not a "success or failure" thing for software products. We should not try to invent the best names in the world with too much energy.

  • C ++ strtok () Implementation Function Analysis
  • Interpretation of the C ++ TinyXml Application Method
  • Basic Content of C ++ Singleton Mode
  • Analysis of related methods for creating Web services in C ++
  • How to initialize the C ++ Constructor

Rules, but a naming rule should be formulated to satisfy most project members and implemented in the project. 3.1 Common rules the common rules discussed in this section are adopted by most programmers. We should expand the specific rules while following these common rules.

Identifiers should be intuitive and readable. They do not need to be decoded ".

It is recommended that identifiers use English words or their combinations to facilitate memory and reading. Do not use Chinese pinyin for naming. The English words in the program are generally not too complex and should be accurate. For example, do not write CurrentValue as NowValue.

The length of the identifier must comply with the "min-length & max-information" principle.

A few decades ago, ansi c specified that the name should not exceed 6 characters. Today's C ++/C does not have this limit. In general, long names can better express meanings. Therefore, function names, variable names, and class names can contain up to a dozen characters. So is the name a longer appointment? No! For example, the variable name maxval maxValueUntilOverflow is easy to use. Single-character names are also useful. Common ones include I, j, k, m, n, x, y, and z. They are usually used as local variables in functions.

The naming rules of C ++ identifiers should be consistent with the operating system or development tool style.

For example, the identifiers of Windows applications are usually in a case-insensitive manner, such as AddChild. The identifier of a Unix application is usually "lowercase and underlined", for example, add_child. Do not mix these two types of styles.

Do not show similar identifiers that are case sensitive.

For example:

 
 
  1. Int x, X; // variables x and X are easy to confuse void foo (int x); // functions foo and foo are easy to confuse
  2. Void FOO (float x );

The local variables and global variables with identical identifiers should not appear in the program. Although the scopes of the two are different without syntax errors, they may be misunderstood. Rule 3-1-6: The variable name should use a "noun" or "Adjective + noun ".
For example:

 
 
  1. float value;  
  2. float oldValue;  
  3. float newValue; 

The name of a global function should use the verb or verb + noun verb phrase ). Class member functions should only use the "verb", and the omitted nouns are the object itself. For example:

 
 
  1. DrawBox (); // global function
  2. Box-> Draw (); // member functions of the class


Use a correct antgroup to name variables with mutex meanings or functions with opposite actions. For example:

 
 
  1. int minValue;  
  2. int maxValue;  
  3. int SetValue(…);  
  4. int GetValue(…); 

Avoid numeric numbers in the name, such as Value1 and Value2, unless the number is required logically. This is to prevent programmers from being lazy and failing to name their brains, resulting in meaningless names because numbers are the most convenient ). The authors of simple naming rules for Windows applications reasonably simplified the naming rules for "Hungary". The following naming rules for C ++ identifiers are easy to use and suitable for the development of Windows applications.

The class name and function name are combined with words starting with an upper-case letter. For example:

 
 
  1. Class Node; // class Name
  2. Class LeafNode; // class Name
  3. Void Draw (void); // function name
  4. Void SetValue (int value); // function name l

Variables and parameters are combined with words starting with lowercase letters. For example:

 
 
  1. BOOL flag;  
  2. int drawMode; 

Constants all use uppercase letters and underline to separate words. For example:

 
 
  1. const int MAX = 100;const int MAX_LENGTH = 100; 

Static variables with the prefix "s _" indicates static ). For example:

 
 
  1. Void Init (...)
  2. {
  3. Static int s_initValue; // static variable
  4. ...
  5. }

If you have to use a global variable, add the global variable prefix g _ to indicate global ). For example:

 
 
  1. Int g_howManyPeople; // global variable
  2. Int g_howMuchMoney; // global variable

The data member of the class is prefixed with m _, which indicates member. This prevents the data member from having the same name as the parameter of the member function. For example:

 
 
  1. void Object::SetValue(int width, int height)  
  2. {  
  3. m_width = width;  
  4. m_height = height;  

To prevent conflicts between some identifiers in a software library and other software libraries, you can add prefixes that reflect the nature of software to various identifiers. For example, all library functions of 3D graphics standard OpenGL start with gl, and all constants or macros are defined) start with GL.

The above section describes the naming rules for C ++ identifiers.

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.