C + + function naming conventions

Source: Internet
Author: User
Tags in degrees naming convention

Transferred from: http://blog.csdn.net/rocksword/article/details/10104533

1, strict use of step-level organization program code:
The indentation of each level is based on the default style of the VC, which is indented at 4 cells per level, with parentheses on the next line. The matching curly braces are required to be in the same column, and the following lines require a further indentation of 4 cells. For example:
2, the location of the prompt information string
In order to support the development of multiple languages, in addition to some temporary information for debugging purposes, all other hints must be defined in the resource.
3, the definition of variables, as far as possible at the beginning of the function.
Second, naming rules:
1. Naming rules for variable names
The naming rules for ① and variables require the "Hungarian law". That is, the beginning of the letter with the type of variable, the remainder with the English meaning of the variable or the abbreviation of its English meaning, try to avoid using Chinese pinyin, the first letter of the word should be capitalized.
That is: variable name = variable type + variable's english meaning (or 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 (bool) starts with B bisparent
BYTE (byte) starts with by Byflag
Short (int) begins with n Nstepcount
Long (long) starts with L Lsum
char (char) starts with C ccount
Float (float) starts with F Favg
Double (double) starts with D Ddeta
void (void) starts with V Vvariant
unsigned int (WORD) starts with W Wcount
unsigned long (DWORD) begins with a DW dwbroad
HANDLE (HINSTANCE) begins with H Hhandle
DWORD starts with DW Dwword
LPCSTR (LPCTSTR) starts with str strstring
Start with SZ with a string ending with 0 szfilename

Ask for the non-given variable type and give the naming recommendation to the Technical Committee.

