Avoid using Exit

  • Thread starter Thread starter Lars Brownies
  • Start date Start date
There is no hard rule for the 'pro's'.
However, when giving advice to questions on this discussion group, I would
agree with the advice in that article from fms.

To avoid long nested procedures, you can do something like this

Dim blnDeleted As Boolean
 
On Sun, 7 Feb 2010 21:57:00 +0100, "Lars Brownies" <[email protected]>
wrote:

I follow the rule that a procedure should have only one exit point.
This is where I clean up and return.
This is a requirement for all programmers in our company.

-Tom.
Microsoft Access MVP
 
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.
 
A single exit point for any procedure certainly makes your code easier to
maintain, easier to follow, safer (you have a place where you clean
everything up.) There are times to break the rule, but only when you
understand the good, logical reasons for the rule in the first place.

You certainly do end up with more nested IFs if you don't exit from multiple
points. But there's no need to make every test another level of nesting.
Instead of:
If sometest Then
If anothertest Then
If thirdTest Then
'finally do something
End If
End If
End If
I prefer:
Dim bCancel As Integer
If Not sometest Then
bCancel = True
End If
If Not bCancel Then
If Not anothertest then
bCancel = True
End If
End If
'and so on.

That's very straightforward: each test is a logical block of its own, and
it's dead-easy to insert or remove tests without re-indenting all your
nesting, so maintenance is easier when you review the code 2 years later. As
well as avoiding the multiple-EXITs issue, it also makes it simple to build
up a message string to show the user about the issues that caused you to
cancel (if you need to do that.)

Just another view, but Luke's recommendation is based on sound principles
IMHO.
 
I follow the rule that a procedure should have only one exit
point. This is where I clean up and return.
This is a requirement for all programmers in our company.

Hmm. I make the exception for a guard clause at the begining of a
sub:

If (some condition) Then Exit Sub
[complicated code that does lots of things]

If nothing has been initialized and needs to be torn down, and it's
a sub or a function that doesn't need to return anything but False
if the guard clause condition is met, then I don't see an issue.

But I make that exception *only* when it's the very first line of
code in the sub (other than comments, On Error... and variable
definitions).
 
On 8 Feb 2010 02:40:46 GMT, "David W. Fenton"

I agree that's a sensible exception.
-Tom.

I follow the rule that a procedure should have only one exit
point. This is where I clean up and return.
This is a requirement for all programmers in our company.

Hmm. I make the exception for a guard clause at the begining of a
sub:

If (some condition) Then Exit Sub
[complicated code that does lots of things]

If nothing has been initialized and needs to be torn down, and it's
a sub or a function that doesn't need to return anything but False
if the guard clause condition is met, then I don't see an issue.

But I make that exception *only* when it's the very first line of
code in the sub (other than comments, On Error... and variable
definitions).
 
Back
Top