If you are still a young vibrant programmer, run away! VB6 is a dead-end. This VB6 gig only makes sense if you're positioning yourself as a VB6'er gun-for-hire ("have laptop, will travel") type - where, if you're lucky, you might get to be about as essential as a COBOL coder. So if you must...
Check out the links MarkJ gives in his comments above. You can also google "vb6 coding standards" or "visual basic 6 books" for more ideas. "Hardcore Visual Basic" by Bruce McKinney, available on-line for free at http://vb.mvps.org/hardcore/ (lots of good stuff here about Win32 API and COM), Chapter 1, "Language Purification", is worth a read, and should be helpful in addressing some bad habits.
My advise would be to learn C# or Java (as MarkJ recommends) on the side and track down good books or websites about coding standards and best practices written for these languages. From there, adopt or adapt what you can into your VB6 coding style.
Here are my VB6 Specific Anti-Bad-Habit Tips:
(1) Always use Option Explicit
. Be careful with, or otherwise avoid, the other Option
statements.
(2) Avoid DoEvents
- instead find a way to make it happen with Timers or Events. Know what a main event loop is. Know what the message pump is. Know that a VB6 app is at its core a main event loop that services its message pump (hidden from the VB6 programmer). DoEvents
is a cheat that often ends in tears.
(3) Avoid Option Base
. Avoid To
in dimensioning arrays. The first element of an array should have an index of zero.
(4) Do not Dim
variables inside of an If ... EndIf
, Do ... Loop
, For ... Next
, or any other such block - it will lead to confusion over scope in VB.NET land (or for people familiar with other languages). It may make code harder to read or follow.
(5) Always Dim
with As
- i.e. don't use Variant
or Object
unless absolutely necessary. Unlearn the variable decorations such as $ (String) or % (Integer) - in other words, don't use them, use As
.
(6) Prefer For Each
over For
whenever possible and appropriate.
(7) Prefer ByVal
over ByRef
for numeric parameters that won't be changed as a side effect by a Sub
or Function
.
(8) Regard On Error Resume Next
as something toxic and to be avoided whenever possible. Must you use it? Comment verbosely explaining why it's needed, then turn it off ASAP. Maybe encapsulate it in its own Sub or Function.
(9) Learn about the Model/View/Controller (MVC) design pattern. Avoid placing any business logic into a Form.
(10) Prefer Boolean
(True
vs False
) over Integer
(0
vs 1
or -1
) or (heaven help us!) String
("N"
vs "Y"
). Assuming Dim MyFlag As Boolean
, know that If MyFlag = True Then
raises my suspicion that the other coder doesn't get Boolean
. Prefer If MyFlag Then
and If Not MyFlag Then
.
(11) ActiveX/COM development isn't for wimps - and should be done with proper understanding of Binary Compatibility and within Virtual PC.