D Coding/naming convention
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.
Different ways of naming you variables exists. You are advised to adopt a naming convention; some use snake case others use camel case. The Leszynski naming convention define variables with a consistent prefix that makes it easy to identify its data type. It is common to use Leszynski convention within the VBA community. The R community use snake case but camel case is also okay. Some common prefixes used for the Leszynski naming convention are:
Type | Prefix | Example |
---|---|---|
Boolean | bln | blnFound |
Currency | cur | curRevenue |
Date (Time) | dtm | dtmStart |
Double | dbl | dblTolerance |
Integer | int | intQuantity |
Long | lng | lngDistance |
String | str | strFName |
Variant | vnt | vntCheckSum |
Array | ary | aryNumbers (optional) |
User form | frm | frmProcess |
Worksheet | wst | wstDistances |
Workbook | wbk | wbkData |
Many other prefixes can be used also.
Choose the naming convention you like best in your study group. But stick only to one of them. A few examples:
this_is_snake_case # note you do not use capital letters here
thisIsCamelCase # you start each word with a capital letter (except the first)
dblTolerance # Lezynski convention naming a double (dbl) variable
strFullName # Lezynski naming a string (str) variable
When defining variables and functions, it is in general good practice to use nouns for variables and verbs for functions.
D.1 Commenting your code
It is always good practice to comment your code. Such that others can get a fast overview and understand your code easier. You can add comments at the lines of code you think is difficult to understand. Moreover, you should document the whole procedure too. We will use roxygen documentation comments which are widely known. A few examples in VBA are
The top of a module file:
''' Module description.
' Can be more than one line.
' @remarks Put your remarks on the module implementation here
' @author Lars Relund <junk@relund.dk>
' @date 2016-08-26
Before each sub
, function
etc. write:
'' Procedure description
'
' @param strA Explanation of input parameter strA
' @param intB Explanation of input parameter intB
' @return Return value (if a function)
' @remarks Further remarks
Public Function MyFunc(strA As String, intB As Integer) As Integer {
...
}
Further tags (i.e. keywords starting with @) can be seen here.
In R we use a ‘hash’ (#’) to comment functions: