VB coding specification

zhaozj2021-02-16  67

IDE settings

The "Requirements Variable Declaration", "Align Controls to Grid" must be opened, "Auto Indent" Switch.

The width of the Tab is unified to 4 spaces, and the grid unit is set to: Width 50 HEIGHT 50.

name

engineering

ActiveX controls and DLL engineering naming formats are (....lib), exe project directly, if it is a general component engineering, direct naming, if it is a project or product engineering, use items or product abbreviations as a prefix. Such as: xwy ... .lib.

Engineering naming does not have to abbreviate, in order to express and use, you can use it as long as possible, and naming formats (nouns), (adjective nouns) or (noun form of noun). Such as: xwystockoperationlib or xwystocklib.

(Note: At any time, do not use Chinese naming, including folders, file names, function names, variable names. Unless the file needs to interact with users!)

variable

Variable naming is not recommended to adopt Hungarian nomenclature, unless the naming will have conflicts with the keywords, the Hungarian nomenclature of the real name of the type abbreviation variable is used. Under normal circumstances, variable naming should be simple, try to use abbreviations.

If it is a general value type, such as Integer String, use the variable use to name it, try to use the full name:

DIM Name as String

DIM Count As Interger

For general temporary variable definitions, it should be as simple as possible, such as:

DIM I as integer

For i = 0 to 100

Next I

If you are a class object or a custom type object, name the class name or custom type name in a single usage:

DIM EM As EnityManager

If you are not used, use the type name abbreviation as a prefix, that is, Hungarian nomenclature:

DIM EMREAD AS EntityManager

DIM EMSAVE AS EntityManager

(Note: All prefixs are all lowercase, the back of the word first letters)

The abbreviation rules are as follows:

If the name consists of multiple words, take the first letter of each word, such as EntityManager abbreviation as EM, procedureManager abbreviation as PM.

If the name consists of a word, the first letter is taken on the word, such as Entity abbreviations.

Abbreviations should be controlled within 3 letters, as clear as possible, for the interface name, I ... I am prefixed in the IAntration, such as the name of IEntity, should be considered Entity.

Unless the first letter is a vowel, it should take a consonant as an abbreviation, such as the quote prefix of the TextBox control is TXT.

Range Identification:

Global variable plus prefix: 'g_'

Module-level variable prefix: 'm_'

Process grade variables do not prefix

Global variables and modular variables should try to use the full name, not recommended to use abbreviations, such as g_entityManager

Control

Control Naming Use Control Type Abbreviation Control Use Named Mode, Type Abbreviation should be controlled within 3 letters, and the abbreviation rules are named with variables. The following is the type ablation of common controls, should be strict, if you use new controls, First, it should be used in the group to consistent with the type name abbreviation.

cmb Combo box chk Checkbox cmd Command button dlg Common dialog controldt DTPicker DropDateControlenm EnumEditBoxfra Frame frm Form gra Graph grd EditGrid MSHFlexGrid FlexGrid DataGridimg Image ImageList lab Label ln Line lst List box lv ListViewmnu Menu control nm NumEditBoxopt Option button pic Picture rpt Report sbr Scroll bar shp Shape SPN Spin St Statusbar TB ToolbartMR Timer Txt TextBoxTV TreeView Function

Here are the functions of SUB and Function, and the following two processes are collectively referred to as functions.

The function represents an action, so its structure should be a verb noun, the verb must be lowercase, the name of the name of the next letter, such as:

GetMaterialCodeUpdategridReadOrder

Function Naming Try not to use abbreviations, and its name should make people clear, can know the functionality of this function from the name, do not use meaningless function names, such as: getCode (when this function belongs to the MATERAIL class, it still has Significant), Update, ReadData.

When the function name is not sufficient to express its function, use the annotation that makes the caller enough to understand.

Naming of parameters: The principle of parameter naming is all lowercase, if the parameter includes two or more words, the first word letters lowercase, other words first letters, such as Showcol, iSupdate.

constant

The naming of constants should be all capitalized, using '_' as separators between words, words with all the names, such as:

Public const msg_empty_row as string = "There is a space exist!"

Explanation:

(1) Should be used for some common words, such as MSG

(2) Declaring variables using public instead of earlier versions of Global

(3) The statement on constants must bring type, as String above

Attributes

The naming of the attribute uses the principle of the first letter, such as Itemcount Item

Class, form and module

Naming use of class, do not have to add any prefix and suffix, and the first letter capitalization, such as: SystemConfig

Form name use function noun form suffix, such as: Listform. But for documents, uniform use of Detail suffix replacement FORM module naming: Don't add any prefix and suffix, direct naming

Custom control

Custom control naming: Noun Ctrl

Estitgridctrl

format

definition

The defined code block should be put together, try not to define variables in the middle, and the definition of variables should be enjoys, and cannot be indented. At the same time, the "AS" keyword is aligned, as follows:

DIM I as integer

DIM J AS INTEGER

DIM EM As EntityManager

The definition of the object should be used as much as possible to the library name to prevent the name conflict later, such as reference two libs, each containing a stock class, if not using the as ... .lib.stock definition method, you cannot Compilation pass, in order to prevent new library bands to naming conflicts in order to expose the program expansion and modification, it is recommended to add library ID when defining a class object, and the definition of the class object of this project is also added, such as:

DIM EM As ObjectPersistenceLib.entityManager

Blank line

The blank line is the interval between code blocks and blocks, and there must be a space line (two lines), and the function is inside, the variable declaration block, and the implementation block (other code to implement the block refer to variable declaration) To use Interval (one line), implement the interior of the block, identify a function segment through the blank line, such as:

Private subick (Order as NysaleBacklib.Order)

'* decrease stock

Dim objstockItem as nysoftlib.stockItem

Dim objstock as nysoftlib.stock

DIM I as integer

Set objstock = createstock ()

For i = 0 to order.Itemcount - 1

Set objstockItem = Order.Item (i)

'* decrease stock

Call Objstock.ReduceItem (ObjstockItem, True)

Next I

Set objstock = Nothing

End Sub

(Note: Do not use too much blank line, empty, too much influence code reading!)

indentation

The indentation must be strictly enforced, and the variable declaration block does not indent, and the implementation block must ensure that all indentation (ie, it is impossible to implement the block is aligned).

For basic control structures, there must be indentation, such as: if, do, with, for, open, select block, and indentation examples:

... ..

IF .......

... ..

END IF

... ..

(Note: In any place, don't write an elseif statement, convert to an IF..EELSE..EELSE..endif structure)

For too long, you must use the continuation, and the continuation location must be obvious, examples:

SQL = "SELECT [CODE], [Name] from [Person]" _

& "Where [code] like '001%'"

If the parameter of the function is too long, it should also be continued, examples: '**

'Increase inventory

'@Param ProductCode product number

'@Param SPEC length specification

'@Param color Color

'@PARAM PATCH is a ring

'@Param Volumn disk number

'@Param Ordinal subventory sequence number

'@Param Length Length

'@Param Ischeck is an increase in the warehouse (otherwise adding the audio)

Public Sub Adddddtail (ProductCode As String, _

Spec as double, _

Color as string, _

Patch as boolean, _

Volumn as string, _

Ordinal as integer, _

Length as double, _

Ischeck as boolean)

Comment

the amount

Note To make it right as possible, but you must do what you mean by reading your code, let the caller understand the expression priority principles of the function functionality as follows:

(1) Expression by function name

(2) Expression by code

(3) Expression by comments

It is known that the comment is provided when the code cannot fully express function functions, and the comment should also be accurate and concise.

format

The format of the annotation follows the Writing of VBDoCMAN, in general, using the VBDOCMAN's annotation editor to annotate writing, which can be noted for obvious parameters or function functions. The parameter type in the parameter comment is not.

Example:

'**

'Read document information

'@Param ORDERID document number

'@Param ORDER document

Private Function Readorder (Order AS String, Order As NysaleBacklib.Order) AS Boolean

END FUNCTION

At the top of each code module (form, class, module, control), you must write the code to write people (using English name or Chinese Pinyin abbreviation), code creation time, code modification time, and modification.

Example:

'**

'Inventory modification

'@writer pureach

'@createdate 2003-11-12

'@Revision Pureach 2003-11-15

'Increase the function of simultaneously affecting the final warehipment date

What is a good code?

(1) Code format with strong readability, distinguishes different code blocks

(2) Clear naming, deliver enough information in length as possible

(3) Note to the code (do not let the information you can expressed by the note)

(4) The survival of the variable is as short as possible, so that readers don't need to remember a lot of variable declarations.

(5) Use small functions to separate functional complicated large functions

转载请注明原文地址:https://www.9cbs.com/read-22401.html

New Post(0)