A few years ago, Charles Simonyi (who later became famous Microsoft programmer) design method for naming prefix-based, this method is later called "Hungarian notation" in memory of him. His ideas are based on each meaning it represents the identifier to a prefix. Microsoft later adopted this idea, a prefix to each identifier to indicate its data type. Therefore, the prefix is an integer variable n, long integer variable is nl, character array variables ca, and the string (null terminated character array type) to sz prefixed names can be very weird for example:.. lpszFoo represent "Foo" is a pointer to a null character at the end of the string long integer pointer.
The advantage of this approach is that the type of people able to identify variable by variable name, not than to find its definition. Unfortunately, this approach not only makes the variable name is very convoluted, but also change the variable type of work change very difficult. in Windows3.1, the integer variable is 16 wide. If we used an integer variable at the beginning, but after calculating the 30 --- 40 functions by using integer variable width found is not enough, then we must not only change the type of the variable, but also to change the name variable 30 - 40 months in this function.
Because unrealistic, except for some stubborn Windows programmers have no longer use the "Hungarian notation" was. There is no doubt that in some cases it still exists, but most people have now abandoned it. In general enter the prefix is a bad idea, because it is a variable whose type closely tied together.
For functions of 30 lines or less, Hungary methods generally have an advantage.
Especially for the programming interface, you have an advantage.
But there is a strong algorithm requires, in particular, there are many types of abstract C ++ program, Hungary method is simply a disaster.
Do you use in any place.
Now, with a good IDE tools, such as: VC, SourceInsight and so on.
Select the variable, it will automatically prompt you tell declarations and definitions, so
Hungarian notation is no great need of.
Nothing more than to better readability.
The code is actually good writing habits is more important than the mandatory use of Hungarian notation.
Systematic. Integrity. readability. To clear classification. Have a comment!
Hungarian Notation is a naming convention to promote Microsoft on various types of symbolic variables, functions, objects, prefix, such as macro definition. The main idea of Hungarian notation is: join prefix variable and function names to enhance people's understanding of the program. It was initiated by a Hungarian Microsoft's internal use, the results of which became popular at Microsoft, and to promote the Windows developers around the world. The following describes Hungarian notation, the latter will try to follow its example in the above code and style. Again, not asking all readers have to comply, but I hope the reader as a modern software developers to comply with it.
Prefix | Types of | Chinese Description |
a | Array | Array |
b | BOOL(int) | Boolean (integer) |
by | Unsigned Char(Byte) | Unsigned characters (bytes) |
c | Char | Character (byte) |
cb | Count of Bytes | Byte count |
cr | Color Reference Value | Color (reference) value |
cx | Count of x(Short) | Set x (short integer) |
f | Flags(usually multiple bit values) | Logo (usually there are a number of values) |
fn | Function | function |
g_ | Global | Global |
h | Handle | Handle |
i | Integer | Integer |
l | Long | Long integer |
lp | Long Pointer | Long pointer |
m_ | Data Member of a Class | Data members of a class |
n | Short Integer | Short integer |
p | Pointer | pointer |
s | String | String |
sz | Zero Terminated String | Zero-terminated string |
tm | Text Metric | Text Rules |
in | Unsigned Integer | Unsigned integer |
the | Unsigned Long(ULONG) | Unsigned long integer |
w | WORD(Unsigned Short) | Unsigned short integer |
x, y | x, y Coordinates (Short) | Coordinate value (short integer) |
v | Void | air |
Global variables related projects beginning with g_ class member variables with m_, local variables could be considered if a larger display function with l_ to explain the local variables.
Prefix | Types of | example |
g_ | Global Variables | g_Servers |
C | Class or structure | CDocument, CPrintInfo |
m_ | Member variables | m_pDoc, m_nCustomers |
VC common prefix list:
Prefix | Types of | description | example |
ch | char | 8位字符 | chGrade |
ch | TCHAR | 16位Unicode集字符 | chName |
b | BOOL | 布尔变量 | bEnable |
n | int | 整型 | nLength |
n | UINT | 无符整型 | nLength |
w | WORD | 16位无符号整型 | wPos |
l | Long | 32位有符号整型 | lOffset |
dw | DWORD | 32位无符号整型 | dwRange |
p | * | 指针变量,内存模块指针(Ambient memory model point) | pDoc |
lp | Far* | 长指针 | lpDoc |
lpsz | LPSTR | 32位字符串指针 | lpszName |
lpsz | LPCSTR | 32位常量字符串指针 | lpszName |
lpsz | LPCTSTR | 32位Unicode集常量指针 | lpszName |
h | handle | Windows对象句柄 | hWnd |
lpfn | (*fn)() | 回调函数指针 Callback Far pointer to CALLBACK function | lpfnAbort |
MFC、句柄、控件及结构的命名规范:
Windows类型 | 样本变量 | MFC类 | 样本变量 |
HWND | hWnd | CWnd* | pWnd |
HDLG | hDlg | CDialog* | pDlg |
HDC | hDC | CDC* | pDC |
HGDIOBJ | hGdiObj | CGdiObject* | pGdiObj |
HPEN | hPen | CPen* | pPen |
HBRUSH | hBrush | CBrush* | pBrush |
HFONT | hFont | CFont* | pFont |
HBITMAP | hBitmap | CBitmap* | pBitmap |
HPALETTE | hPaltte | CPalette* | pPalette |
HRGN | hRgn | CRgn* | pRgn |
HMENU | 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 | rect | CRect | rect |
一般前缀命名规范:
前缀 | 类型 | 实例 |
C | 类或结构 | Cdocument, CPrintInfo |
m_ | 成员变量 | m_pDoc, m_nCustomers |
变量命名规范:
前缀 | 类型 | 描述 | 实例 |
ch | char | 8位字符 | chGrade |
ch | TCHAR | 如果_UNICODE定义,则为16位字符 | chName |
b | BOOL | 布尔值 | bEnable |
n | int | 整型(其大小依赖于操作系统) | nLength |
n | UINT | 无符号值(其大小依赖于操作系统) | nHeight |
w | WORD | 16位无符号值 | wPos |
l | LONG | 32位有符号整型 | lOffset |
dw | DWORD | 32位无符号整型 | dwRange |
p | * | 指针 | pDoc |
lp | FAR* | 远指针 | lpszName |
lpsz | LPSTR | 32位字符串指针 | lpszName |
lpsz | LPCSTR | 32位常量字符串指针 | lpszName |
lpsz | LPCTSTR | 如果_UNICODE定义,则为32位常量字符串指针 | lpszName |
h | handle | Windows对象句柄 | hWnd |
lpfn | callback | 指向CALLBACK函数的远指针 |
前缀 | 符号类型 | 实例 | 范围 |
IDR_ | 不同类型的多个资源共享标识 | IDR_MAIINFRAME | 1~0x6FFF |
IDD_ | 对话框资源 | IDD_SPELL_CHECK | 1~0x6FFF |
HIDD_ | 对话框资源的Help上下文 | HIDD_SPELL_CHECK | 0x20001~0x26FF |
IDB_ | 位图资源 | IDB_COMPANY_LOGO | 1~0x6FFF |
IDC_ | 光标资源 | IDC_PENCIL | 1~0x6FFF |
IDI_ | 图标资源 | IDI_NOTEPAD | 1~0x6FFF |
ID_ | 来自菜单项或工具栏的命令 | ID_TOOLS_SPELLING | 0x8000~0xDFFF |
HID_ | 命令Help上下文 | HID_TOOLS_SPELLING | 0x18000~0x1DFFF |
IDP_ | 消息框提示 | IDP_INVALID_PARTNO | 8~0xDEEF |
HIDP_ | 消息框Help上下文 | HIDP_INVALID_PARTNO | 0x30008~0x3DEFF |
IDS_ | 串资源 | IDS_COPYRIGHT | 1~0x7EEF |
IDC_ | 对话框内的控件 | IDC_RECALC | 8~0xDEEF |
应用程序符号命名规范
Microsoft MFC宏命名规范:
名称 | 类型 |
_AFXDLL | 唯一的动态连接库(Dynamic Link Library,DLL)版本 |
_ALPHA | 仅编译DEC Alpha处理器 |
_DEBUG | 包括诊断的调试版本 |
_MBCS | 编译多字节字符集 |
_UNICODE | 在一个应用程序中打开Unicode |
AFXAPI | MFC提供的函数 |
CALLBACK | 通过指针回调的函数 |
库标识符命名法:
标识符 | 值和含义 |
u | ANSI(N)或Unicode(U) |
d | 调试或发行:D = 调试,忽略标识符为发行。 |
静态库版本命名规范:
库 | 描述 |
NAFXCWD.LIB | 调试版本:MFC静态连接库 |
NAFXCW.LIB | 发行版本:MFC静态连接库 |
UAFXCWD.LIB | 调试版本:具有Unicode支持的MFC静态连接库 |
UAFXCW.LIB | 发行版本:具有Unicode支持的MFC静态连接库 |
动态连接库命名规范:
名称 | 类型 |
_AFXDLL | 唯一的动态连接库(DLL)版本 |
WINAPI | Windows所提供的函数 |
Windows.h中新的命名规范:
类型 | 定义描述 |
WINAPI | 使用在API声明中的FAR PASCAL位置,如果正在编写一个具有导出API人口点的DLL,则可以在自己的API中使用该类型 |
CALLBACK | 使用在应用程序回叫例程,如窗口和对话框过程中的FAR PASCAL的位置 |
LPCSTR | 与LPSTR相同,只是LPCSTR用于只读串指针,其定义类似(const char FAR*) |
UINT | 可移植的无符号整型类型,其大小由主机环境决定(对于Windows NT和Windows 9x为32位);它是unsigned int的同义词 |
LRESULT | 窗口程序返回值的类型 |
LPARAM | 声明lParam所使用的类型,lParam是窗口程序的第四个参数 |
WPARAM | 声明wParam所使用的类型,wParam是窗口程序的第三个参数 |
LPVOID | 一般指针类型,与(void *)相同,可以用来代替LPSTR |