Best Coding Practices for Visual Basic Programmers

Programmers follow a number of informal coding rules to improve the readability, maintainability and performance of their code. This blog post will teach you how to write better code in Visual Basic.

photo credit: elliottcable via photopin cc

1. Write comments for others and yourself.

Undoubtedly, comments make your program more understandable and more readable. Commenting is a good habit. If you write comments in appropriate places of your source code, this will not only help other programmers better understand your code but it will also be greatly helpful for you to understand your own code.

Here are some commenting tips:
  • Write comments as you are writing the code. If you are planning to write the comments after finishing your entire project or even after finishing a part of the project, that is a bad idea. Because at that time, you will forget where you had to write the comments and which part of the code might cause confusions. 
  • Write comments clearly. If necessary, write in full sentences. Your comments must be plain, concise, straight-forward and easy-to-understand so that others can fully understand them.
  • Write comments at the beginning of every module, function or block of code. Describe what it does, or write any other necessary information.
  • Write comments in those places where confusion may arise. Clear the confusions using some appropriate comments.

2. Follow the naming conventions.

Though there are no hard and fast rules about naming conventions, you should stick to a particular naming convention. Naming conventions will make your code more readable. Naming meaningfully the variables, symbolic constants, controls, modules, and functions will help you and others in recognizing them in the code. 

  • Naming variables: Give meaningful names to the variables. This will help you understand which variable is used for what purpose. For example, 'number', 'sum', 'result', and 'average' are the meaningful variable names.
  • Naming Functions: When you are writing a user-defined function, give a meaningful name to it. The function name should indicate what it does. For example: GetResult().
  • Naming controls: For VB6 programs, generally, three-letter prefixes are added to the control names. The three-letter prefix indicates which control you are using. And the remaining part of the control name describes the action performed by the control. For example, ‘cmdShow’ is a control name, where ‘cmd’ indicates that this is a command button and ‘Show’ indicates that it shows something. There are no fixed rules for this. You may have your own style. Learn more about naming conventions.
  • Named constants and modules: Names of symbolic constants and modules should also convey a meaning.

3. Format your code.

Formatted code is easy to read. Formatting ensures logical organization of the code, which is helpful, both to you and other programmers. Use indentation in your code. Give spaces wherever necessary.  

If n1 = 0 Then
        result1 = "0"
        Do While n1 >= 1
            r1 = mod_operator(n1, 2)
            result1 = r1 & result1
            n1 = Fix(n1 / 2)
End If

4. Smaller Scope and shorter lifetime are better.

The scope of a variable should be as small as possible to avoid confusions. And the lifetime should be as short as possible. This will ensure maintainability. 

Learn about:

5. Choose the right data types.

Consider taking small-sized data types wherever possible. The variables, that are too large, waste memory unnecessarily.

Related tutorials:

6. Eliminate unnecessary variables.
Whenever you are using unnecessary variables, you are wasting memory, and it will make the program unnecessarily large in size. So taking considerably less number of variables greatly optimizes the code.

7. Error handling is a must.

Error handling is a must for any program. Error handling ensures that the program will not suddenly stop running, despite the fact that there is an error or exception in the program. Instead, appropriate messages have to be shown to the user so that the program continues running. 

For example, in a calculator program, if the user enters a 100 digit number, a message should be displayed informing the user that the number is too large. 

To give you another example, suppose, in that very calculator program, you’re dividing a number by 0. This will cause a run-time error. As a result, the program would stop. So develop efficient error handling routines.

Learn about:

8. Use the ‘Option Explicit’ statement.
In Visual Basic, use the ‘Option Explicit’ statement to ensure that all the variables are explicitly declared. 

Learn about:

9. Avoid control flow functions - they are slow.

Avoid control flow functions as they are slow. The control flow functions let you write less code, but they are slow. So use control flow statements instead.

Related tutorials:

10. Keep the code simple.

The best advice I can give you to write better code is to keep the code simple. So finding the simplest solution to a problem should be your aim.

11. Write code for human beings.

Write code in such a way that human beings can understand it easily. Because, it is difficult for us to understand the logic of the code that is complex and less readable. So your aim should be to make the code human-understandable.

These are the common programming practices and coding techniques that let you write high-quality code, i.e. readable, maintainable and high-performing code. These coding principles will help you to develop better programs.