Standard - Visual Basic Coding Convention PDF
Standard - Visual Basic Coding Convention PDF
Code 09fe-HD/PM/HDCV/FSOFT
Version 1/1
RECORD OF CHANGE
10/06/13 3.1.3 Suggested A added new "File Send Fax, Format V1/1
Prefixes for Menus Character, Help Contents"
10/06/13 3.1.7 Constant A,M Added new ". Although standard V1/1
Visual Basic constants do not
include Hungarian information,
prefixes like int, str,g and m can be
very useful in understanding the
value and scope of a constant. For
constant names, follow the same
rules as variables"; Updates
Example "Public Const
gstrDATE_FORMAT as String =
“DD/MM/YYYY”
09fe-HD/PM/HDCV/FSOFT 2/26
Standard_Visual Basic Coding Convention V1/1
TABLE OF CONTENTS
4. DATABASE ................................................................................................................ 21
09fe-HD/PM/HDCV/FSOFT 3/26
Standard_Visual Basic Coding Convention V1/1
6. TIP .............................................................................................................................. 23
6.4. Multital……………………………………………………………………………………………25
6.5. DLL’s and OCX’s ......................................................................................................... 25
09fe-HD/PM/HDCV/FSOFT 4/26
Standard: Visual Basic Coding Convention V1/1
1. INTRODUCTION
1.1. Purpose
Purpose of this document is to provide Coding Convention for all developers in all Visual Basic
projects for our group.
The main reason for using a consistent set of coding conventions is to standardize the
structure and coding style of an application so that you and others can easily read and
understand the code.
Good coding conventions result in precise, readable, and unambiguous source code that is
consistent with other language conventions and as intuitive as possible.
Fsoft projects
1 04e-QT/PM/HDCV/FSOFT Process_Coding
1.4. Definition
Terminology Explanation
VB Visual Basic
09fe-HD/PM/HDCV/FSOFT 5/26
Standard_Visual Basic Coding Convention V1/1
2. GENERAL
09fe-HD/PM/HDCV/FSOFT 6/26
Standard_Visual Basic Coding Convention V1/1
3. DETAIL
3.1.1. Modules
Package IDX_xxxx
09fe-HD/PM/HDCV/FSOFT 7/26
Standard_Visual Basic Coding Convention V1/1
09fe-HD/PM/HDCV/FSOFT 8/26
Standard_Visual Basic Coding Convention V1/1
09fe-HD/PM/HDCV/FSOFT 9/26
Standard_Visual Basic Coding Convention V1/1
MS Chart ch chSalesbyRegion
09fe-HD/PM/HDCV/FSOFT 10/26
Standard_Visual Basic Coding Convention V1/1
Applications frequently use many menu controls, making it useful to have a unique set of
naming conventions for these controls. Menu control prefixes should be extended beyond the
initial "mnu" label by adding an additional prefix for each level of nesting, with the final menu
caption at the end of the name string. The following table lists some examples.
For controls not listed above, you should try to standardize on a unique two or three character
prefix for consistency. Use more than three characters only if needed for clarity.
For derived or modified controls, for example, extend the prefixes above so that there is no
confusion over which control is really being used. For third-party controls, a lower-case
abbreviation for the manufacturer could be added to the prefix. For example, a control instance
created from the Visual Basic Professional 3D frame could uses a prefix of fra3d to avoid
confusion over which control is really being used.
09fe-HD/PM/HDCV/FSOFT 11/26
Standard_Visual Basic Coding Convention V1/1
Prefixes that are added by Visual basic are not changed( although possible) f.i. when one uses
the class wizard to add a property blnIsBezig a private variable is generated with the name
mvarblnIsBezig.
Global g gstrUserName
Module-level m mblnCalcInProgress
(*) “an” is used when the next character is a vowel, in order to avoid that the two vowels are
used together. Also with wordt consonants such as “m” and “n”, which are pronounced as if
the would start with a vowel.
09fe-HD/PM/HDCV/FSOFT 12/26
Standard_Visual Basic Coding Convention V1/1
Remark : for user-defined class it is better to use an instance variable with a name
corresponding with the class. f.i.:
format : WwwXxxYyy
Examples :
- OphalenBoekingen or OhlBk
- OpvullenScherm or OvlSrm
09fe-HD/PM/HDCV/FSOFT 13/26
Standard_Visual Basic Coding Convention V1/1
The body of procedure or function name should use mixed case and should be as long as
necessary to describe its purpose. In addition, function names should begin with a verb, such
as InitNameArray or CloseDialog.
For frequently used or long terms, standard abbreviations are recommended to help keep
name lengths reasonable. In general, variable names greater than 32 characters can be
difficult to read on VGA displays.
When using abbreviations, make sure they are consistent throughout the entire application.
Randomly switching between Cnt and Count within a project will lead to unnecessary
confusion.
Calling up a procedure or function from another module results into an indication in the
modulename:
In a large project with many user-defined types, it is often useful to give each such type a
three-character prefix of its own. If these prefixes begin with "u," they will still be easy to
recognize quickly when you are working with a user-defined type. For example, “ucli” could be
used as the prefix for variables of a user-defined Client type.
3.1.7. Constant
Must be UPPER CASE and has a underscore (_) between each word. Although standard
Visual Basic constants do not include Hungarian information, prefixes like int, str,g and m can
be very useful in understanding the value and scope of a constant. For constant names, follow
the same rules as variables.
All procedures and functions should begin with a brief comment describing the functional
characteristics of the procedure (what it does). This description should not describe the
implementation details (how it does it) because these often change over time, resulting in
unnecessary comment maintenance work, or worse yet, erroneous comments. The code itself
and any necessary inline comments will describe the implementation.
09fe-HD/PM/HDCV/FSOFT 14/26
Standard_Visual Basic Coding Convention V1/1
Arguments passed to a procedure should be described when their functions are not obvious
and when the procedure expects the arguments to be in a specific range. Function return
values and global variables that are changed by the procedure, especially through reference
arguments, must also be described at the beginning of each procedure.
Procedure header comment blocks should include the following section headings.
For example:
'********************************************************************
' Purpose: Adds a new user into the database
' Inputs:
' strLoginName : LoginName of User to identify another user
' strPassword: Password of user to use
' blnFlagFirstLogin: The value for identify user login into
system in the first time
' Returns:
' True value if insert into database completed success
' False value if insert into database not completed
' Author: Nguyen Van A – Developer, Group X, FPT-SOFT
'********************************************************************
3.2.2. Inline comment
- Every important variable declaration should include an inline comment describing the
use of the variable being declared.
- Variables, controls, and procedures should be named clearly enough that inline
commenting is only needed for complex implementation details.
09fe-HD/PM/HDCV/FSOFT 15/26
Standard_Visual Basic Coding Convention V1/1
- At the start of the .bas module that contains the project's Visual Basic generic constant
declarations, you should include an overview that describes the application, enumerating
primary data objects, procedures, algorithms, dialogs, databases, and system
dependencies. Sometimes a piece of pseudocode describing the algorithm can be helpful.
Example
3.3.1. Indenting
Standard, tab-based, nested blocks should be indented, and converted to four spaces
The functional overview comment of a procedure should be indented one space. The highest
level statements that follow the overview comment should be indented one tab, with each
nested block indented an additional tab.
Example:
Variables and defined constants should be grouped by function rather than split into isolated
areas or special files. Visual Basic generic constants should be grouped in a single module to
separate them from application-specific declarations.
09fe-HD/PM/HDCV/FSOFT 16/26
Standard_Visual Basic Coding Convention V1/1
Always use the & operator when linking strings and the + operator when working with
numerical values. Using the + operator to concatenate may cause problems when operating
on two variants. For example:
vntVar1 = "10.01"
vntVar2 = 11
Numeric: Right
String: Left
09fe-HD/PM/HDCV/FSOFT 17/26
Standard_Visual Basic Coding Convention V1/1
Bottom margin?
Date DD/MM/YYYY
Report title
Justify Left
Size 16
Column
heading
09fe-HD/PM/HDCV/FSOFT 18/26
Standard_Visual Basic Coding Convention V1/1
Font size 10
Report body
(data)
Font size 9
Row spacing 0
Total row
Font size 9
Justify Right
Grouping line
Width 1 Point
09fe-HD/PM/HDCV/FSOFT 19/26
Standard_Visual Basic Coding Convention V1/1
Page number
Font size 9
Style
Font size 10
Font size 10
09fe-HD/PM/HDCV/FSOFT 20/26
Standard_Visual Basic Coding Convention V1/1
4. DATABASE
Use Parameters for variables in the SQL – never compose a SQL statement by concatenating
the variable values in the SQL string.
Use Bookmarks to keep track of a selection a user previously made in e.g. a grid.
Use as much as possible read-only for Recordsets. If you have many records in a list, and
you need to edite, you can always open a separate Recordset with only one Record.
After an action on a selected row in a grid (f.i. by using a Bookmark), you can maintain the
same selection Create Recordsets with a minimum of locking.
Reason:
Recordset cursors are client-side unless the performable profit of server-side cursors is
considerable. Problems arise with Jet4.0 oledb driver when server-side cursors are used (see
msdn “PRB: DataGrid Not Populated Using Jet.OLEDB.4.0 Provider and ADO Server Side
Cursor”).
09fe-HD/PM/HDCV/FSOFT 21/26
Standard_Visual Basic Coding Convention V1/1
5. ERROR HANDLING
All event handlers have an error handler. If this isn’t provided and a error appears, the
application will be closed by VB (!).
Good programming practice: only one error handler in all procedures. In some cases this will
be superfluous.
Error will be raised and sent to form. Error will be logged at the class level.
Err.Description messages for functional errors (i.e. the errors we expect to get, e.g. invalid
input by user) are generated at runtime by using the message manager. These messages can
be parameterized to pass context sensitive information and can be shown directly by the
presentation layer.
Put as few code into the class_Initialize event as possible. The object isn’t really created yet at
that point which makes error handling less evident.
Create an extra “Initialize” method which can be called after creating the logger and other
utility classes.
09fe-HD/PM/HDCV/FSOFT 22/26
Standard_Visual Basic Coding Convention V1/1
6. TIP
By using variables of the type enumeration it isn’t guaranted that the variables of this type are
limited to the values defined by the enrum type.
Above-metioned line is executed, without causing an error. The value is accorded to the
variable m_Alignment, unless the programmer himself examines if the mentioned value is
found within the means.
In other words Enum types verify the datatype, but not if the value belongs to the defined
values within the enum type.
6.2. Checklist
2 ByRef Parameters are not advised d for input/output. If it seems necessary, a class
is probably a better solution.
7 Use enumerations (enum) for possible values of function and sub parameters.
09fe-HD/PM/HDCV/FSOFT 23/26
Standard_Visual Basic Coding Convention V1/1
10 Methods and classes are saved in the “Attributes – Description” panel in the
classbuilder. This description can be found in the Object browser.
12 Use Module level variables only when necessary (property values, …). Don’t use it
for communication between subroutines.
13 Always use IF
Then
End If
Example :
sngBedrag = Val(txtBedrag.Text)
Result :
Conclusion :
Don’t use this function with amounts, except in exceptional circumstances where the operation
can’t cause problemes.
Problem : These functions depent on the Regional settings Windows, but operate incorrect if
the string, that needs to be converted, has a different format than the international settings.
09fe-HD/PM/HDCV/FSOFT 24/26
Standard_Visual Basic Coding Convention V1/1
If the decimal and thousand separator are switched (f.i. English into Dutch), no fault is raised.
The symbol that the user applies as decimal, is considered and ignored as thousand
separator.
Example :
sngBedrag = CSng(txtBedrag.Text)
Result :
Conclusion :
The developer needs to know for sure that the correct decimal separator is used in the
convertable string. f.i. don’t use wrong symbols in the textbox.
6.4. Multital
To do this… Do this..
Multitaal form.caption Install an invisible label on the form and add it in the
translationdatabase.
You always unregister all un-used DLL’s before deleting the DDL of the developing PC.
09fe-HD/PM/HDCV/FSOFT 25/26
Standard_Visual Basic Coding Convention V1/1
09fe-HD/PM/HDCV/FSOFT 26/26