Coding Conventions
Separators
Separators are a good measure to organize sections in a module. The main separator is a comment with 80 hashes followed by a short description of the section:
'################################################################################
'# Section
Class Module
The separation in a typical class module looks like this:
'################################################################################
'# Constants and variables
'################################################################################
'# Enum and Type
'################################################################################
'# Class
'################################################################################
'# Properties
'################################################################################
'# Methods
Code Module
A plain has less sections than a #Class Module:
'################################################################################
'# Constants and variables
'################################################################################
'# Enum and Type
'################################################################################
'# Methods
Object Module
An object module is based on an object like a form (Microsoft Access) or a worksheet (Microsoft Excel). Let's take a Microsoft Access form as example:
'################################################################################
'# Constants and variables
'################################################################################
'# Enum and Type
'################################################################################
'# Form
'################################################################################
'# Methods
Subsections
If the code should become lengthy (which it should not, but sometimes it happens), it is wise to subdivide the sections into subsections. The separator for a subsection is a lighter variant of the section separator built out of hyphens:
'--------------------------------------------------------------------------------
'- Subsection
The right choice of subsections affects the readability of code, typical use cases are the separation of
- public and private and / or
- functional domains
Applying at least one of these types of subsections should divide your code into smaller more easily maintainable parts and enhance at the same time the readability and coprehension of your code.
Organization
- called functions after calling functions
- grouping functions (VBA Access Form: Form, Controls, Methods)
Comments
Function Names
A function name
- describes what the function does
- starts with a verb
- starts lowercase
Sub reportDifference()
'...
End Sub
Line breaks
Blank lines
- Dim block
- if, select, etc
Error Handling
Name parameters
- Param:="Xyz"
Obsolete constructs
- Call
- Set Nothing