C + + naming law

Source: Internet
Author: User
Tags function prototype naming convention uppercase letter

1. Naming laws

At present, there are four kinds of naming laws in the Industry: hump nomenclature, Hungarian nomenclature, Pascal's nomenclature and underline nomenclature, among which the first three are the more popular Nomenclature.

(1) hump command Method. As its name implies, it means mixing uppercase and lowercase letters to form the names of variables and functions. For example, the following is the same function named with the camel-named and underscore methods:
printemployeepaychecks (
print_employee_paychecks ();
The first function name uses the hump naming method, and each logical breakpoint in the function name is marked with an uppercase Letter. The second function name uses the underscore method, and each logical breakpoint in the function name is marked with an Underscore.
hump nomenclature has become more and more popular in recent years, in many new libraries and environments such as Microsoft windows, It is used in many proper phases. On the other hand, the underscore is popular after the advent of c, in many of the old programs and Unix environment, it is very common use.


(2) Hungarian Nomenclature. Widely used in environments such as Microsoft Windows. The naming convention for variables (and MACROS) used in Windows programming is the Hungarian nomenclature, which is proposed by Charles Simonyi, a competent Microsoft programmer. Simone.
Hungarian nomenclature identifies the scope, type, and so on, by prefixing the variable name with the corresponding lowercase symbol identifier. These symbols can be used in multiple ways, in the order of first m_ (member variables), re-pointers, re-simple data types, and so On. The advantage of this is that it can increase the readability of the program and facilitate the understanding and maintenance of the Program.
for Example: m_lpszstr, which represents a long pointer member variable that points to a string that ends with 0 characters.
The key to Hungarian nomenclature is that the name of the identifier starts with one or more lowercase letters as a prefix The prefix is preceded by a single word or combination of words with the first letter capitalized, which indicates the purpose of the Variable.


(3) Pascal (pascal) Nomenclature. Similar to the Hump nomenclature, the difference is that the hump naming method is the first letter lowercase, and the Pascal nomenclature is capitalized, such as:
DisplayInfo ();
String UserName;
Both use Pascal's Nomenclature.


(4) summary of three naming conventions: MyData is an example of Pascal's name; MyData is a hump-named method, the first letter of the first word in lowercase, and the first letter of the following word capitalized, looks like a camel; imydata is a Hungarian nomenclature, Its lowercase i describes its form, followed by the same name as pascal, indicating the purpose of the Variable.

2 Basic principles of the nomenclature

(1) the name of the identifier should be clear, clear, have a definite meaning, while using the complete word or everyone can understand the basic abbreviation, to avoid misunderstanding-try to use English words or all Chinese spelling, if there are English words and Chinese mixed definition, the use of the hyphen "_" will be English and Chinese cut Open. Shorter words can be abbreviated by removing the "vowel", and the long Word may be abbreviated by the first few letters of the word; some words are generally recognized abbreviations. For example: temp->tmp, flag->flg, statistic->stat, increment->inc, message->msg and other abbreviations can be basically recognized by EVERYONE.

(2) If a special convention or abbreviation is used in a name, a note shall be Provided. The abbreviations or conventions used in the file, especially the special abbreviations, should be annotated as necessary at the beginning of the source File.

(3) their own unique naming style, to be consistent from start to finish, can not change back and Forth. The personal naming style can only be used if it conforms to the naming rules of the project group or product GROUP. (that is, There is no specified place in the naming convention to have a personal naming style).

(4) for the variable name, prohibit to take a single character (such as i, j, K ...), It is suggested that in addition to have a specific meaning, but also to indicate its variable type, data type, etc., but i, j, K for the local loop variable is Allowed. variables, especially local variables, are easy to strike if they are represented by a single character (E.G. I write j), and are not checked at compile time, and it is possible to spend a lot of time checking for this small error.

(5) do not define identifiers with numbers or more strange characters, unless necessary.

(6) The naming specification must be consistent with the system style used and unified within the same project.

(7) in the same software product, the interface part identifier (variables, structures, functions and constants) should be well-named, to prevent the compilation, link conflict. Identifiers for interface parts should be more restrictive to prevent collisions. If you can specify the variables and constants of the interface section before adding "module" Identification.

(8) using the correct antonym group to name a variable with mutually exclusive meaning or a function of the opposite Action.
Here are some of the antonyms groups that are commonly used in Software.
add/remove begin/end Create/destroy
insert/delete first/last g et/release
increment/decrement Put/get
add/delete lock/unlock open/close
Min/max old/new start/stop
next/previous source/target show/hide
send/receive source/destination
cut/paste Up/down

example:
int min_sum;
int max_sum;
int Add_user (BYTE *user_name);
int Delete_user (BYTE *user_name);

(9) In addition to the compiler Switch/header files and other special applications, should avoid the use of _example_test_ such as the beginning and end of the definition of an underscore

3 Naming conventions for variable names

(1) the naming rules of variables require the use of the "hungarian law".

that is, the beginning of the letter with the type of variable, the rest of the English meaning of the variable, English abbreviation, the Chinese full spelling or the abbreviation of the whole Chinese spelling, require the first letter of the word should be capitalized.
that is: variable name = Variable type + variable's English meaning (or English abbreviation, Chinese full spell, Chinese full spell Abbreviation)
for Non-generic variables, Add a comment description to the definition, and the variable definition is as likely to be placed at the beginning of the Function.
See table Below:
BOOL starts with B BFLG
int begins with I iCount
short int begins with n nstepcount
long int starts with L lsum
Char starts with C ccount
unsigned char starts with by
float starts with F favg
double starts with D Ddeta
unsigned int (WORD) starts with W wcount
unsigned long int (DWORD) starts with a DW dwbroad
string starts with S sFileName
start with SZ with a string ending with 0 szfilename

(2) The basic principle of naming a pointer variable is:
the basic principle of a pointer variable is: "p" + variable type prefix + name, such as a float* type should be represented as Pfstat. The basic rule for a binary pointer variable is: "pp" + variable type prefix + name. The basic rule for a triple pointer variable is: "ppp" + variable type prefix + name.

(3) Global variables start with g_, such as a global long variable defined as G_lfailcount. That is, the variable name =g_+ the variable type + the English meaning (or Abbreviation) of the Variable. This rule also avoids problems caused by local variables and global variables with the same name.

(4) static variables start with s_, such as a static pointer variable defined as S_plperv_inst. That is: variable name =s_+ variable type + variable's English meaning (or abbreviation)

(5) A variable in an enumeration type (enum) requires a prefix with an enumeration variable or its Abbreviation. and is required in Uppercase. Such as:
enum Cmemdays
{
emdays_monday;
emdays_tuesday;
...
};

(6) The name of the struct, union variable is defined by the type in Uppercase. and to prefix it, the naming rules for internal variables are consistent with the variable naming rules.

The structure usually begins with s, such as:
struct Scmnpoint
{
The x position of the int nx;//point
int NY;//point y position
};

a consortium typically begins with u, such as:
Union Ucmlpoint
{
LONG lX;
LONG lY;
}

(7) The constant (including the wrong Encoding) is named, requiring the constant name in uppercase, the constant name in English to express its meaning. When more than one word is required, the hyphen "_" connection must be used between the word and the Word.
such as: #define Cm_file_not_found Cmmakehr (0x20b) where CM represents the Category.

(8) A const variable requires the C_ to be added before the Variable's naming Rules. That is: c_+ variable naming rules; example: const char* c_szfilename;


4 Naming conventions for functions

(1) the name of the function should be as far as possible in English (or English abbreviation, Chinese full spelling, chinese full spell abbreviation) function completion functions--function name should accurately describe function functions. According to the naming principle of the dynamic-object structure, the verb in the function name is preceded, and the prefix of the function is added before the name, and the function name must not be less than 8 letters in Length. The first letter of the function name is capitalized, if each word that contains two words is Capitalized. If it is an OOP method, you can only have verbs (nouns are the object itself). Example:
LONG Getdevicecount (...);
void Print_record (unsigned int rec_ind);
int Input_record (void);
unsigned char get_current_color (void);

(2) avoid using meaningless or ambiguous verbs to name the Function. Use process, handle, and so on to name functions, because these verbs do not specify what to do.

(3) a function prototype declaration must be Used. Function prototype declarations include: reference to foreign functions and intrinsic functions, external references must be on the right side of the function source: module name and file name, intrinsic function, as long as the comment its definition file name--and the caller in the same file (simple Program) do not need comments.
You should ensure that the names, types, and types of parameters in each function declaration are the same as those in the Definitions.


3.5 function parameter Naming specification

(1) named reference variable naming specification for parameter Names.
(2) in order to improve the running efficiency of the program, reduce the stack of parameters, transfer the parameters of large structures, all are passed by pointer or reference Method.
(3) in order to make it easier for other programmers to identify whether a pointer parameter is an ingress parameter or an exit parameter, and for the compiler to check for errors, the const flag should be added before the entry Parameter.
Such as: ... cmcopystring (const char * c_szsource, CHAR * szdest)


3.6 Naming conventions for file names (including dynamic libraries, components, controls, engineering files, and so On)

The name of the filename requires the content of the file to be expressed, requiring the file name to be no less than 5 letters, and the use of filenames such as File1,myfile is strictly Forbidden.

C + + naming law

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.