Best Coding Practices Visual Basic 6

Spread the love

It is up to you to follow following practices.

  1. Avoid magic numbers when you can: These are numbers which are hardcoded in your program.
  2. Be modular: Putting code and data together into modules hides it from the rest of the program, makes it easier to debug, makes it easier to work with conceptually and even makes load time of procedures the in same module quicker.
  3. Program defensively: e.g. check the variable type before using it, so that the bug can be spotted easily later on.
  4. Visual Basic procedures should have only one purpose, ideally. this is also an aid to larger programs when things start to get complex.
  5. Avoid deep nesting of conditionals or loops. Three levels of nesting should be about the maximum.
  6. Use access procedures to protect sensitive data. E.g. using get and set method to give values to variables.
  7. Ideally, variables should always be defined with the smallest scope possible.
  8. Do not pass global variables to procedures. This can cause the serious bug in your program.
  9. Use the & operator when linking strings and the + operator when working with numerical values.
  10. When you create a long string, use the underscore line-continuation character to create multiple lines of code. This so you can read or debug the string easily. e.g.
    • Dim Msg As String
    • Msg = “Well, there is a problem ” _
    •         & “with your program. I am not sure “_
    •         & ” this is my third line and we are going to “_
    •         & ” fourth line and you can continue.”
  11. Avoid using variants if you can.  Because they waste memory and time due to the conversion of the data type.
  12. Indent your code with four spaces. Keep consistency with the spaces.
  13. Watch out for one big Visual Basic pitfall: misspelled variables. Because you don’t have to declare a variable in Visual Basic to use it.