HN学习整理

基本原则

变量名 = 属性 + 类型 + 对象描述

名称要求有明确含义,可以去全称或简称,保证名字的连贯性

通用的命名习惯

属性部分

g_ 全局变量
c_ 常量
m_ 类成员变量
s_ 静态变量
类型部分
a 数组
p 指针
fn 函数
v 无效
h 句柄
l 长整型
b 布尔
f 浮点/文件
dw 双字
sz 字符串
n 短整型
d 双精度浮点
c/cnt 计数
c/ch 字符
i 整型
by 字节
w 字
r 实型
u 无符号

举例

hwnd h是类型描述,表示句柄,wnd是变量对象描述,表示窗口,所以hwnd表示串口句柄
pfnEatApple pfn是类型描述,表示指向函数的指针,EatApple是变量对象描述,所以pfnEatApple 表示指向EatApple函数的函数指针变量
g_cch g_是属性描述,表示全局变量,c和ch分别是计数类型和字符类型,一起表示变量类型,所以g_cch表示一个对字符进行计数的全局变量

常见变量命名规范

lpsz LPSTR 32位字符串指针 lpszName
lpsz LPCSTR 32位常量字符串指针 lpszName
lpsz LPCTSTR 如果_UNICODE定义,则为32位常量字符串指针 lpszName
h handle Windows对象句柄 hWnd
• g_nWheels : member of a global namespace, integer
• m_nWheels : member of a structure/class, integer
• m_wheels, _wheels : member of a structure/class
• s_wheels : static member of a class
• c_wheels : static member of a function
• bBusy : boolean
• chInitial : char
• cApples : count of items
• dwLightYears : double word (Systems)
• fBusy : float (or flag)
• nSize : integer (Systems) or count (Apps)
• iSize : integer (Systems) or index (Apps)
• fpPrice: floating-point
• dbPi : double (Systems)
• pFoo : pointer
• rgStudents : array, or range
• szLastName : zero-terminated string
• u16Identifier : unsigned 16-bit integer (Systems)
• u32Identifier : unsigned 32-bit integer (Systems)
• stTime : clock time structure
• fnFunction : function name

根据前缀的使用习惯不同,分为systems notation和Apps Notation
In Systems Hungarian notation, the prefix encodes the actual data type of the variable.
Apps Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable’s purpose is, or what it represents.

习惯命名:

Systems notation
• lAccountNum : variable is a long integer (“l”);
• arru8NumberList : variable is an array of unsigned 8-bit integers (“arru8”);
• szName : variable is a zero-terminated string (“sz”); this was one of Simonyi’s original suggested prefixes.
• bReadLine(bPort,&arru8NumberList) : function with a byte-value return code.
Apps notation
• rwPosition : variable represents a row (“rw”);
• usName : variable represents an unsafe string (“us”), which needs to be “sanitized” before it is used
• strName : Variable represents a string (“str”) containing the name, but does not specify how that string is implemented.

参考资料
百度百科 http://baike.baidu.com/view/419474.htm
Wikipedia https://en.wikipedia.org/wiki/Hungarian_notation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值