CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  VB.NET

VB.net 编码规范

楼主Ninputer(装配脑袋)2002-02-03 21:37:44 在 .NET技术 / VB.NET 提问

在开发中保持良好的编码规范是十分重要的。我所采用的新的VB.net编码规范,是一种被证明能明显改善代码可读性,并有助于代码管理、分类的编码规范。采用这种编码规范,能避免如匈牙利命名法带来的繁长前缀,便于记忆变量的用途。下面的介绍这种编码规范。  
  一、类型级单位的命名  
  1、类。  
  以Class声明的类,都必须以名词或名词短语命名,体现类的作用。如:  
  Class   Indicator  
  当类是一个特性(Attribute)时,以Attribute结尾,当类是一个异常(Exception)时,以Exception结尾:  
  Class   ColorSetException  
  Class   CauseExceptionAttribute  
  当类只需有一个对象实例(全局对象,比如Application等),必须以Class结尾,如  
  Class   ScreenClass  
  Class   SystemClass  
  当类只用于作为其他类的基类,根据情况,以Base结尾:  
  MustInherit   Class   IndicatorBase  
  如果定义的类是一个窗体,那么名字的后面必须加后缀Form,如果是Web窗体,必须加后缀Page:  
  Class   PrintForm   :   Inherits   Form   '*   Windows窗体  
  Class   StartPage   :   Inherits   Page   '*   Web窗体  
  2、枚举和结构  
  同样必须以名词或名词短语命名。最好体现枚举或结构的特点,如:  
  Enum   ColorButtons   '以复数结尾,表明这是一个枚举  
  Structure   CustomerInfoRecord   '以Record结尾,表明这是一个结构体  
  3、委派类型  
  普通的委派类型以描述动作的名词命名,以体现委派类型实例的功能:  
  Delegate   Sub   DataSeeker   (ByVal   SeekString   As   String)  
  用于事件处理的委派类型,必须以EventHandler结尾,如:  
  Delegate   Sub   DataChangedEventHandler   (ByVal   Sender   As   Object,   ByVal   e   As   DataChangedEventArgs)  
  4、接口  
  与其他类型不同,接口必须要由I作为前缀,并用形容词命名,突出表现实现接口的类将具有什么能力:  
  Interface   ISortable  
  5、模块  
  模块不是类型,他的名称除了必须以名词命名外,必须加以后缀Module:  
  Module   SharedFunctionsModule  
  上述所有规则的共同特点是,每个组成名称的词语都必须是大写开头,禁止完全大写或小写的名称。  
  二、方法和属性的命名  
  1、方法  
  无论是函数还是子程序,方法都必须以动词或动词短语命名。无需区分函数和子程序,也无需指明返回类型。  
  Sub   Open(ByVal   CommandString   As   String)  
  Function   SetCopyNumber(ByVal   CopyNumber   As   Integer)  
  参数需要指明ByVal还是ByRef,这一点写起来会让程序边长,但非常必要。如果没有特别情况,都使用ByVal。参数的命名方法,参考后面“变量的命名方法”。需要重载的方法,一般不写Overloads,根据需要编写重载的方法。  
  2、属性  
  原则上,字段(Field)是不能公开的,要访问字段的值,一般使用属性。属性以简洁清晰的名词命名:  
  Property   Concentration   As   Single  
  Property   Customer   As   CustomerTypes  
  3、事件  
  事件是特殊的属性,只能在事件处理上下文中使用。命名的原则一般是动词或动词的分词,通过时态表明事件发生的时间:  
  Event   Click   As   ClickEventHandler  
  Event   ColorChanged   As   ColorChangedEventHangler  
  三、变量和常数  
  常数以表明常数意义的名词命名,一般不区分常数的类型:  
  Const   DefaultConcentration   As   Single   =   0.01  
  在严格要求的代码中,常数以c_开头,如c_DefaultConcentration,但最好不要用它,它会带来输入困难。  
  普通类型的变量,只要用有意义的名字命名即可,不可使用简称和无意义的名称诸如A,x1等,下面给出了良好的例子:  
  Dim   Index   As   Integer  
  Dim   NextMonthExpenditure   As   Decimal  
  Dim   CustomerName   As   String  
  不能起太长的名字,应该尽量简洁,如下面的例子:  
  Dim   VariableUsedToStoreSystemInformation   As   String   '*   错误,太复杂了  
  Dim   SystemInformation   As   String   '*   正确,简单明了  
  Dim   sysInfo   As   String   '*   错误,过于简单  
  特殊情况可以考虑一个字母的变量:  
  Dim   g   As   Graphic  
  对于控件,应该指明控件的类型,方法是直接在变量后面加以类名:  
  Friend   WithEvents   NextPageButton   As   Button   '*   按钮  
  Friend   WithEvents   ColorChoicerPanel   As   Panel   '*   面版  
  Friend   WithEvents   CardFileOpenDialog   As   FileOpenDialog   '*   文件打开对话框  
  等等,无需规定某种类型的变量的前缀,只需把类型写在后面就行了,试对比下列代码:  
  btnCancel.Text   =   "&Cancel"  
  CancelButton.Text   =   "&Cancel"  
  显然后者更能使阅读者明白变量的类型是一个按钮。  
  四、标签  
  标签就是用于Goto跳转的代码标识,由于Goto并不推荐使用,所以标签的使用也比较苛刻。标签必须全部大写,中间的空格用下划线_代替,而且应该以_开头,比如:  
  _A_LABEL_EXAMPLE:  
  如此定义标签是为了与其他代码元素充分区别。  
  五、名字空间  
  通常,一个工程使用一个名字空间,通常不需要用Namespace语句,而是在工程选项的“Root   Namespace”中指定,使用根名字空间可以使代码更加整齐,容易修改,这一点是VB十足的优点。名字空间的语法是:  
  公司名.产品名[.组件名的复数]  
  如:  
  Namespace   Ninputer.VirtualScreen  
  Namespace   Ninputer.CardEditor.CustomeControls  
  随便起一个名字空间的名字绝对不是一个好主意,一定要遵守上述规定。  
  六、注释  
  注释的规则繁多,这里仅提到其中一点:正常的注释以'*开头,单独的'只用来注释暂时不用的代码  
  '*   这是普通的注释  
  '*   这段代码在调试正确后加入  
  'If   UseHighSpeed(g)   =   True   Then   ....  
  这样能够方便的采用代码注释工具控制代码的使用。  
  以上已经简单的介绍了我使用的VB.net代码规范,这个代码规范也适用于C#。仅供大家参考。  
  问题点数:59、回复次数:7Top

1 楼chjcwl(PBVC)回复于 2002-02-03 21:39:34 得分 10

谢谢,收藏先!Top

2 楼mwenyuan(William Ma)回复于 2002-02-03 23:17:32 得分 10

好,另存为。。。Top

3 楼topcool(狂热者)回复于 2002-02-04 08:46:02 得分 10

好的,先收藏!Top

4 楼AdamBear(亚当熊)回复于 2002-02-04 21:06:34 得分 10

      微软的标准也不一定是最好的标准,没有好的标准还不如没有标准。  
        下面是大师Bruce   Mickenney的话:  
  I’m   not   trying   to   evangelize   Hungarian.   Everyone   I   know   who   uses   it   (including   me)   hated   it   at   first.   It   just   grows   on   you.   Maybe   it   will   grow   on   you   enough   to   make   you   a   convert   during   the   course   of   this   book—or   maybe   it   won’t.   In   any   case,   being   able   to   read   Hungarian   is   a   skill   you   won’t   regret   acquiring.   If   you   haven’t   really   understood   the   point   of   the   snippets   of   Hungarian   code   you   have   seen   in   various   Microsoft   manuals,   here’s   a   brief   introduction   that   will   make   reading   the   sample   code   easier.  
   
  Long-time   Microsoft   developer   Charles   Simonyi,   who   happens   to   be   Hungarian   by   birth,   developed   the   convention.   That—along   with   the   fact   that   C   code   written   in   this   style   looks   like   foreign   gibberish   to   the   uninitiated—prompted   the   name.   The   idea   (simplified   to   a   point   that   would   probably   horrify   Simonyi)   is   that   variables   should   consist   of   two   parts:   a   lowercase   base   type   indicating   the   kind   of   variable   and   an   initial-cap   qualifier   that   distinguishes   this   variable   from   others   of   the   same   kind.  
   
  For   example,   an   integer   that   keeps   track   of   the   file   position   would   have   the   base   type   i   for   index   and   the   qualifier   Pos   to   form   the   variable   iPos.   If   you   must   keep   track   of   both   a   file   position   and   a   line   position   in   the   same   context,   you   need   to   qualify   further:   iFilePos   and   iLinePos.   If   you   were   creating   a   Project   Save   As   dialog   box,   you   might   call   it   FProjectSaveAs   and   fill   it   with   controls   such   as   cboFiles,   cboDirs,   lstFileTypes,   lstDrives,   cmdOk,   cmdCancel,   and   cmdNetwork.   If   you   had   an   array   of   buttons   to   activate   different   windows,   the   base   type   cmd   wouldn’t   be   enough,   so   you   could   modify   it   with   the   array   prefix   a,   as   in   acmd­Window.   To   access   this   array,   you   might   need   a   count   variable   showing   the   number   of   windows,   cWindow,   and   an   index   to   the   current   window,   iWindow­Cur.   In   a   small   function   using   only   one   local   index   variable,   you   don’t   need   a   qualifier—just   call   it   i.  
   
  This   doesn’t   begin   to   touch   on   the   complexity   of   the   original   Hungarian   convention.   In   addition,   the   whole   idea   has   been   bastardized.   At   least   three   incompatible   official   dialects   of   Hungarian   are   used   by   C   programmers   at   Microsoft,   and   now   the   Visual   Basic   documentation   group   has   introduced   their   own   variation   of   Hungarian.   Unfortunately,   the   crudest   of   these   variations   is   the   one   used   in   the   Windows   Software   Development   Kit   (SDK),   and   it   is   now   spreading   confusion   to   the   world.   In   a   few   short   years,   the   Hungarian   coding   convention   has   evolved   as   much   as   natural   languages   evolve   in   a   thousand   years.  
   
  Compare,   for   example,   the   naming   convention   in   the   Windows   SDK   Help   file   with   the   one   in   the   Visual   Basic   API   Help   file   shipped   with   Visual   Basic   version   3.   (If   you   don’t   remember   version   3,   never   mind.)   Both   files   are   aimed   at   C   programmers—the   first   at   those   writing   Windows-based   programs   in   C,   and   the   second   at   those   writing   VBX   controls   in   C.   You’d   expect   both   files   to   use   the   same   convention,   but   the   names   for   similar   variables   are   in   fact   very   different,   although   both   systems   are   vaguely   recognizable   as   Hungarian.  
   
  In   the   SDK,   for   example,   a   Boolean   variable   has   the   prefix   b   for   Boolean.   In   the   Visual   Basic   API,   a   Boolean   variable   has   the   prefix   f   for   flag.   In   the   SDK,   a   variable   used   as   a   bit   flag   has   the   prefix   w   or   dw   for   Word   or   DWord,   indica­ting   its   type—or   at   least   the   Windows   include   file   version   of   its   type.   In   the   Visual   Basic   API,   a   similar   variable   has   the   prefix   fs   or   fl   for   flag   of   short   or   flag   of   long,   respectively,   indicating   both   its   use   and   its   type.   This   goes   on.   Windows   SDK   names   sometimes   indicate   the   use   of   the   variable,   but   more   often   they   simply   indicate   the   data   type,   and   even   then   in   an   artificial   form   that   has   no   relation   to   Basic   (or   to   C,   for   that   matter).  
   
  Alas,   the   version   of   Hungarian   used   in   the   Visual   Basic   documentation   is   a   cousin   of   the   Windows   version.   It   uses   prefixes   based   on   the   types   (lng   for   Long,   str   for   String,   sng   for   Single,   and   so   on).   Worse,   it   uses   generic   prefixes   for   different   kinds   of   multimember   types   rather   than   specific   prefixes   for   each   type.   For   example,   you’ll   see   frm   for   all   forms,   cls   for   all   classes,   and   udt   for   all   user-defined   types.   I   invented   my   version   of   Hungarian   before   all   the   others,   so   don’t   ask   me   to   copy   them.  
  Top

5 楼Ninputer(装配脑袋)回复于 2002-02-07 13:08:18 得分 0

我的标准并不是微软的标准。Top

6 楼Wuxyingshu(无影石)回复于 2002-02-10 14:39:43 得分 10

你的QQ多少?Top

7 楼wyvernwhite()回复于 2002-02-12 20:37:53 得分 9

我也想要:-)Top

相关问题

  • 编码规范
  • 哪里有VB的编码规范文档?
  • * * * * * 高分求VB程序开发的编码规范 * * * * *
  • -------------asp编码规范--------------
  • java编码规范
  • 关于java编码规范
  • 软件编码规范
  • 征求javascript编码规范
  • 昨天买了本《C++编码规范》
  • 到哪里找编码规范????

关键词

  • vb.net
  • 命名
  • 代码
  • 编码
  • 属性
  • vb
  • 接口
  • 变量
  • 类型
  • 编码规范

得分解答快速导航

  • 帖主:Ninputer
  • chjcwl
  • mwenyuan
  • topcool
  • AdamBear
  • Wuxyingshu
  • wyvernwhite

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo