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.

1. Write comments for others and yourself.

Undoubtedly, comments make your program more understandable. Your comments will not just help other programmers understand your code, they will also be helpful for you.

Here are some tips:
  • Write comments as you are writing the code. If you want to write the comments after finishing your entire project or even after finishing a small part of the project, that is a bad idea. Because at that time, you will forget where you had to write the comments.
  • Write comments clearly. Your comments should be concise and easy to understand.
  • Write comments at the beginning of every module, function or block of code. Describe what it does, or give any other necessary information.
  • Write comments in the places where confusion may arise. Clear the confusions using some appropriate comments.

2. Use meaningful names for variables, functions, etc.

Naming meaningfully the variables, symbolic constants, controls, modules, and functions will greatly increase the readability of your 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 so that one can understand from the name what the function does. 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’ tells 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 be meaningful.

3. Format your code.

Formatted code is easy to read. Use indentation in your code. Give tabs and 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.

To avoid confusions, make the scope of a variable as small as possible. The lifetime should also be very short. This will ensure maintainability. I know that sometimes, a small scope and short lifetime are difficult to achieve but you should definitely have small scopes and short lifetime when possible.

Learn about:

5. Choose the right data types.

Consider taking small-sized datatypes wherever possible. The variables, that are too large, waste memory space.

Related tutorials:

6. Eliminate unnecessary variables.
Whenever you are using unnecessary variables, you are wasting memory space, and it will make the program very large in size. Taking a small number of variables greatly optimizes the code.

7. Error handling is a must.

Error handling is a must for any project. Error handling ensures that the code will not suddenly stop running despite the fact that there is an error or exception in the program.

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. 

And suppose you’re dividing a number by 0. This will cause a run-time error and the program will stop. So you have to develop efficient routines that can handle the exception and show the user an error message.

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 is - keep the code simple. Always try to find a simple solution to any problem.

These are the common programming practices that will improve the quality of your code. These coding principles will help you develop readable, maintainable, and high-performing programs.