Lars Brownies said:
In the following article the author recommends avoiding the use of EXIT
commands in the body of procedures:
http://www.fmsinc.com/free/NewTips/VBA/Avoid_Procedure_Exits.html
If you want to follow this rule, I think you will end up with a lot of
nested if-then structures.
My question: is this rule common practice among the pro's?
I don't entirely agree, or at least I think he makes his case too strongly,
setting up something of a "straw man" argument to do it. It's not a bad
rule of thumb to avoid unnecessary premature exits in a procedure, and I'd
agree that deeply nested exits are likely to be a result of muddy thinking,
but I'd say there's no special reason to prefer this:
'------ example 1 ------
Dim fDontDoIt As Boolean
If (some criteria) Then
fDontDoIt = True
Else
If (other criteria) Then
fDontDoIt = True
Else
If (yet other criteria) Then
fDontDoIt = True
End If
End If
End If
If Not fDontDoIt Then
' Do whatever this proc is for.
End If
'------ end example 1 ------
.... to this:
'------ example 2 ------
If (some criteria) Then Exit Sub
If (other criteria) Then Exit Sub
If (yet other criteria) Then Exit Sub
' Do whatever this proc is for.
'------ end example 2 ------
However, that is the special case of establishing at the beginning of the
procedure whether you want to execute the main body of the procedure. In
general, if you can get into the main body of the procedure and only then
determine that you should not continue, either your procedure should be
broken into two or more procedures, or else there's an error condition that
ought to be raised as such.