SoftEng 版 (精华区)

发信人: lofe ()感激生活(), 信区: SoftEng
标  题: 匈牙利命名法
发信站: 哈工大紫丁香 (Sun Sep  3 17:49:08 2000), 转信

 Hungarian
------------------------------------------------------------------------

While working at Microsoft I learned "Hungarian". This is a convention
for naming identifiers in C (variables, functions etc.). The odd name
derives from the nationality of its inventor, Charles Simonyi, and the
fact that the resulting names are as intelligible to the layman as
Hungarian.

It is invaluable in large programming environments where many people
use and change a single body of code (which may not be the case in a
university research lab, where code is thrown away before it gets a
chance to grow to, say, the size of Word for Windows).

Given an identifier, anyone can precisely determine its type and
purpose. Given a purpose and a type (function, pointer to array of
floats, ...), anyone can unambiguously generate a name or predict what
name somebody else chose. (Close to unambiguously for me on Winword at
least.) Off-by-one errors, type mismatches, and misused pointers are
impossible.

In practice Hungarian becomes what I call "Bulgarian", as base types
and prefixes for a particular project are used. I use "z" for the
float base type, for example. You might use "st" for a stream as in
"FILE* pst = fopen(szFile);". Because we use only 26 letters, extreme
Bulgarian runs into parsing ambiguity. Kanji would be much better for
this!

There is a lengthy document on its use written by Doug Klunder at
Microsoft, which I have misplaced so I can't give it to you though he
might (dougk@microsoft.com). This is my own little summary.

HUNGARIAN SUMMARY
_______________________________
BASE TYPES

f       flag (boolean)
ch      char
st      string (length-prefixed)
sz      NULL-terminated string (ptr to)
fn      function

w       word
b       byte
l       long
u       unsigned word
bit     bit
v       void

env     environment
sb      segment base
ib      offset




_______________________________
PREFIXES MODIFYING BASE TYPES

p       pointer
lp      far ptr
hp      huge ptr
np      near ptr

rg      array
i       index into array
c       count
d       difference
h       handle
hh      huge handle
gr      group (of variable-size objects)
b       byte offset (in a "gr")
mp      map (array) followed by index & value types
dn      array (domain)
e       element (of "dn")        rgx = mpixx   dnx =
mpxex
f       bit flag (of integer)
sh      shift amount
u       union
a       allocation (pointee)
v       global
_______________________________
SUFFIXES MODIFYING A TYPE

First..Last
Lim          Last+1
Min..Max-1
Mic..Mac-1    current Min & Max
Most          Mac-1

Sav           temporary saved value
Nil           illegal value
Null         0, illegal value
T             temporary value
Src/Dest      transfer
_______________________________
FUNCTION NAMES

ValueFromArg1Arg2Arg3
_______________________________
A few examples:

for (int ich=0; ich < cch; ich++) pchDest[ich] = pchSrc[ich];
for (int ich=ichMin; ich < ichMax; ich++) pchDest[ich] = pchSrc[ich];
for (int ich=ichFirst; ich <= ichLast; ich++) pchDest[ich] =
pchSrc[ich];

rgw[iw] = 42;

int fFoundit = (*pfnSearch)(szFile);


夕阳西下
Hungarian

While working at Microsoft I learned "Hungarian". This is a convention
for naming identifiers in C (variables, functions etc.). The odd name
derives from the nationality of its inventor, Charles Simonyi, and the
fact that the resulting names are as intelligible to the layman as
Hungarian.

It is invaluable in large programming environments where many people
use and change a single body of code (which may not be the case in a
university research lab, where code is thrown away before it gets a
chance to grow to, say, the size of Word for Windows).

Given an identifier, anyone can precisely determine its type and
purpose. Given a purpose and a type (function, pointer to array of
floats, ...), anyone can unambiguously generate a name or predict what
name somebody else chose. (Close to unambiguously for me on Winword at
least.) Off-by-one errors, type mismatches, and misused pointers are
impossible.

