Programming style

Many students who are new to computer programming assume that getting the program to work correctly is all that matters! However, even when a program is working correctly, changes to the program might be necessary in the future. Research has shown that program maintenance accounts for over 67per cent of software development time. There are many reasons why maintenance becomes necessary. For example, it may be necessary to make changes to the functionality of a program, or perhaps the organisation that the program was written for has changed their procedures in some way. Improving the layout and readability of the program will make maintenance easier. Both can be achieved by following these style guidelines:

Ensure that VBA program code contains enough comments. Comments should typically be placed at the start of a VBA program, such as details that include the date the program was written, the program version number, the

name of the programmer, and any other things that might help with understanding the code. Other comments might be included to the right of a VBA statement. It is also good programming practice to leave some spaces before in-line comments begin to improve readability.

♦ Use blank lines to separate distinct sections in a program to enhance the readability of the code. For example, the blank line in line 3 of Listing 3.1 separates the comments from the beginning of the main body of code.

♦ Indent VBA program statements inside the body of the macro name and any other VBA statement blocks. It is recommended that the number of characters for indentation be no less than three spaces. Indenting is not essential. Your programs will work without it, but it does make it easier to identify errors. Note that it is better to use the Tab key on the keyboard for indenting lines; otherwise if you use the Space Bar you will probably end up with inconsistent indenting.

♦ As a general rule, you should indent the main program block, i.e., between the Sub... and End Sub lines. You should also indent any other blocks of code within these lines. Examples of blocks are: If .End If, or With.. .End With.

♦ Use a consistent naming convention for items used in VBA program design (more on this in Chapter 5). Meaningful names should be assigned to items, saying something about the item being named.

♦ Use meaningful names for VBA macros. These will help you to know which one to choose in the Select macro list of the Macro dialog box. I have used names for Sub procedures such that they use alphabetic characters only and use capital letters for the second and later words. For example, if we were naming a procedure to name print a character, then the sub procedure would be named printCharacter.

0 0

Post a comment