Difference between revisions of "Coding Conventions"

From vb24
Jump to navigation Jump to search
Line 1: Line 1:
 
== Separators ==
 
== 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:
 +
 
 +
<syntaxhighlight lang="vb">
 +
'################################################################################
 +
'# Section
 +
</syntaxhighlight>
 +
 
 +
=== Class Module ===
 +
The separation in a typical class module looks like this:
 +
 
 +
<syntaxhighlight lang="vb">
 +
'################################################################################
 +
'# Constants and variables
 +
 
 +
'################################################################################
 +
'# Enum and Type
 +
 
 +
'################################################################################
 +
'# Class
 +
 
 +
'################################################################################
 +
'# Properties
 +
 
 +
'################################################################################
 +
'# Methods
 +
</syntaxhighlight>
 +
 
 +
 
 +
=== Code Module ===
 +
A plain has less sections than a [[#Class Module]]:
 +
 
 +
<syntaxhighlight lang="vb">
 +
'################################################################################
 +
'# Constants and variables
 +
 
 +
'################################################################################
 +
'# Enum and Type
 +
 
 +
'################################################################################
 +
'# Methods
 +
</syntaxhighlight>
 +
 
 +
 
 +
=== 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:
 +
 
 +
<syntaxhighlight lang="vb">
 +
'################################################################################
 +
'# Constants and variables
 +
 
 +
'################################################################################
 +
'# Enum and Type
 +
 
 +
'################################################################################
 +
'# Form
 +
 
 +
'################################################################################
 +
'# Methods
 +
</syntaxhighlight>
 +
 
 +
 
 +
=== 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:
 +
 
 +
<syntaxhighlight lang="vb">
 +
'--------------------------------------------------------------------------------
 +
'- Subsection
 +
</syntaxhighlight>
 +
 
 +
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 ==
 
== Organization ==

Revision as of 12:57, 27 September 2010

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