Naming rules for VC Programming-[Hungarian naming method]

Source: Internet
Author: User
Tags uppercase letter

 

Windows and VC Programming naming rules the Hungarian naming method is a naming convention for programming. The basic principle is: variable name = property + Type + object description. The name of each object must have a clear meaning. The full name or part of the object name can be used. Naming should be based on the principle of easy to remember and easy to understand. Currently, the Hungarian naming rules are commonly used in Windows program development and MFC program development. The following are general rules of HN naming. Attribute section
Global variable g _
Const constant C _
C ++ class member variable M _
Static variable S _

Type
Pointer P
Function FN
Invalid v
Handle H
Long Integer L
Boolean B
Float (sometimes a file) f
Double-word DW
String SZ
Short integer n
Double Precision Floating Point D
Count C (usually CNT)
CH (usually C)
Integer I (usually N)
Byte
Word w
Real-type R
Unsigned u

Description
Max
Min
Initialize init
Temporary Variable T (or temp)
Source object SRC
Target object dest

Here are some examples:
(1) hwnd: h indicates the type description, indicating the handle, and WND indicates the variable object description, indicating the window, so hwnd indicates the window handle;
(2) pfneatapple: PFN is a type description, indicating a pointer to a function. eatapple is a variable object description, so it indicates
The function pointer variable pointing to the eatapple function.
(3) g_cch: G _ is the attribute description, indicating global variables. C and ch are the Count type and character type respectively, indicating the variable class together.
Type, the object description is ignored here, so it represents a global variable for counting characters.Summary: Hungarian naming law

Naming rules for MFC, handles, controls, and structures: Windows sample variables: MFC sample Variables
Hwnd; cwnd * pwnd;
Hdlg; cdialog * pdlg;
HDC; CDC * PDC;
Hgdiobj; cgdiobject * pgdiobj;
Hpen; cpen * ppen;
Hbrush; cbrush * pbrush;
Hfont; cfont * pfont;
Hbitmap; cbitmap * pbitmap;
Hpalette hpaltte; cpalette * ppalette;
Hrgn; crgn * prgn;
Hmenu; cmenu * pmenu;
Hwnd hctl; cstate * pstate;
Hwnd hctl; cbutton * pbutton;
Hwnd hctl; cedit * pedit;
Hwnd hctl; clistbox * plistbox;
Hwnd hctl; ccombobox * pcombobox;
Hwnd hctl; cscrollbar * pscrollbar;
Hsz hszstr; cstring pstr;
Point pt; cpoint pt;
Size size; csize size;
Rect; crect rect;

General prefix naming rules prefix type instances
C Class or structure cdocument, cprintinfo
M _ member variables m_pdoc, m_ncustomers

Variable naming convention prefix type description instance
Ch char 8-character chgrade
Ch tchar if _ Unicode is defined, it is a 16-character chname
B bool Boolean benable
N int INTEGER (its size depends on the operating system) nlength
N uint unsigned value (its size depends on the operating system) nheight
W word 16-bit unsigned value wpos
L long 32-bit signed integer loffset
Dw dword 32-bit unsigned integer dwrange
P * pointer pdoc
LP far * remote pointer lpszname
Lpsz lpstr 32-Bit String pointer lpszname
Lpsz lpcstr 32-bit constant string pointer lpszname
Lpsz lpctstr if _ Unicode is defined, it is a 32-bit constant string pointer lpszname
H handle windows object handle hwnd
Lpfn callback: Remote pointer to the callback function

Resource Type naming rules prefix symbol type instance range
Idr_multiple Resource Sharing identities of different types idr_maiinframe 1 ~ 0x6fff
Resource idd_spell_check 1 ~ 0x6fff
Hidd _ dialog box resource help context hidd_spell_check 0x20001 ~ 0x26ff
Idb_bitmap resource idb_company_logo 1 ~ 0x6fff
IDC _ cursor resource idc_penpencil 1 ~ 0x6fff
IDI _ icon resource idi_notepad 1 ~ 0x6fff
Id_tools_spelling 0x8000 ~ 0 xdfff
Hid _ Command help context hid_tools_spelling 0x18000 ~ 0x1dfff
The IDP _ message box prompts idp_invalid_partno 8 ~ 0 xdeef
Hidp _ message box help context hidp_invalid_partno 0x30008 ~ 0x3deff
IDS _ string resource ids_copyright 1 ~ 0x7eef
Idc_recalc 8 ~ 0 xdeef

Microsoft MFC macro naming convention name Type
_ Afxdll's unique dynamic connection library (DLL) version
_ ALPHA: only compiles the DEC Alpha processor.
_ Debug includes debugging versions for Diagnosis
_ MBCS compile multi-byte character sets
_ Unicode open Unicode in an application
Functions provided by afxapi MFC
Callback functions that Use Pointer callback

Library identifier name method identifier value and meaning
U ansi (n) or Unicode (u)
D debugging or release: D = debugging; ignore the identifier as release.

Static library version naming convention library description
Debugging version of nafxcwd. Lib: MFC static Connection Library
Nafxcw. Lib release: MFC static Connection Library
Uafxcwd. Lib debugging version: the static Connection Library of MFC with Unicode support
Uafxcw. Lib release version: static Connection Library of MFC with Unicode support

Dynamic Connection Library naming convention name Type
_ Afxdll unique dynamic connection library (DLL) version
Functions provided by winapi windows

Description of the new naming convention type definition in windows. h
Winapi uses the far Pascal position in the API declaration. If you are writing a DLL with an export API population point, you can use this type in your own API.
Callback uses the position of far Pascal in the application call-back routine, such as the window and dialog box.
The same as lpstr, but the same as the definition (const char far *)
A portable unsigned integer type. It is a synonym for an unsigned Int.
Type of the return value of the lresult window Program
Lparam declares the type used by lparam. lparam is the fourth parameter of the window program.
Wparam declares the type used by wparam. wparam is the third parameter of the window program.
Lpvoid is generally pointer type, which is the same as (void *) and can be used to replace lpstr Hungary programming naming rules.The Hungarian Naming Convention includes the following naming conventions: variables, functions, types, constants, and classes.

(1) The Hungarian name of the variable.
Using the Hungary name method, all variable names should appear in the form of suffix + name. For example:

Char * szname; // string with 0 as the ending character. The name variable bool bcanexit is stored. // Boolean variable. Can you exit DWORD dwmaxcount; // 32-bit double-character variable, Max count

(2) The Hungarian name of the function.
Unlike variable naming, the name of a function does not contain a prefix. The name of each word in a function must start with an uppercase letter. For example:

int ConvertNumber( int ix );void ShowMessage( char* szMessage );    

(3) type and constant Hungarian naming method.
All types and constants are named in uppercase letters, for example:

#define MAX_NUM 256typedef unsigned char UCHAR;  

(4) Hungary naming method.
Class naming rules are to add a letter C before the name, for example:

 class CMyClass{public: CMyClass(); ~CMyClass(); …private: m_szName; 
};    

When naming member variables of a class, the M _ prefix must be added before the variable name. In general, it is better to follow this rule to program, so that you can learn more about the role of variables in later reading or modifying programs.

**************************************** **************************************** **********

VC naming rules

I. Program style:
1. Strictly adopt the hierarchical program code:
The default style of VC is used for each level of indentation, that is, each layer is indented into four cells, and brackets are placed in the next line. The matching braces must be in the same column, and the following rows must be indented by four cells. For example:
2. Position of the prompt message string
The prompt string to be given in the program. In order to support development in multiple languages, all other prompts except Temporary Information for debugging must be defined in the resource.
3. Define variables at the beginning of the function as much as possible.

Ii. Naming rules:
1. Naming rules for variable names
1. The variable naming rules must use the Hungary rule ". That is, the start letter uses the type of the variable, and the rest uses the English meaning of the variable or the abbreviation of its English meaning. Do not use Chinese pinyin as much as possible, and the first letter of the word should be large.
That is, the variable name = the variable type + the meaning (or abbreviation) of the Variable)
For non-General variables, add comments during definition. The variable definition may be placed at the beginning of the function as much as possible.
See the table below:
Bool (bool) starts with B and bisparent
Byte (byte) starts with byflag
Short (INT) starts nstepcount with N
Long (long) starts lsum with L
Char (char) starts with C and ccount
Float (float) starts with F.
Double (double) start with D ddeta
Void (void) starts vvariant with V
Unsigned int (Word) starts with W wcount
Unsigned long (DWORD) starts with DW dwbroad
Handle (hinstance) starts with H
DWORD starts with DW
Start strstring with 'strstring
String ending with 0 starts with SZ szfilename

