Spread the love
It is up to you to follow following practices.
- Avoid magic numbers when you can: These are numbers which are hardcoded in your program.
- 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.
- Program defensively: e.g. check the variable type before using it, so that the bug can be spotted easily later on.
- Visual Basic procedures should have only one purpose, ideally. this is also an aid to larger programs when things start to get complex.
- Avoid deep nesting of conditionals or loops. Three levels of nesting should be about the maximum.
- Use access procedures to protect sensitive data. E.g. using get and set method to give values to variables.
- Ideally, variables should always be defined with the smallest scope possible.
- Do not pass global variables to procedures. This can cause the serious bug in your program.
- Use the & operator when linking strings and the + operator when working with numerical values.
- 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.”
- Avoid using variants if you can. Because they waste memory and time due to the conversion of the data type.
- Indent your code with four spaces. Keep consistency with the spaces.
- 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.