VB 6 Coding Style

In general, it’s more important to have some coding styles than to use a particular set of rules. Code that is consistent is easier to read, understand, debug, and maintain no matter what rules you use.

However, it amazes me how many developers seem to have no rules or rules that are inconsistent and contradictory. In one very large (56,000 line) project, I’ve seen people using arbitrary indentation, no comments, improper variable naming (for example, using an “m” prefix that normally means “module-level” for local variables), and so forth.

So here are the rules that I generally use. Adopt the ones that makes sense to you and replace the others. Note that these are the rules that I use for Visual Basic 6. I use slightly different rules for other languages, including VB .NET.


  • Indentation: Indent 4 spaces to show scope. Indent within a subroutine, loop, or If Then block as in the following:
        Private Sub MySub()
        Dim i As Integer
            For i = 1 To 10
                Debug.Print i
            Next i
        End Sub
  • Make sure indentation lines up block start and end statements. For example, make sure the If, Then, Else, and End If statements in a block all line up.
  • Declare variables at the beginning of a subroutine at the same level of indentation as the Sub statement. (A lot of people prefer indent the declarations. Whatever you prefer. Just pick one. Note that VB .NET indents them for you whether you like ot or not.)
  • Use Option Explicit and Option Strict.
  • Declare each variable on a separate line.
  • Prefix module-level variables with “m_” and use “Pascal Case” (capitalize the first letter of each word) as in m_NumEmployees.
  • Prefix global-level variables with “g_” and use “Pascal Case” as in g_NumEmployees.
  • Name constants in ALL CAPS with underscores as in MAX_EMPLOYEES.
  • Specify a data type for all variables and constants as in:
        Private Const MAX_EMPLOYEES As Long = 100
  • Name routine-local variables in lower case with underscores as in num_employees.
  • Use Pascal case for subroutine, function, property, and other routine names as in NumEmployees().
  • Always specify one of Private, Public, etc. whenever those are allowed. I.e. don’t declare a subroutine with just Sub, use Private or Public to make its scope obvious.
  • Use type prefixes on control names as in picEmployee (a PictureBox) and lblResult (a Label).
  • Do not use type prefixes (aka Hungarian notation) for other variables.
  • Make variable names obvious enough that you don’t miss Hungarian notation.
  • Use a comment at the beginning of every module explaining what its purpose is.
  • Use a comment before every routine explaining what the routine does. Explain parameters and return values.
  • Use liberal comments within the code to explain what it is doing.
  • Do not use GoTo.
  • Do not use IIF. It’s confusing and If Then Else is actually faster.
  • Do not use the : character to place more than one statement on a single line.
  • If a single-line If Then statement is long, make it a multi-line If Then End If statement so it’s easier to read.
  • Don’t try to make anything do too many things. For example, Visual Basic’s Line command draws lines or rectangles, optionally specifying the drawing color and whether the rectangle should be filled. This should have been at least two routines, Line and Rectangle.
  • When you use error handling code with an On Error GoTo statement, always place an Exit Sub statement immediately before the error handler. Never allow the code to drop into the error handling code. This makes the code run in a special error handling mode and can be quite confusing.
  • Use Debug.Assert to test for conditions that should not occur. This lets you catch bugs early and easily.
  • If you catch an error, check for all errors not just the ones you expect. Use Debug.Assert to catch those you don’t expect.
  • In a Select Case statement, use a Case Else to catch unexpected conditions. If you don’t think the condition should ever occur, use Debug.Assert to detect it.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )


Connecting to %s

%d blogger menyukai ini: