




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
编程中的命名设计那点事2009年6月7日 Neo 发表评论 阅读评论 6,069 次点击 在我开始设计系统的时候,我会花去很多时间去设计命名,因为好的命名和好的设计是分不开的。In the beginning was the Word, and the Word was with God, and the Word was God太初有道。道与神同在,道就是神。 (约翰福音第一章,第一节)在设计过程中给类,方法和函数好的命名会带来好的设计,虽然这不是一定成立,但是如果坏的命名那一定不会给你带来好的设计。在设计过程,如果你发现你很难命名某一个模块,某个方法时,可能你真正遇到的问题不是难命名的问题,而是这个设计是否真的合理,你或许应该花更多的时间来重新设计一下你的模块。好的命名不仅会带来好的设计,好的命名还提高了程序的可读性,降低代码维护的成本。另一方面,如果糟糕的命名会给代码带来一堵无形的墙,让你必须深入代码去研究代码具有的行为,增加你理解代码的时间。为此我总结了几条关于命名的指导原则,希望这几条原则能为你的命名设计带来帮助,我使用的是C+的语法,当然这些原则也很容易扩展到其他语言中去。类型命名(类,接口,和结构)名字应该尽量采用名词Bad:HappyGood:Happiness不要使用类似名字空间的前缀Bad:SystemOnlineMessageGood:System:Online:Message形容词不要用太多,能描述清楚就行Bad:IAbstractFactoryPatternBaseGood:IFactory在类型中不要使用Manager 或则 Helper 或则其他没意义的单词如果你一定要在一个类型上加上Manager或Helper,那么这个类型要么就是命名的非常糟糕,要么就是设计的非常糟糕,如果是后则,那么这个类型就应该管理manage和帮助help一下自己了。Bad:ConnectionManagerXmlHelperGood:ConnectionXmlDocument, XmlNode, etc.如果某个类不能通过简单的命名来描述它具有的功能,可以考虑用类比的方式来命名Bad:IncomingMessageQueueCharacterArraySpatialOrganizerGood:MailboxStringMap如果你使用类比,你就应该一致的使用它们Bad:Mailbox,DestinationIDGood:Mailbox,Address函数(方法和过程)简洁Bad:list.GetNumberOfItems()Good:list.Count()不要太简洁Bad:list.Verify()Good:list.ContainsNull()避免缩写Bad:list.Srt()Good:list.Sort()对于完成某件事情的函数使用动词Bad:obj.RefCount();Good:list.Clear();list.Sort();obj.AddReference();对于返回布尔型的函数,使用类似提问的方式Bad:list.Empty();Good:list.IsEmpty();list.Contains(item);对于只是返回属性,而不改变状态的函数则使用名词Bad:list.GetCount();Good:list.Count();不要在函数名字中重复参数的名称Bad:list.AddItem(item);handler.ReceiveMessage(msg);Good:list.Add(item);handler.Receive(msg);不要方法的名字中重复此方法的类的名称Bad:list.AddToList(item);Good:list.Add(item);不要在函数的名字中加入返回类型,除非函数名必须以返回类型进行区别Bad:list.GetCountInt();Good:list.GetCount();message.GetIntValue();message.GetFloatValue();不要名字中使用And 或则 Or如果你使用一个连接词来连接函数名,那么这个函数肯定是做了太多的事情,更好的做法是将其分成更小的函数来处理(类似面向对象设计准则中的责任单一原则)。如果你想确保是这是一个原子的操作,那么你应该用一个名字来描述这个操作或一个类来封装他Bad:mail.VerifyAddressAndSendStatus();Good:mail.VerifyAddress();mail.SendStatus();这是一篇非常优秀的文章,我用我的语言在组织了一下,如果喜欢英文的读者可以点击这里阅读原文Windows系统目录常见的简写环境变量所谓的Windows系统目录简写环境变量,就是为一些常用且固定的Windows系统目录的路径建立一个与之对应的相对简单的缩写,使之更容易输入或定位。Windows系统默认情况下都是安装在电脑C盘的Windows目录下,但这并不是固定的,如果你的系统不是安装在这个目录下,那么程序想要定位你的某个系统目录的话,就需要使用到目录简写环境变量了。使用这些环境变量,程序员或系统管理员无需事先了解你的系统安装位置,就能轻易的找到所要使用的系统目录路径。下面,我就来为大家介绍一些常见的Windows系统目录简写环境变量:%SYSTEMDRIVE%这代表的是Windows系统所在磁盘分区,也就是Windows系统所安装到的盘符根目录,通常就是C盘的根目录了。%HOMEDRIVE%这和上面介绍的%SYSTEMDRIVE%的功能是一样的。%SYSTEMROOT%它所指向的是Windows系统所在的目录,通常就是C:Windows。%WINDIR%和%SYSTEMROOT%的功能相同,指向Windows所在目录。%ProgramFiles%指向Program Files的路径,通常情况下是C:Program Files。%CommonProgramFiles%指向公用文件(Common Files)目录,通常是C:Program FilesCommon Files。%USERPROFILE%指向当前帐户的用户目录,通常是C:Documents and Settings当前用户名。%HOMEPATH%功能和上面的%USERPROFILE%是一样的。%ALLUSERSPROFILE%指向所有用户的用户目录,通常是C:Documents and SettingsAll Users。%APPDATA%指向当前用户的Application Data目录,通常是C:Documents and Settings当前用户名Application Data。很多文章都会介绍一个叫做%ALLAPPDATA%的变量,说它是指向C:Documents and SettingsAll UsersApplication Data的,但其实这个变量并不能使用。%TEMP%它指向的是当前用户的临时文件目录,通常是C:Documents and Settings当前用户名Local SettingsTemp。%TMP%与%TEMP%的指向相同。%ComSpec%指向C:WINDOWSSystem32cmd.exe,也就是命令提示符。Visual C+入门精解(一)续Programming 2007-04-15 09:26:52 阅读182 评论1 字号:大中小订阅 VISUAL C+ 下的数据类型类型含义ATOMAtom. For more information, see Atoms.BOOLBoolean variable (should be TRUE or FALSE).BOOLEANBoolean variable (should be TRUE or FALSE).BYTEByte (8 bits).CALLBACKCalling convention for callback functions.CHAR8-bit Windows (ANSI) character. For more information, see Character Sets Used By Fonts.COLORREFRed, green, blue (RGB) color value (32 bits). See COLORREF for information on this type.CONSTVariable whose value is to remain constant during execution.DWORD32-bit unsigned integer.DWORD_PTRUnsigned long type for pointer precision. Use when casting a pointer to a long type to perform pointer arithmetic. (Also commonly used for general 32-bit parameters that have been extended to 64 bits in 64-bit Windows. )DWORD3232-bit unsigned integer.DWORD6464-bit unsigned integer.FLOATFloating-point variable.HACCELHandle to an accelerator table.HANDLEHandle to an object.HBITMAPHandle to a bitmap. HBRUSHHandle to a brush.HCONVHandle to a dynamic data exchange (DDE) conversation.HCONVLISTHandle to a DDE conversation list.HCURSORHandle to a cursor.HDCHandle to a device context (DC).HDDEDATAHandle to DDE data.HDESKHandle to a desktop.HDROPHandle to an internal drop structure.HDWPHandle to a deferred window position structure.HENHMETAFILEHandle to an enhanced metafile.HFILEHandle to a file opened by OpenFile, not CreateFile.HFONTHandle to a font.HGDIOBJHandle to a GDI object.HGLOBALHandle to a global memory block.HHOOKHandle to a hook.HICONHandle to an icon.HIMAGELISTHandle to an image list.HIMCHandle to input context.HINSTANCEHandle to an instance.HKEYHandle to a registry key.HKLInput locale identifier.HLOCALHandle to a local memory block.HMENUHandle to a menu.HMETAFILEHandle to a metafile.HMODULEHandle to a module. The value is the base address of the module.HMONITORHandle to a display monitor.HPALETTEHandle to a palette.HPENHandle to a pen. HRGNHandle to a region.HRSRCHandle to a resource.HSZHandle to a DDE string.HWINSTAHandle to a window station.HWNDHandle to a window.INT32-bit signed integer.INT_PTRSigned integral type for pointer precision. Use when casting a pointer to an integer to perform pointer arithmetic.INT3232-bit signed integer.INT6464-bit signed integer.LANGIDLanguage identifier. For more information, see Locales.LCIDLocale identifier. For more information, see Locales.LCTYPELocale information type. For a list, see Locale and Language Information.LONG32-bit signed integer. LONG_PTRSigned long type for pointer precision. Use when casting a pointer to a long to perform pointer arithmetic.LONG3232-bit signed integer.LONG6464-bit signed integer.LONGLONG64-bit signed integer.LPARAMMessage parameter.LPBOOLPointer to a BOOL. LPBYTEPointer to a BYTE. LPCOLORREFPointer to a COLORREF value.LPCRITICAL_SECTIONPointer to a CRITICAL_SECTION.LPCSTRPointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.LPCTSTRAn LPCWSTR if UNICODE is defined, an LPCTSTR otherwise.LPCVOIDPointer to a constant of any type.LPCWSTRPointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.LPDWORDPointer to a DWORD.LPHANDLEPointer to a HANDLE.LPINTPointer to an INT.LPLONGPointer to a LONG.LPSTRPointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.LPTSTRAn LPWSTR if UNICODE is defined, an LPSTR otherwise.LPVOIDPointer to any type.LPWORDPointer to a WORD.LPWSTRPointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.LRESULTSigned result of message processing.LUIDLocally unique identifier. PBOOLPointer to a BOOL.PBOOLEANPointer to a BOOL.PBYTEPointer to a BYTE.PCHARPointer to a CHAR.PCRITICAL_SECTIONPointer to a CRITICAL_SECTION.PCSTRPointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.PCTSTRA PCWSTR if UNICODE is defined, a PCSTR otherwise.PCWCHPointer to a constant WCHAR.PCWSTRPointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. PDWORDPointer to a DWORD.PFLOATPointer to a FLOAT.PHANDLEPointer to a HANDLE.PHKEYPointer to an HKEY.PINTPointer to an INT. PLCIDPointer to an LCID.PLONGPointer to a LONG.PLUIDPointer to a LUID. POINTER_3232-bit pointer. On a 32-bit system, this is a native pointer. On a 64-bit system, this is a truncated 64-bit pointer. POINTER_6464-bit pointer. On a 64-bit system, this is a native pointer. On a 32-bit system, this is a sign-extended 32-bit pointer.PSHORTPointer to a SHORT.PSTRPointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.PTBYTEPointer to a TBYTE.PTCHARPointer to a TCHAR.PTSTRPWSTR if UNICODE is defined, a PSTR otherwise.PTBYTEPointer to a TBYTE.PTCHARPointer to a TCHAR.PTSTRA PWSTR if UNICODE is defined, a PSTR otherwise.PUCHARPointer to a UCHAR.PUINTPointer to a UINT.PULONGPointer to a ULONG.PUSHORTPointer to a USHORT.PVOIDPointer to any type.PWCHARPointer to a WCHAR.PWORDPointer to a WORD.PWSTRPointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.REGSAMSecurity access mask for registry key.SC_HANDLEHandle to a service control manager database. For more information, see SCM Handles.SC_LOCKHandle to a service control manager database lock. For more information, see SCM Handles.SERVICE_STATUS_HANDLEHandle to a service status value. For more information, see SCM Handles.SHORTShort integer (16 bits).SIZE_TThe maximum number of bytes to which a pointer can point. Use for a count that must span the full range of a pointer.SSIZE_ TSigned SIZE_T.TBYTEA WCHAR if UNICODE is defined, a CHAR otherwise.TCHARA WCHAR if UNICODE is defined, a CHAR otherwise.UCHARUnsigned CHAR.UINTUnsigned INT.UINT_PTRUnsigned INT_PTR.UINT32Unsigned INT32.UINT64Unsigned INT64.ULONGUnsigned LONG.ULONG_PTRUnsigned LONG_PTR.ULONG32Unsigned LONG32.ULONG64Unsigned LONG64.ULONGLONG64-bit unsigned integer.UNSIGNEDUnsigned attribute.USHORTUnsigned SHORT.VOIDAny type.WCHAR16-bit Unicode character. For more information, see Character Sets Used By Fonts.WINAPICalling convention for system functions.WORD16-bit unsigned integer.WPARAMMessage parameter.Visual C+入门精解(一)Programming 2007-04-15 09:25:21 阅读138 评论0 字号:大中小订阅 第一部分匈牙利命名法规则一般情况下,变量的取名方式为: + + 。范围前缀_,类型前缀_,限定词。特殊的类型命名,前缀表示:类、接口前缀类型例子备注LmClass LmObject表示类型本身不与范围前缀结合使用IInterface 接口IUnknown注:类名前缀改为Lm,对于非全局的类最好有语义表示其所属模块。类的实例命名与类名大致相同,只是类名语义表示类的通用含义,而类名表示此实例的具体语义。如类名LmSketPoint表示草图点的类定义,而它的两个实例 _StartPoint,_EndPoint分别代表起点和终点的语义。类的实例命名带上前缀_。特殊约定:a MouseTool的派生类的前缀为_Mt.b 对话框类的前缀为CDlg.c 橡皮条类的前缀为_Rb.凡围前缀:前缀类型例子备注g_全局作用域g_Serversm_成员变量m_pDoc, l_局部作用域l_strName少用注:编程时尽量少用全程变量,对于全程变量还应在类型前缀后加上如下关键字:特征模块 : Fea草图模块 : Sket装配模块 : Asm工程图模块: Lay曲面模块 : Surf界面模块 : Ui常用的一般数据类型的前缀前缀类型内存规格描述例子chchar8-bit characterchGradechTCHAR16-bit character if _UNICODE is definedchNamebBOOLBoolean valuebEnablednintInteger (size dependent on operating system)nLengthnUINTUnsigned value (size dependent on operating system)nLengthwWORD16-bit unsigned valuewPoslLONG32-bit signed integerlOffsetdwDWORD32-bit unsigned integerdwRangep*Ambient memory model pointerpDoclpFAR*Far pointerlpDoclpszLPSTR32-bit pointer to character stringlpszNamelpszLPCSTR32-bit pointer to constant character stringlpszNamelpszLPCTSTR32-bit pointer to constant character string if _UNICODE is definedlpszNamehhandleHandle to Windows objecthWndlpfn(*fn)()callbackFar pointer to CALLBACK functionlpfnAbort常用Windows对象名称缩写Windows 对象例子变量MFC类例子对象HWNDhWnd;CWnd*pWnd;HDLGhDlg;CDialog*pDlg;HDChDC;CDC*pDC;HGDIOBJhGdiObj;CGdiObject*pGdiObj;HPENhPen;CPen*pPen; 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;CEdit*pEdit; HWNDhCtl;CListBox*pListBox;HWNDhCtl;CComboBox*pComboBox;Visual C+常用宏定义命名列表前缀符号类型符号例子范围IDR_标识多个资源共享的类型IDR_MAINFRAME1 to 0x6FFFIDD_对话框资源(Dialog)IDD_SPELL_CHECK1 to 0x6FFFIDB_位图资源(Bitmap)IDB_COMPANY_LOGO1 to 0x6FFFIDC_光标资源(Cursor)IDC_PENCIL1 to 0x6FFFIDI_图标资源(Icon)IDI_NOTEPAD1 to 0x6FFFID_IDM_工具栏或菜单栏的命令项ID_TOOLS_SPELLING0x8000 to 0xDFFFHID_命令上下文帮助(Command Help context)HID_TOOLS_SPELLING0x18000 to 0x1DFFFIDP_消息框提示文字资源IDP_INVALID_PARTNO8 to 0xDFFFHIDP_消息框上下文帮助(Message-box Help context)HIDP_INVALID_PARTNO0x30008 to 0x3DFFFIDS_字符串资源(String)IDS_COPYRIGHT1 to 0x7FFFIDC_对话框内的控制资源(Control)IDC_RECALC8 to 0xDFFFa Array 数组b BOOL (int) 布尔(整数)by Unsigned Char (Byte) 无符号字符(字节)c Char 字符(字节)cb Count of bytes 字节数cr Color reference value 颜色(参考)值cx Count of x (Short) x的集合(短整数)dw DWORD (unsigned long) 双字(无符号长整数)f Flags (usually multiple bit values) 标志(一般是有多位的数值)fn Function 函数g_ global 全局的h Handle 句柄i Integer 整数l Long 长整数lp Long pointer 长指针m_ Data member of a class 一个类的数据成员n Short int 短整数p Pointer 指针s String 字符串sz Zero terminated String 以0结尾的字符串tm Text metric 文本规则u Unsigned int 无符号整数ul Unsigned long (ULONG) 无符号长整数w WORD (unsigned short) 无符号短整数x,y x, y coordinates (short) 坐标值/短整数v void 空有关项目的全局变量用g_开始,类成员变量用m_,局部变量若函数较大则可考虑用l_用以显示说明其是局部变量。前缀 类型 例子g_ 全局变量 g_ServersC 类或者结构体 CDocument,CPrintInfom_ 成员变量 m_pDoc,m_nCustomersVC常用前缀列表:前缀 类型 描述 例子ch char 8位字符 chGradech TCHAR 16位UNICODE类型字符 chNameb BOOL 布尔变量 bEnabledn int 整型(其大小由操作系统决定) nLengthn UINT 无符号整型(其大小由操作系统决定) nLengthw WORD 16位无符号整型 wPosl LONG 32位有符号整型 lOffsetdw DWORD 32位无符号整型 dwRangep * Ambient memory model pointer 内存模块指针,指针变量 pDoclp FAR* 长指针 lpDoclpsz LPSTR 32位字符串指针 lpszNamelpsz LPCSTR 32位常量字符串指针 lpszNamelpsz LPCTSTR 32位UNICODE类型常量指针 lpszNameh handle Windows对象句柄 hWndlpfn (*fn)() 回调函数指针 Callback Far pointer to CALLBACK function lpfnAbortWindows对象名称缩写: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 hPalette; CPalette* pPalette; HRGN hRgn; CRgn* pRgn; HMENU hMenu; CMenu* pMenu; HWND hCtl; CStatic* pS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 民舞课件教学课件
- 内江人和国有资产经营有限责任公司2025年公开招聘工作人员的模拟试卷带答案详解
- 民法绿色原则课件
- 民法总则课件钟秀勇
- 难点详解青岛版9年级数学下册期末测试卷及答案详解
- 民法学硕课件
- 难点解析京改版数学8年级上册期中试题及答案详解(各地真题)
- PRMT5-MTA-IN-5-生命科学试剂-MCE
- 3-5-Dimethyl-4-hydroxybenzonitrile-d8-生命科学试剂-MCE
- 内科护理(中级)考试彩蛋押题附答案详解【黄金题型】
- 外墙仿石漆合同协议书
- 股票代持合同协议书
- 广东2025年广东省特种设备检测研究院第一批招聘笔试历年参考题库附带答案详解
- 2025新人教版英语八上单词默写表(先鸟版)
- 起重吊装安全专项施工方案方案
- 基层卫生岗位(社区护理组)练兵和能竞赛试题
- 反恐安全风险评估记录
- 【图文】个人简历模板大全-可直接下载使用
- DB42∕T 1049-2015 房产测绘技术规程
- 2025年美术作品授权合同协议
- 构建学校与家庭共育的信息化平台研究
评论
0/150
提交评论