Difference between revisions of "Coding Conventions"

From vb24
Jump to navigation Jump to search
Line 6: Line 6:
 
'################################################################################
 
'################################################################################
 
'# Section
 
'# Section
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 27: Line 26:
 
'################################################################################
 
'################################################################################
 
'# Methods
 
'# Methods
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 43: Line 41:
 
'################################################################################
 
'################################################################################
 
'# Methods
 
'# Methods
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 66: Line 63:
 
'################################################################################
 
'################################################################################
 
'# Methods
 
'# Methods
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 81: Line 77:
 
'--------------------------------------------------------------------------------
 
'--------------------------------------------------------------------------------
 
'- Subsection
 
'- Subsection
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 116: Line 111:
  
 
== Comments ==
 
== Comments ==
 +
Comments are good and comments are bad.
 +
 +
The good thing about comments is that they can explain a complicated code manoeuvre and
 +
by that shorten the time for understanding the code a lot.
 +
 +
The bad thing about comments is that they are usually not as well maintained as the code,
 +
which leads the possible evil side effect that the comment explains something that the
 +
code actually does not do.
  
 +
Besides that should comments be completely superfluous, if your code is clean.
  
 +
So what do we do with comments now? We try to avoid them whereever we can and whenever
 +
we come across a comment, we will try to remove it, unless it is crucial for understanding
 +
the code underneath &hellip; and we do not have the time to refactor the code to some
 +
better understandable state.
  
  
== Function Names ==
+
== Names ==
A function name
+
Choosing the right name for a class, module, form, variable, constant, method, table
* describes what the function does
+
or whatever object we are dealing with is our foremost duty as these names are hard
* starts with a verb
+
to be changed later on and a good naming also paints a better picture of our idea
 +
about the system and how it is supposed to work.
 +
 
 +
=== General ===
 +
* Everything is singular
 +
* Nothing is abbreviated
 +
 
 +
Exceptions
 +
* importLines(), importLine()
 +
* Iso
 +
 
 +
=== Variables ===
 +
Besides the prefix obtained by the [[Naming_Conventions]] the name of a variable has
 +
to reflect its content.
 +
 
 +
<syntaxhighlight lang="vb">
 +
Dim strText As String
 +
Dim intCode As Integer
 +
Dim blnDone As Boolean
 +
</syntaxhighlight>
 +
 
 +
Sometimes the prefix makes even a further differentiation unnecessary
 +
 
 +
<syntaxhighlight lang="vb">
 +
Dim strCustomer As String  '  Name of the customer
 +
Dim lngCustomer As Long    '    Id of the customer
 +
Dim blnCustomer As Boolean 'Status of the customer
 +
</syntaxhighlight>
 +
 
 +
This is a matter of taste and subject to the coder's personal preferences as well
 +
as the meaningfulness in the context of the code. The clear, undisputable and
 +
unique way would in this case still be
 +
 
 +
<syntaxhighlight lang="vb">
 +
Dim strCustomerName  As String
 +
Dim lngCustomerId    As Long 
 +
Dim blnCustomerStatus As Boolean
 +
</syntaxhighlight>
 +
 
 +
 
 +
=== Methods ===
 +
A method name
 +
* describes what the function does,
 +
* starts with a verb and
 
* starts lowercase
 
* starts lowercase
  
 
<syntaxhighlight lang="vb">
 
<syntaxhighlight lang="vb">
Sub reportDifference()
+
Public Sub reportDifference()
 
     '...
 
     '...
 
End Sub
 
End Sub
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
If the method is a function, it usually starts with "get" or
 +
&ndash; if the result is a boolean value &ndash; with "is" or "has".
 +
Some people use numeric verbs in order to indicate that the return
 +
value of a function is numeric like "countItems", but in favour
 +
of a clear distinction between subs and functions this should be rather
 +
defined as "getItemCount".
 +
  
 
== Line breaks ==
 
== Line breaks ==
 +
 +
 +
=== Code ===
 +
 +
=== Line ===
 +
  
 
== Blank lines ==
 
== Blank lines ==

Revision as of 13:42, 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

'################################################################################
'# Controls

'################################################################################
'# Methods

You notice the parallels to a class module, just the "Class" is here replaced by "Form" and the "Properties" are the "Controls".


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

Organizing the code beyond the subdivision into sections also simplifies the code maintenance:

Order

If your module is for example a Microsoft Access form, you should rearrange the methods according to their (likeliness) of occurrence.

In the "Form" section this would result in the order: Open, Load, Resize, Activate, Current. Unused functions can be omitted of course.

Hierarchy

There are functions that will be called first and functions that will be called by other functions (and which are usually declared private). It is good practice to define the called functions after the calling functions.

By doing so you allow the reader of your code to first envision your intention and then to read how you solved the different tasks.


Comments

Comments are good and comments are bad.

The good thing about comments is that they can explain a complicated code manoeuvre and by that shorten the time for understanding the code a lot.

The bad thing about comments is that they are usually not as well maintained as the code, which leads the possible evil side effect that the comment explains something that the code actually does not do.

Besides that should comments be completely superfluous, if your code is clean.

So what do we do with comments now? We try to avoid them whereever we can and whenever we come across a comment, we will try to remove it, unless it is crucial for understanding the code underneath … and we do not have the time to refactor the code to some better understandable state.


Names

Choosing the right name for a class, module, form, variable, constant, method, table or whatever object we are dealing with is our foremost duty as these names are hard to be changed later on and a good naming also paints a better picture of our idea about the system and how it is supposed to work.

General

  • Everything is singular
  • Nothing is abbreviated

Exceptions

  • importLines(), importLine()
  • Iso

Variables

Besides the prefix obtained by the Naming_Conventions the name of a variable has to reflect its content.

Dim strText As String
Dim intCode As Integer
Dim blnDone As Boolean

Sometimes the prefix makes even a further differentiation unnecessary

Dim strCustomer As String  '  Name of the customer
Dim lngCustomer As Long    '    Id of the customer
Dim blnCustomer As Boolean 'Status of the customer

This is a matter of taste and subject to the coder's personal preferences as well as the meaningfulness in the context of the code. The clear, undisputable and unique way would in this case still be

Dim strCustomerName   As String 
Dim lngCustomerId     As Long   
Dim blnCustomerStatus As Boolean


Methods

A method name

  • describes what the function does,
  • starts with a verb and
  • starts lowercase
Public Sub reportDifference()
    '...
End Sub

If the method is a function, it usually starts with "get" or – if the result is a boolean value – with "is" or "has". Some people use numeric verbs in order to indicate that the return value of a function is numeric like "countItems", but in favour of a clear distinction between subs and functions this should be rather defined as "getItemCount".


Line breaks

Code

Line

Blank lines

  • Dim block
  • if, select, etc

Error Handling

Name parameters

  • Param:="Xyz"

Obsolete constructs

  • Call
  • Set Nothing