Coding | specification
vb.net Coding Specification (also applicable to C #)Ninputer (original) keyword vb.net coding specification
It is important to maintain good coding standards in the development process. The new vb.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, the end of the attribute, when the class is an exception (Exception), ends with Exception:
Class colorsetexception
Class Causeexceptionattribute
When a class has 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, ends 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 added:
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 used 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, 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.
Five, 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 Ninputer.virtualscreen
Namespace Ninputer.CardEditor.CustomeControls
It is definitely not a good idea to name a name space, and you must comply with the above rules.
Vi. notes
There are a variety of rules for annotations, only one of which is mentioned here: 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 (g) = True Then ....
In this way, the Code annotation tool can be used to control the use of the code conveniently.
The above is a simple introduction to the VB.net code specification that I use, and this code specification is also applicable to C #. For your reference only.
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.