Propose and give naming suggestions for unspecified variable types to the Technical Committee.

② The basic principles for naming pointer variables are as follows:
The basic principles for a heavy pointer variable are:
"P" + variable type prefix + name
For example, a float * type should be expressed as pfstat.
The basic rules for multiple pointer variables are as follows:
Double pointer: "PP" + variable type prefix + name
Triple pointer: "PPP" + variable type prefix + name
......
③ Global variables start with G _. For example, a global long variable is defined as g_lfailcount, that is, the variable name = g _ + the variable type + the meaning (or abbreviation) of the variable)
④ Static variables start with S _. For example, a static pointer variable is defined as s_plperv_inst, that is, the variable name = S _ + variable type + the meaning (or abbreviation) of the variable)
⑤ A member variable starts with M _. For example, a long member variable is defined as m_lcount. That is, the variable name = M _ + variable type + the meaning (or abbreviation) of the variable)
6. For variables of the enumeration type (Enum), you must use the enumerated variable or its abbreviation as the prefix. In addition, uppercase is required.
For example, Enum cmemdays
{
Emdays_monday;
Emdays_tuesday;
......
};
7. The naming requirements for struct, union, and class variables should be defined in uppercase. The prefix must be added. The internal variable naming rules are consistent with the variable naming rules.
The structure generally starts with S.
For example, struct scmnpoint
{
Int NX; // The X position of the vertex
Int NY; // The y position of the vertex
};
A consortium generally starts with u.
For example, Union ucmlpoint
{
Long lx;
Long ly;
}
Class generally starts with C
For example:
Class ccmfpoint
{
Public:
Float fpoint;
};
The general structure should be defined as a class template for future scalability considerations.
For example:
Template
Class ccmtvector3d
{
Public:
Type X, Y, Z;
};
Constant, the constant name (including the wrong encoding), requires that the constant name in uppercase, the constant name in English to express its meaning.
For example, # define cm_file_not_found cmmakehr (0x20b) Where cm indicates the category.
For const variables, add C _ before the naming rules of variables, that is, C _ + variable naming rules. For example:
Const char * c_szfilename;
2. Function naming rules:
The function name should be expressed in English as much as possible. Follow the naming rules of the dynamic object structure. In the function name, the verb is in the front and the prefix of the function is added before the name. The function name must be at least eight letters in length.
For example:
Long cmgetdevicecount (......);
3. function parameter specifications:
1. For parameter names, see variable naming rules.
② In order to improve the program running efficiency, reduce the stacks occupied by parameters, and transmit large-structure parameters, all parameters are transmitted using pointers or references.
③ To facilitate other programmers to identify whether a pointer parameter is an entry parameter or an exit parameter, and to facilitate compiler check errors, the const mark should be added before the entry parameter. For example:
...... Cmcopystring (const char * c_szsource, char * szdest)
4. Introduce function specifications:
For a function exposed as a secondary development function from a dynamic library, class prefix + basic naming rules are used to distinguish it from other functions and Windows functions. For example, the function for editing an image in the dynamic library is defined as imgfunctionname (IMG is short for image ).
The naming prefixes of the three databases are given:
① For common function libraries, use cm as the prefix.
② Use VR as the prefix for the 3D function library.
③ Use IMG as the prefix for image function libraries.
For macro definition, the result code uses the same prefix.
5. Naming rules for file names (including dynamic libraries, components, controls, and engineering files:
Naming of file names requires that the content of the file be expressed. The file name must contain no less than five letters. It is strictly prohibited to use file names such as file1 and myfile.

Iii. annotation specifications:
1. Function header comments
For functions, the following format should be noted in terms of "function", "parameter", "Return Value", "main idea", "Call method", and "date:
// Program description starts
// ================================================ ========================================== //
// Function: delete another string from a string.
// Parameter: strbydelete, strtodelete
// (Entry) strbydelete: The deleted string (original string)
// (Exit) strtodelete: string to be deleted from the previous string.
// Return: 1 is returned if you find and delete it. Otherwise, 0 is returned. (If there is an error code for the returned value, // list the error codes ).
// Main idea: This algorithm mainly uses the cyclic comparison method to find it from strbydelete
// String matching strtodelete, matching multiple strbydelete
// There are multiple strtodelete substrings. See:
// Name of the book ......
// Call method :......
// Date: start date, for example, 2000/8/21. -- 2000/8/23.
// ================================================ ========================================== //
Function Name (......)
// Program description ended
① For some functions, some of the parameters are input values and some of the parameters are output values. Therefore, the parameters must be described in detail as entry parameters or exit parameters, some parameters with unclear meanings need to be described in detail (for example, when the angle is used as a parameter, it must be indicated that the angle parameter is in the unit of radian (PI) or degree ), variables that are both entry and exit should be both marked at the entrance and exit. And so on.
② Function comments should be placed in the function header file, and the comments should be placed at the same time in the implementation part of the function in the implementation file.
③ In the comments, we should explain in detail the main implementation ideas of the function, especially some of our own ideas. If necessary, we should specify the reasons for the idea. Comments on the source of some simulated functions.
④. In the comments, specify the appropriate call method of the function and the processing method of the returned value. In the comments, we must emphasize the danger of calling, where errors may occur.
⑤ Date annotation requirements record the date from the test of the function to the end function.
6. There should be a set of special strings used for identification between the function annotation and the function name.
If the algorithm is complex or the variable definition in the algorithm is related to the location, the definition of the variable must be illustrated. Explain hard-to-understand algorithms as much as possible.
2. Variable comments:
The comment on the variable follows the variable to describe the role of the variable. In principle, each variable should be annotated, but it can be non-Annotated for variable with significant significance, such as I and j.
For example, long llinecount // The number of lines.
3. file notes:
Add the following comments to the beginning of the file:
//////////////////////////////////////// /////////////////////////////
// Project: The project name of the file.
// Author: **, modifier :**
// Description: describes the functions of the file.
// Main function :............
// Version: Specifies the version of the file, and the completion date.
// Modify: Describes the file modification content, reasons, and date.
// References :......
//////////////////////////////////////// /////////////////////////////
For repeated header file inclusion, the first object must be defined as follows:
# Ifndef _ filename_h __
# DEFINE _ filename_h __
Filename indicates the name of the header file.
4. Other notes:
In the function, we do not need to comment every line of statements. However, you must add block comments before each major part of each functional module, comment out each group of statements, and comment out as many branches as possible in the loop and process.
The cycle, condition, and selection must be commented out.
If the order before and after cannot be reversed, we recommend that you add the sequence number in the comment.
For example:
In other programs executed in sequence, a comment must be added for every 3-5 rows of statements, indicating the role of a small module composed of these statements. Note your own unique ideas in comments.

4. Program Robustness:
1. Specification of function return values:
For the return position of a function, try to keep it single, that is, a function tries its best to have only one return position. (Single portal and Single Exit ).
It is required that the return values of all functions be unified. The return values of all functions are returned in encoding.
For example, the encoding is defined as follows:
# Define cm_point_is_null cmmakehr (0x200)
:
:
The recommended function implementation is as follows:
Long function name (parameter ,......)
{
Long lresult; // keep the error code
Lresult = cm_ OK;
// If the parameter has an error, the error code is returned.
If (parameter = NULL)
{
Lresult = cm_point_is_null;
Goto end;
}
......
End:
Return lresult;
}
2. Goto applications:
For the application of the GOTO statement, we need to use as few goto statements as possible. Only backward transfer is required.
3. Processing of resource variables (resource variables refer to variables that consume system resources ):
Assign an initial value to the resource variable. The allocated resources must be released immediately after they are used up and assigned a value again.
4. To judge complex conditions, brackets should be used whenever possible for program readability.
Example: If (szfilename! = NULL) & (lcount> = 0) | (bisreaded = true ))

V. Portability:
1. High quality code requirements can be cross-platform. Therefore, we should consider the support for different platforms, especially for Windows98 and WindowsNT.
2. Due to the good portability of C language, C code is required for Algorithm functions, rather than C ++ code.
3. Perform different processing on functions of different hardware and software.

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.