Naming procedure line labels
I recently worked on improving a VB6 application, and as part of the task, I decided to "clean up" the way procedures used line labels.
While programmers usually use pretty descriptive (and often unique) names for procedure line labels, our preference has always been to stick to naming them exactly the same, irrespective of the procedure they're in.
Take this example:
Public Sub HideForm(ByRef F As Form)
On Error GoTo Me_Fail
Dim C As Integer, Loaded As Boolean
C = Forms.Count
With F
Do Until C = 0
If Forms(C - 1).Name = .Name Then
Loaded = True
End If
C = C - 1
Loop
If Loaded Then
.Hide
End If
End With
Me_Exit:
Exit Sub
Me_Fail:
Alert "Application", "HideForm", Err.Number, Err.Description, ERROR.FATAL
Resume Me_Exit
End Sub
This approach simplifies the procedure creation process because each procedure gets "Me_Exit" and "Me_Fail" as the two standard labels which get branched to, as part of the error handling logic. It's also easy to remember, because every procedure has the same two generic labels.
I have been doing this for years but it appears that I'm in the minority, as most VB source code that I've seen tends to use procedure line label names which vary from procedure to procedure. This is unnecessary as Visual Basic won't let you use the "Goto" statement to branch outside of a procedure anyway.
While some VB developers might argue that there is sometimes a need for more than just the two error handling labels I mentioned above, I'd say that if you think so, then you probably need to think a bit more about your code design.
Due to the large volume of spam, comments are disabled. If you have anything relevant to say, you can leave a comment via Twitter, or contact me directly.