Vb.net/asp.net Coding Specification (ZT)

Source: Internet
Author: User
Tags date exit constant datetime functions goto variables web services
asp.net| Coding | It is very important to maintain a good coding standard in development. The new vb.net/asp.net coding specification that I have adopted is a coding specification that has been shown to significantly improve code readability and help in code management and categorization. By using this coding specification, we can avoid the long prefix of the Hungarian nomenclature, so as to facilitate the use of memory variables. The following is an introduction to this coding specification.




First, the name of the type-level unit

1, class

-classes declared by class, must be named after the noun or noun phrase, reflecting the role of the class. Such as:

Class indicator
-When a class is an attribute, ending with an attribute, ending with a Exception when the class is an exception (Exception):

Class colorsetexception

Class Causeexceptionattribute
-When a class needs only one object instance (global object, such as application, etc.), it must end with class, as

Class Screenclass

Class Systemclass
-When a class is used only as a base class for other classes, as appropriate, end with base:

MustInherit Class Indicatorbase
-If the defined class is a form, then the name must be followed by the suffix form, and if it is a Web form, the suffix page must be appended:

Class printform:inherits form ' * Windows Form

Class startpage:inherits Page ' * Web Form



2, enumeration and structure

It must also be named after a noun or noun phrase. It is best to embody the characteristics of an enumeration or structure, such as:

Enum Colorbuttons ' ends with a complex number, indicating that this is an enumeration

Structure Customerinforecord ' ends with a record, indicating that this is a structural body


3. Delegate type

-The normal delegate type is named for the noun that describes the action to reflect the functionality of the delegate type instance:

Delegate Sub Dataseeker (ByVal seekstring as String)
-The delegate type for event handling must end with EventHandler, such as:

Delegate Sub Datachangedeventhandler (ByVal Sender as Object, ByVal e as Datachangedeventargs)


4, interface

Unlike other types, an interface must be prefixed by I, named after an adjective, highlighting the capabilities of the class that implements the interface:

Interface isortable


5. Module

A module is not a type, except that his name must be named after a noun, which must be suffix module:

Module Sharedfunctionsmodule

The common feature of all of the above rules is that each constituent name must begin with a capital letter, prohibiting all uppercase or lowercase names.




Ii. nomenclature of methods and properties

1. Methods

Either a function or a subroutine, the method must be named with a verb or a verb phrase. You do not need to differentiate between functions and subroutines, and you do not need to indicate the return type.

Sub Open (ByVal commandstring as String)
Function Setcopynumber (ByVal copynumber as Integer)

Parameters need to indicate whether ByVal or ByRef, which is written to make the program side long, but very necessary. If there is no special case, use ByVal. The named method of the parameter, referring to the "named method of the variable" later. Overloaded methods are required, generally do not write overloads, as needed to write overloaded methods.


2, properties

In principle, fields (field) are not exposed, and you want to access the values of fields, typically using properties. Attributes are named with concise and clear nouns:

Property concentration as single
Property Customer as Customertypes


3. Events

Events are special properties that can only be used in the context of event handling. The principle of nomenclature is usually the participle of a verb or a verb, which indicates the time of occurrence of the event:

Event Click as ClickEventHandler
Event colorchanged as Colorchangedeventhangler




Iii. Variables and constants

Constants are named for nouns that indicate constant meaning, and generally do not distinguish between types of constants:

Const defaultconcentration as Single = 0.01

In strict code, constants start with c_, such as c_defaultconcentration, but it is best not to use it, it will bring difficulty in input.

Ordinary types of variables, as long as the use of meaningful names can not use abbreviations and meaningless names such as a,x1, the following gives a good example:

Dim Index as Integer
Dim Nextmonthexpenditure as Decimal

Dim CustomerName as String

Cannot get too long a name, should be as concise as possible, as the following example:

Dim variableusedtostoresysteminformation as String ' * error, too complicated

Dim systeminformation as String ' * correct, straightforward

Dim SysInfo as String ' * error, too simple

Special circumstances may consider a variable of a letter:

Dim G as Graphic

For a control, you should indicate the type of the control by directly following the variable with the class name:

Friend WithEvents Nextpagebutton as button ' * buttons
Friend WithEvents Colorchoicerpanel as Panel ' * Face edition
Friend WithEvents Cardfileopendialog as fileopendialog ' * File Open dialog box

And so on, you don't have to specify the prefix for some type of variable, just write the type back, and try to compare the following code:

Btncancel.text = "&cancel"
Cancelbutton.text = "&cancel"

Obviously the latter can make the reader understand that the type of the variable is a button.





Four, prefix



1. Object

1) Standard Object

Name
Prefix
Example
Description

System.Array
Arr
Arrusers
User Collection

System.Boolean
bln
Blndoesuserexist
Whether the user exists

System.Byte
Byt
Bytstreamcontent
Word Throttling Content

System.Char
Chr
Chrkeypress
Keys

System.DateTime
DtE
Dtecreateddatetime
Date Created

System.Decimal
Dec
Decyearlysalequota
Annual Sales

System.Double
Dbl
Dbltotalprice
Total Amount

System.interger
Int
Intmessages
Number of messages

System.Object
Obj
Objexternalfunction
External features

System.Single
Sng
Sngfinishrate
Completion rate

System.String
Str
Strloginname
Login Name

System.Exception
Exc
Excret
Error

System.Enum
Enm
Enmuserstates
User status

Structure
Stu
Stuemployees
Employee type

System.Data.SqlClient.SqlConnection
Cnn
Cnndatabase
Database connection

System.Data.SqlClient.SqlCommand
Cmm
Cmmuseraddupdate
Users to add updates

System.Data.SqlClient.SqlDataAdapter
Sda
Sdausers
User Data Adapters

System.Data.SqlClient.SqlDataReader
Sdr
Sdruserdata
User Data reader










2) Custom Objects

We stipulate that the prefix of the object type should be determined according to the name of the custom object, as follows:

Objects: Sysset

Prefix: ss

Example: Sssafety



2. Depending on the life cycle of the variable and the constant, we should define a different life cycle prefix to differentiate so that we know the scope of the variable/constant.

A) classes, modules, components, controls

We stipulate that in the scope of classes, modules, components, controls, the life cycle prefix of a variable should be added "M_" (module-module). Examples are as follows:

Name
Prefix
Example
Description

System.Array
M_arr
M_arrusers
User Collection

System.Boolean
M_bln
M_blndoesuserexist
Whether the user exists

System.Byte
M_byt
M_bytstreamcontent
Word Throttling Content

System.Char
M_chr
M_chrkeypress
Keys

System.DateTime
M_dte
M_dtecreateddatetime
Date Created

System.Decimal
M_dec
M_decyearlysalequota
Annual Sales

System.Double
M_dbl
M_dbltotalprice
Total Amount

System.interger
M_int
M_intmessages
Number of messages

System.Object
M_obj
M_objexternalfunction
External features

System.Single
M_sng
M_sngfinishrate
Completion rate

System.String
M_str
M_strloginname
Login Name

System.Exception
M_exc
M_excret
Error

System.Enum
M_enm
M_enmuserstates
User status

Structure
M_stu
M_stuemployees
Employee type

System.Data.SqlClient.SqlConnection
M_cnn
M_cnndatabase
Database connection

System.Data.SqlClient.SqlCommand
M_cmm
M_cmmuseraddupdate
Users to add updates

System.Data.SqlClient.SqlDataAdapter
M_sda
M_sdausers
User Data Adapters

System.Data.SqlClient.SqlDataReader
M_sdr
M_sdruserdata
User Data reader












b procedures, functions, properties, events

We stipulate that in the scope of procedures, functions, properties, events, the life cycle prefix of a variable should be added "O_" (owner-private). Examples are as follows:

Name
Prefix
Example
Description

System.Array
O_arr
O_arrusers
User Collection

System.Boolean
O_bln
O_blndoesuserexist
Whether the user exists

System.Byte
O_byt
O_bytstreamcontent
Word Throttling Content

System.Char
O_chr
O_chrkeypress
Keys

System.DateTime
O_dte
O_dtecreateddatetime
Date Created

System.Decimal
O_dec
O_decyearlysalequota
Annual Sales

System.Double
O_dbl
O_dbltotalprice
Total Amount

System.interger
O_int
O_intmessages
Number of messages

System.Object
O_obj
O_objexternalfunction
External features

System.Single
O_sng
O_sngfinishrate
Completion rate

System.String
O_str
O_strloginname
Login Name

System.Exception
O_exc
O_excret
Error

System.Enum
O_enm
O_enmuserstates
User status

Structure
O_stu
O_stuemployees
Employee type

System.Data.SqlClient.SqlConnection
O_cnn
O_cnndatabase
Database connection

System.Data.SqlClient.SqlCommand
O_cmm
O_cmmuseraddupdate
Users to add updates

System.Data.SqlClient.SqlDataAdapter
O_sda
O_sdausers
User Data Adapters

System.Data.SqlClient.SqlDataReader
O_sdr
O_sdruserdata
User Data reader
















Five, label

tags are used for goto jump code identification, because Goto is not recommended, so the use of tags is also more stringent. The label must be all uppercase, the middle space should be underlined _ instead, and should start with _, for example:

_a_label_example:

This defines the label so that it is sufficiently different from other code elements.




Six, the name space

Usually, a project uses a namespace, usually does not need to use the Namespace statement, but in the engineering option "root Namespace" in the specified, use the root namespace can make the code more neat, easy to modify, this is the full advantages of VB. The syntax for namespaces is:

Company name. Product name [. complex number of component names]

Such as:

Namespace com.net
Namespace COM. File.IO.Files

It is definitely not a good idea to name a name space, and you must comply with the above rules.





Vii. formatting

Good formatting code is good for our browsing and maintenance.

1, Block

. NET provides #region ... #End Region block control. We should classify and organize the pieces according to the functions that the code implements.



2. Indent

Each level should be indented directly with tab, not space (SPACEBAR).



3, Flow

Each method, function, property, event should have and only one entry and one exit. If you meet multiple layers of nesting and need to jump straight out, use local boolean or integer variables to indicate that you want to jump out of multiple layers.

Such as:

Private Function Testforreturn () as Boolean

Dim O_blnret as Boolean=false

Dim i As Integer, L As Integer

For I=1 to 100

For L=1 to 10

If (x) Then

O_blnret=true

Exit for

Else

End If

Next

If O_blnret Then

Exit for

Else

End If

Next

Return O_blnret ' This is the only exit

End Function






Viii. annotations

1. Guidelines

A wide range of rules for annotations, basically: normal annotations start with ' *, separate ' to annotate temporarily unused code

' * It's an ordinary note
' * This code is added after the debugging is correct
' If usehighspeed (i) = True Then ....

In this way, the Code annotation tool can be used to control the use of the code conveniently.



2, class (including WebForm, WinForm, etc.), modules, components, controls

The object's information must be entered at the very beginning of each class, module, component, control, and the sample content and format are as follows:

'******************************************************************

'*

' * Object name: Onlineupdateservice

' * Namespaces: Http://www.domain.cn/onlineupdate/OnlineUpdateService.asmx

' * Author: CHS (Wilson Chan)

' * Function Description: Online Update Web Services

' * Date Created: 2003/07/27

' * Modified Date: 2003/09/5 16:59

' * Modify the record:

' *-2003/07/31

' * 1.addupdateusercategory/deleteusercategory

' * 2.addupdateuserstate/deleteuserstate

' * 3.addupdateserverupdate/deleteserverupdate

' * 4.addupdateclientupdate/deleteclientupdate

' * 5.addupdateclientupdatelog/deleteclientupdatelog

' * 6.getserverupdates/getclientupdates/getclientupdatelogs

'*

'******************************************************************

In general, we require that the content be: Object name, namespace, author, function description, creation date, modification date, change record, in particular, we request that the change record should describe the recent modification in detail.



3, methods, functions, events, and properties

Each method, function, event, and property must enter information for that object at the very beginning, and the sample content and format are as follows:

' * Name: onlineupdatefileaddupdate

' * Function: Add change update information

' * Author: CHS

' * Created: 2003/7/31

' * Modified: 2003/7/31

' * parameter: IntID: Unique indicator

' * Strtitle: Title

' * Strversion: Version

' * Strverifier: Logo

' * strdescription: Description

' * Strrelatedfile: Related documents

' * strupdatepackage: Download package

' * Intstate: State

' * Dtecreateddatetime: Date Created

' * Return: Success, True, vice versa, False

' * Description:

Generally, we require the content to be: Name, function, author, description, creation, modification, parameter and return.







Viii. Integrity

1, If ... Else ... End If

2. Select case ... Case Else ... End Select





IX. Safety

1. Try ... Catch... Finally ... End Try







Ten, optimize

1, with

2, for ... Next

3, do Until ... Loop

4, Code refactoring







Xi. Other

1, shortcut keys

2,





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.