c语言变量命名规则较多较乱Clanguagevariablenamingmorechaos.docx
《c语言变量命名规则较多较乱Clanguagevariablenamingmorechaos.docx》由会员分享,可在线阅读,更多相关《c语言变量命名规则较多较乱Clanguagevariablenamingmorechaos.docx(7页珍藏版)》请在冰豆网上搜索。
c语言变量命名规则较多较乱Clanguagevariablenamingmorechaos
c语言变量命名规则_较多较乱(Clanguagevariablenaming_morechaos)
c语言变量命名规则_较多较乱(Clanguagevariablenaming_more
chaos)
Afewyearsago,CharlesSimonyi(wholaterbecameafamousprogrammerofMicrosoft)isdesignedbyusingaprefixbasednamingmethod,thismethodwascalledthe"Hungariannotation"tocommemoratehim.Histhoughtisaccordingtoeachidentifiermeanstogiveitaprefix.Microsoftlateradoptedthisideaforeachidentifieraprefixtoillustrateitsdatatype.Therefore,theprefixnisinteger,longintegervariableisNL,charactertypearrayvariableisCa,andthestring(endwithemptycharacterarraytype)withSZastheprefix.Thesenamesmaybeverystrange.Forexample:
lpszFoo"Foo"isapointertoanullterminatedstringforthelongpointer.
Theadvantageofthismethodistoenablepeopletopassthenameofthevariabletoidentifythetypeofavariable,ratherthantofindthedefinitionofit.Unfortunately,thismethodnotonlymakesthevariablenameverychatty,buttochangethevariabletypeworkhasbecomeverydifficult.InWindows3.1,thewholetypevariableto16wideif.Weuseanintegervariableatthebeginning,butafterthecalculationby30---40function,usingtheintegervariablewidthisnotenough,thenweshouldnotonlychangethetypeofthevariable,butalsotochangethenameofthisvariableinthe30--40function.
Becauseofunrealistic,exceptforsomestubbornYuanwaiWindowsprogramhasnoonetousethe"Hungariannotation".Thereisnodoubtthatinsomeoccasions,itstillexists,butmostpeoplehavenowabandonedit.Ingeneral,theinputprefixisabadidea,becauseitputthevariableinitstypetightly
tiedtogether.
Hungarianmethodsgenerallyhaveadvantagesoverfunctionsthatarelessthan30lines.
Especiallyforinterfaceprogramming,ithasadvantages.
ButtheHungarianapproachisadisasterforalgorithmsthathavestrongcomputationalrequirements,especiallywithmanyabstracttypesofC++.
Lookwhereyou'reusingit.
NowtherearegoodIDEtools,suchasVC,SourceInsight,andsoon
Theselectedvariablewillautomaticallypromptyouforitsdeclarationanddefinition,sothat
Hungariannomenclatureisnotverynecessary
It'sjustforthereadabilityoftheprogram
Infact,goodcodewritinghabitsaremoreimportantthanforcingHungariannomenclature
Systematicness。
Entirety。
Readability.Classificationshould
beclear.Theremustbenotes!
HungariannomenclatureisaMicrosoftnamingspecificationforvarioustypesofsymbolssuchasvariables,functions,objects,
prefixes,macros,andsoon.ThemainideaofHungariannomenclatureistoaddprefixestovariablesandfunctionnamestoimprovepeople'sunderstandingofprograms.ItwaslaunchedbyaHungarianwithinMicrosoft,andasaresultitbecamepopularwithinMicrosoftandpromotedtoWindowsdevelopersaroundtheworld.TheHungariannomenclatureisfollowed,followedbythefollowingexampleandthestyleofthecodeabove.It'snotthatallreadersaregoingtofollowit,butreadersareexpectedtofollowitasamodernsoftwaredeveloper.
AArrayarray
B,BOOL(int),Boolean(integer)
By,Unsigned,Char(Byte)unsignedcharacters(bytes)
CCharcharacters(bytes)
CB,Count,of,bytesbytes
Cr,Color,reference,valuecolor(Reference)values
CXCountofX(Short)xcollection(shortinteger)
DWDWORD(unsigned,long)doubleword(unsignedlonginteger)
FFlags(usually,multiple,bit,values)flags(usuallywithmultiplevalues)
FNFunctionfunction
G_globalglobal
HHandlehandle
IIntegerinteger
LLonglonginteger
LPLongpointerlongpointer
M_Data,member,of,a,class,datamembersofaclass
NShortintshortinteger
指针ppoint
sstring字符串
以0结尾的字符串'szeroterminatedstring
文本规则tmtextmetric
the无符号整数unsignedint
unsignedlong(ulong)无符号长整数ul
wword(unsignedshort)无符号短整数
x,y,x,ycoordinates(short)坐标值/短整数
for空v
有关项目的全局变量用g_开始,类成员变量用m_,局部变量若函数较大则可考虑用l_用以显示说明其是局部变量.
前缀类型例子
(g_全局变量g_servers
(c类或者结构体cdocument,cprintinfo
mmm_成员变量_ncustomers_73.
vc常用前缀列表.
前缀类型描述例子
c.8位字符chgradetank
tchar16位unicode类型字符chnamech
(bbool布尔变量benabled
整型(其大小由操作系统决定)nlengthnint
nouint无符号整型nlength(其大小由操作系统决定)
wword16位无符号整型wpos
thelong32位有符号整型loffset
32位无符号整型dwrangedwdword
p*eup内存模块指针memorymodel,指针变量73
in长指针lpdoc*lp
lpszlpstr32位字符串指针lpszname
lpszlpcstr32位常量字符串指针lpszname
lpszlpctstr32位unicode类型常量指针lpszname
inwindows对象句柄hwndh
lpfn(fn)()回调函数指针callbackfarpointertocallback
functionlpfnabort
windows对象名称缩写.
windows对象例子变量mfc类例子对象
hwndhwnd;cwnd*pwnd.
hdlghdlg;cdialog*pdlg.
hdchdc;cdc*pdc.
hgdiobjhgdiobj;cgdiobject*pgdiobj.
hpenhpen;cpen*nwpp.
hbrushhbrush;cbrush*pbrush.
hfonthfont;cfont*pfont.
hbitmaphbitmap;cbitmap*pbitmap.
hpalettehpalette;cpalette*ppalette.
hrgnhrgn;crgn*prgn.
hmenuhmenu;cmenu*pmenu.
hwndhctl;cstatic*pstatic.
hwndhctl;cbutton*pbtn.
hwndhctl.this*pedit.
hwndhctl;clistbox*plistbox.
hwndhctl;ccombobox*pcombobox.
vc常用宏定义命名列表.
前缀符号类型符号例子范围
a_标识多个资源共享的类型a_mainframe1~0x6fff
对话框资源._(dialog)._spell_~0x6fff.
hidd_基于对话框的上下文帮助hidd_0x20001~0x26ff_spell
check
idbidb_位图资源(bitmap)_~0x6fff_companylogo
IDC_cursorresource(Cursor)IDC_PENCIL1~0x6FFF
IDI_iconresource(Icon)IDI_NOTEPAD1~0x6FFF
ThecommanditemID_TOOLS_SPELLING0x8000~0xDFFFoftheID_orIDM_toolbarormenubar
TheHID_commandcontexthelpsHID_TOOLS_SPELLING0x18000~0x1DFFF
TheIDP_messageboxpromptsthetextresource
IDP_INVALID_PARTNO8~0xDFFF
TheHIDP_messageboxcontexthelpsHIDP_INVALID_PARTNO0x30008~0x3DFFF
IDS_stringresource(String)IDS_COPYRIGHT1~0x7FFF
ThecontrolresourceIDC_RECALC8~0xDFFFintheIDC_dialogbox
MicrosoftMFCmacronamingspecification
Nametype
_AFXDLLtheonlydynamiclinklibrary(Dynamic,Link,Library,DLL)version
_ALPHAcompilesonlytheDECAlphaprocessor
_DEBUGincludesdiagnosticdebugversion
_MBCScompilesamultibytecharacterset
_UNICODEopensUnicodeinanapplication
FunctionsprovidedbyAFXAPIMFC
CALLBACKfunctionsthatarecallbackbypointers
Libraryidentifiernaming
Identifiervaluesandmeanings
UANSI(N)orUnicode(U)
Ddebugorrelease:
D=debug;ignoreidentifierforrelease
Staticlibraryversionnamingspecification
Librarydescription
NAFXCWD.LIBdebugversion:
MFCstaticconnectionLibrary
NAFXCW.LIBrelease:
MFCstaticconnectionLibrary
UAFXCWD.LIBdebugversion:
MFCstaticconnectionlibrarywithUnicodesupport
UAFXCW.LIBrelease:
aMFCstaticconnectionlibrarywithUnicodesupport
Dynamicconnectionlibrarynamingspecification
Nametype
_AFXDLL'sonlydynamicconnectionLibrary(DLL)version
ThefunctionsprovidedbyWINAPIWindows
NewnamingconventionsinWindows.h
Typedefinitiondescription
WINAPIusestheFARPASCALlocationintheAPIdeclaration,andifyouarewritingaDLLthathastheexportpopulationpointofAPI,youcanusethistypeinyourownAPI
CALLBACKusesthelocationoftheFARPASCALintheapplicationcallbackprogram,suchaswindowsanddialogs
LPCSTRisthesameasLPSTR,exceptthatLPCSTRisusedasaread-onlystringpointer,anditsdefinitionissimilar(const,char,FAR*)
UINTportableunsignedintegertypewhosesizeisdeterminedbythehostenvironment(forWindows,NT,andWindows9xis32bit);itissynonymouswithunsignedint
ThetypeofthereturnedvalueoftheLRESULTwindowprogram
LPARAMdeclaresthetypeusedbylParam,andlParamisthefourthparameterofthewindowprogram
WPARAMdeclaresthetypeusedbywParam,andwParamisthethirdparameterofthewindowprogram
LPVOIDgeneralpointertype,thesameas(void*),canbeusedinsteadofLPSTR
InMSDN,acodereferencethatfollowsthecodestyleandHungariannomenclatureisprovidedasfollows:
1#include"sy.h""
2externint*rgwDic;
3externintbsyMac;
4,struct,SY,*PsySz(char,sz[])
{6
7char*pch;
8intcch;
9,struct,SY,*psy,*PsyCreate();
10int*pbsy;
11intcwSz;
12unsignedwHash=0;
13pch=sz;
14while(*pch,=0)
15wHash=wHash<>11+*pch++;
16cch=pch-sz;
17pbsy=&rgbsyHash[(wHash&077777)%cwHash];
18for((*pbsy)=0;pbsy=&psy->bsyNext)
19{
20char*szSy;
21szSy=(psy=(struct,SY*)&rgwDic[*pbsy])->sz;
22pch=sz;
23while(*pch==*szSy++)
24{
25if(*pch++==0)
26return(psy);
27}
28}
29cwSz=0;
30if(cch>=2)
31cwSz=cch-2/sizeof(int)+1;
32×pbsy=(int*)(PSY=psycreate(cwsy+cwsz))-rgwdic;
33零((int*)PSY,cwsy);
34bltbyte(SZ,PSY->SZ,CCH+1);
35回(PSY);
36}