Coding of Procedures/Sub-routines should be done in such a way as to ensure the code is as easy to read as possible. This involves the indentation and spacing of code in order to distinguish between specific processes and functions within the code, as shown in the example below.
Private Sub cmdButton_Click()
Dim strString as string
Dim blnBoolean as boolean
On Error Goto ErrorHandler
''''''''''''''''''''''
' This is an example '
''''''''''''''''''''''
strString = "Hello"
If blnBoolean then
If Function(strString) then
Exit sub
End if
End if
Exit Sub
ErrorHandler:
Msgbox err.number & ":" & err.description, vbcritical, _
"cmdButton_Click"
End Sub
The same code without spacing or indentation proves much more difficult to read (see below).
Private Sub cmdButton_Click()
DIM strString as string
DIM blnBoolean as boolean
On Error Goto ErrorHandler
''''''''''''''''''''''
' This is an example '
''''''''''''''''''''''
strString = "Hello"
If blnBoolean then
If Function(strString) then
Exit sub
End if
End if
Exit Sub
ErrorHandler:
Msgbox err.number & ":" & err.description, _
vbcritical,"Button Click
End Sub
Procedure specific variable declarations should be grouped together at the top of the procedure. Where appropriate attempt to group variables by common threads (i.e. Variables associated with particular processes or with common functions.) and then group by
datatype. This rule should also be applied to variables declared as Option Explicit
Try to keep procedures to a manageable size. If this requires the movement of code to sub-routines or functions then do this. Although the code may be split it will still be much easier to read and understand in smaller chunks.
Procedure & Coding Formats
Phinuss, Monday, September 22, 2008
Subscribe to:
Post Comments (Atom)
Comments :
Post a Comment