The basic principles for naming ② and pointer variables are:
The basic principles for a pointer variable are:
"P" + Variable type prefix + name
such as a float* type should be expressed as Pfstat
The basic rules for multiple pointer variables are:
Double pointer: "PP" + variable type prefix + name
Triple pointer: "PPP" + Variable type prefix + name
......
③, global variables start with g_, such as a global long variable defined as G_lfailcount, that is: variable name =g_+ variable type + variable's english meaning (or abbreviation)
④, static variables start with s_, such as a static pointer variable defined as S_PLPERV_INST, namely: variable name =s_+ variable type + variable's english meaning (or abbreviation)
⑤, member variables start with m_, such as a long member variable defined as M_lcount, that is: variable name =m_+ variable type + variable's english meaning (or abbreviation)
⑥, a variable in an enumeration type (enum), requires that an enumeration variable or its abbreviation be prefixed. and is required in uppercase.
Example: Enum cmemdays
{
Emdays_monday;
Emdays_tuesday;
......
};
⑦, the naming requirements for struct, union, class variables, are capitalized in the defined type. 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; The Y position of the point
};
Unions typically start with u
such as: Union Ucmlpoint
{
Long LX;
Long LY;
}
Classes typically start with C
Such as:
Class Ccmfpoint
{
Public
float Fpoint;
};
The general structure should be defined as a class template for future extensibility considerations
Such as:
Template
Class Ccmtvector3d
{
Public
TYPE x, y, Z;
};
⑧, the constant (including the wrong encoding) name, the constant name is required in uppercase, constant name in English to express its meaning.
such as: #define Cm_file_not_found Cmmakehr (0x20b) where CM represents the category.
⑨, the variable for const requires adding C_ before the variable's naming rules, namely: c_+ variable naming rules;
Const char* C_szfilename;
2, the function of the naming specification:
The name of the function should be expressed in English as much as possible in the function completion. 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.
For example:
Long Cmgetdevicecount (...);
3, function parameter specification:
①, named reference variable naming specification for parameter names.
②, in order to improve the running efficiency of the program, reduce the stack of parameters, transfer the parameters of large structures, are passed by pointer or reference method.
③, in order to make it easier for other programmers to identify whether a pointer parameter is an ingress or egress parameter, while making it easier for the compiler to check for errors, the const flag should be added before the ingress parameter. Such as:
... cmcopystring (const char * c_szsource, char * szdest)
4, the derivation function specification:
For functions that are exposed as two development functions from a dynamic library, to be able to differentiate from other functions and functions of Windows, a class prefix + basic naming convention method is used. For example, the function defined for an image edit that is derived from a dynamic library is Imgfunctionname (where IMG is an image abbreviation).
The naming prefixes for three libraries are now given:
①, the General function library, with cm as the prefix.
②, the three-dimensional function library, using the VR prefix.
③, the image function library, using the IMG prefix.
For macro definitions, the result code is prefixed with the same prefix.
5. 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.
Third, the annotation specification:
1. Comment of function head
For a function, it should be commented in the following format from "function", "parameter", "Return value", "Main idea", "Call Method", and "date" six:
Start of program Description
//================================================================//
Function: Removes another string from one string.
Parameter: Strbydelete,strtodelete
(entrance) Strbydelete: Deleted string (original String)
(export) Strtodelete: The string to remove from the last string.
Return: Find and delete return 1, otherwise return 0. (the error code is listed with the error code for the return value).
Main idea: This algorithm mainly uses the method of cyclic comparison to find from Strbydelete
A string that matches the Strtodelete, to a multiple-match strbydelete
There are multiple strtodelete substrings in the process. Please refer to:
Title......
Call Method: ...
Date: Start date, such as: 2000/8/21.9:40--2000/8/23.21:45
//================================================================//
Function name (...)
End of Program Description
①, for some functions, some of the parameters are passed in the value, and some of the parameters are outgoing values, so the parameters to specify whether the parameter is an entry parameter, or an exit parameter, for some ambiguous parameters to do a detailed explanation (for example: when taking an angle as a parameter, to show that the angle parameter is in radians (PI), or in degrees), the variables that are both entrances and exits should be marked at both the entrance and exit points. Wait a minute.
②, the comment of the function should be placed in the function's header file, the implementation of the function in the implementation of the part should be placed in the same note.
③, in the comments should explain in detail the main implementation of the function of the idea, especially to indicate some of their own ideas, if necessary, should indicate the cause of the idea. Some functions of imitation should be annotated with the source of the function.
④, the appropriate calling method of the function is specified in the comments, and the method of handling the return value. In the comments, emphasize the danger aspect of the call, where there may be an error.
⑤, a comment on a date requires a record of the date from the start of the write function to the test that ended the function.
⑥, there should be a set of special strings to identify between the start of a function comment and the function name.
If the algorithm is more complex, or the variable definition in the algorithm is relative to the location, then the definition of the variable is required to plot. The incomprehensible algorithm can be graphically illustrated as much as possible.
2. Comments on variables:
The comment for the variable immediately follows the variable to explain the role of the variable. In principle for each variable should be commented, but for the very obvious variables, such as: i,j, such as the loop variable can not be annotated.
For example: Long Llinecount//The root number of the line.
3. Notes to the file:
The file should include the following comment at the beginning of the file:
/////////////////////////////////////////////////////////////////////
Engineering: The name of the project where the file resides.
* *, Modified by: * *
Description: The function of the description file.
The main functions: .....
Version: The version of the description file, the finish date.
Modifications: Describes the changes to the file, the reason for the modification, and the date of modification.
References: ...
/////////////////////////////////////////////////////////////////////
In order for the header file to be repeated included requires that the enemy file be defined as follows:
#ifndef __filename_h__
#define __filename_h__
where filename is the name of the header file.
4. Other Notes:
Within the function we do not need to comment on each line of statements. However, you must add block annotations before each major part of each function module, commenting on each set of statements, and commenting as much as possible on loops, branches of the process, and so on.
The loops, conditions, selections, and so on must be commented.
For cases where the order cannot be reversed, it is recommended to add the ordinal number in the note.
For example:
In other sequential execution of the program, every 3-5 lines of statements, you must add a note, indicating the function of the small module composed of this paragraph. Some of the more unique ideas for yourself are required to be indicated in the notes.
Iv. Robustness of the program:
1, the function of the return value specification:
For the return position of the function, try to maintain a single, that is, a function to do as far as possible only one return position. (Single inlet single exit).
Ask everyone to unify the return value of the function, and all of the function's return values will be returned in an encoded manner.
For example, the code is defined as follows:
#define Cm_point_is_null Cmmakehr (0X200)
:
:
The recommended function is implemented as follows:
Long function name (parameter,......)
{
Long LResult; Keep the error number
LRESULT=CM_OK;
Returns the error number if the parameter has an error
if (parameter ==null)
{
Lresult=cm_point_is_null;
Goto END;
}
......
END:
return lResult;
}
2, about the application of goto:
For the application of goto statements, we require as few goto statements as possible. The requirement for a place to be used must be shifted backwards.
3. Processing of resource variables (resource variables refer to variables that consume system resources):
The initial value must be assigned to the resource variable. The allocated resource must be released immediately after it is exhausted and re-assigned.
4, to the complex conditions of judgment, for the readability of the program, should try to use parentheses.
Example: if (((szfilename!=null) && (lcount>=0))) | | (bisreaded==true))

V. Portability:
1. High-quality code requirements can cross-platform, so our code should take into account the support for different platforms, especially for Windows98 and WindowsNT.
2, because the C language transplant is better, so the algorithm function requires C code, can not be used in C + + code.

3, different hardware and software functions to do different processing

C + + function 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.