中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 登陆资料
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 未整理篇 | 技术讨论
VB.NET/ASP.NET编码规范
作者:未知 时间:2004-12-15 12:12 出处:互联网 责编:chinaitpower
              摘要:暂无

VB.NET/ASP.NET编码规范

 

在开发中保持良好的编码规范是十分重要的。我所采用的新的VB.NET/ASP.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"

显然后者更能使阅读者明白变量的类型是一个按钮。

 

 

四、前缀

 

1、 对象

1)标准对象

名称

前缀

例子

说明

System.Array

arr

arrUsers

用户集合

System.Boolean

bln

blnDoesUserExist

用户是否存在

System.Byte

byt

bytStreamContent

字节流内容

System.Char

chr

chrKeyPress

按键

System.DateTime

dte

dteCreatedDateTime

创建日期

System.Decimal

dec

decYearlySaleQuota

年度销售额

System.Double

dbl

dblTotalPrice

总金额

System.Interger

int

intMessages

消息数

System.Object

obj

objExternalFunction

外部功能

System.Single

sng

sngFinishRate

完成率

System.String

str

strLoginName

登陆名称

System.Exception

exc

excRet

错误

System.Enum

enm

enmUserStates

用户状态

Structure

stu

stuEmployees

员工类型

System.Data.SqlClient.SqlConnection

cnn

cnnDatabase

数据库连接

System.Data.SqlClient.SqlCommand

cmm

cmmUserAddUpdate

用户添加更新

System.Data.SqlClient.SqlDataAdapter

sda

sdaUsers

用户数据适配器

System.Data.SqlClient.SqlDataReader

sdr

sdrUserData

用户数据读取器

 

 

 

 

 

        2)自定义对象

        我们规定应该根据自定义对象的名称来确定该对象类型的前缀,例子如下:

        对象:SysSet

        前缀:ss

        例子:ssSafety

 

    2、根据变量与常量的生存周期,我们应该定义不同的生存周期前缀以示区别,以便我们清楚该变量/常量的范围。

a)      类、模块、组件、控件

我们规定在类、模块、组件、控件范围内,变量的生存周期前缀应该添加“m_”(Module-模块)。例子如下:

名称

前缀

例子

说明

System.Array

m_arr

m_arrUsers

用户集合

System.Boolean

m_bln

m_blnDoesUserExist

用户是否存在

System.Byte

m_byt

m_bytStreamContent

字节流内容

System.Char

m_chr

m_chrKeyPress

按键

System.DateTime

m_dte

m_dteCreatedDateTime

创建日期

System.Decimal

m_dec

m_decYearlySaleQuota

年度销售额

System.Double

m_dbl

m_dblTotalPrice

总金额

System.Interger

m_int

m_intMessages

消息数

System.Object

m_obj

m_objExternalFunction

外部功能

System.Single

m_sng

m_sngFinishRate

完成率

System.String

m_str

m_strLoginName

登陆名称

System.Exception

m_exc

m_excRet

错误

System.Enum

m_enm

m_enmUserStates

用户状态

Structure

m_stu

m_stuEmployees

员工类型

System.Data.SqlClient.SqlConnection

m_cnn

m_cnnDatabase

数据库连接

System.Data.SqlClient.SqlCommand

m_cmm

m_cmmUserAddUpdate

用户添加更新

System.Data.SqlClient.SqlDataAdapter

m_sda

m_sdaUsers

用户数据适配器

System.Data.SqlClient.SqlDataReader

m_sdr

m_sdrUserData

用户数据读取器

 

 

 

 

 

 

b)      过程、函数、属性、事件

我们规定在过程、函数、属性、事件范围内,变量的生存周期前缀应该添加“o_”(Owner-私有)。例子如下:

名称

前缀

例子

说明

System.Array

o_arr

o_arrUsers

用户集合

System.Boolean

o_bln

o_blnDoesUserExist

用户是否存在

System.Byte

o_byt

o_bytStreamContent

字节流内容

System.Char

o_chr

o_chrKeyPress

按键

System.DateTime

o_dte

o_dteCreatedDateTime

创建日期

System.Decimal

o_dec

o_decYearlySaleQuota