In practice Hungarian becomes what I call "Bulgarian", as base types
and prefixes for a particular project are used. I use "z" for the
float base type, for example. You might use "st" for a stream as in
"FILE* pst = fopen(szFile);". Because we use only 26 letters, extreme
Bulgarian runs into parsing ambiguity. Kanji would be much better for
this!

There is a lengthy document on its use written by Doug Klunder at
Microsoft, which I have misplaced so I can't give it to you though he
might (dougk@microsoft.com). This is my own little summary.

HUNGARIAN SUMMARY
_______________________________
BASE TYPES

f       flag (boolean)
ch      char
st      string (length-prefixed)
sz      NULL-terminated string (ptr to)
fn      function

w       word
b       byte
l       long
u       unsigned word
bit     bit
v       void

env     environment
sb      segment base
ib      offset




_______________________________
PREFIXES MODIFYING BASE TYPES

p       pointer
lp      far ptr
hp      huge ptr
np      near ptr

rg      array
i       index into array
c       count
d       difference
h       handle
hh      huge handle
gr      group (of variable-size objects)
b       byte offset (in a "gr")
mp      map (array) followed by index & value types
dn      array (domain)
e       element (of "dn")        rgx = mpixx   dnx =
mpxex
f       bit flag (of integer)
sh      shift amount
u       union
a       allocation (pointee)
v       global
_______________________________
SUFFIXES MODIFYING A TYPE

First..Last
Lim          Last+1
Min..Max-1
Mic..Mac-1    current Min & Max
Most          Mac-1

Sav           temporary saved value
Nil           illegal value
Null         0, illegal value
T             temporary value
Src/Dest      transfer
_______________________________
FUNCTION NAMES

ValueFromArg1Arg2Arg3
_______________________________
A few examples:

for (int ich=0; ich < cch; ich++) pchDest[ich] = pchSrc[ich];
for (int ich=ichMin; ich < ichMax; ich++) pchDest[ich] = pchSrc[ich];
for (int ich=ichFirst; ich <= ichLast; ich++) pchDest[ich] =
pchSrc[ich];

rgw[iw] = 42;

int fFoundit = (*pfnSearch)(szFile);


夕阳西下

Variable Naming Convention


   * Use meaningful names for identifiers such as classes, attributes,
member functions, variables, functions, constants, types, etc. As a
general rule, Hungarian notation will be used. However, some
modifications have been made to maintain compatibility with Microsoft
coding conventions.
   * Use of this notation must be consistent. The following prefixes
will be used:

ComboBox cmb
ListBox lst
Button btn
Static Text txt
Group Box grp
CheckBox chk
Radio Button btn
long l
CPoint, Point pt
(or x and y coordinate types)
Char (null term) sz
Cstring class. str
Integer i
Boolean b
Short s
Rectangle RECT
CRect or RECT rect
ColorRef clr
Unsigned integer ui
Handle h
Word w
Pointer p
Far Pointer pf
Byte b
Unsigned Integer n
(counter only)
Window Handle hWnd
Window class cWnd
Class Member Variable m_
Character c
Position Type pos
Void v

Please note that the above list is not complete and will be added to
in each revision of this document. All variables shall start with one
of the above prefixes, followed by their corresponding name which will
start with a capital letter. Variable names are composed of meaningful
components which are abbreviated, followed by the last component which
is spelled out completely.
Examples:
m_pfbTemp A class member which is a far pointer to a Boolean variable.
bContinue A Boolean flag.
ptWinPosition A CPoint type used for a window position. Window has
been abbreviated to Win and Position being the last portion of the
name is spelled out.

The only real exception to the above rule concerns one or two
character variable names which are commonly used as counters. These
include but not limited to n, j, cx, and cy. 


--
※ 修改:.haojs 于 Sep  3 17:46:15 修改本文.[FROM: bbs.hit.edu.cn]
--
※ 转寄:.武汉白云黄鹤站 bbs.whnet.edu.cn.[FROM: bbs.hit.edu.cn]

--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: haojs.bbs@bbs.whnet.]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.597毫秒