Exit Sub before End If

  • Thread starter Thread starter Del
  • Start date Start date
D

Del

What is the downside to exiting a procedure before an IF statement is
completed? Like this:

If x = x then
Exit Sub
End If
 
hi,
What is the downside to exiting a procedure before an IF statement is
completed? Like this:

If x = x then
Exit Sub
End If
There is no exit statement for a conditional, because either a
conditional is true and will be executed or it is false and it won't.

Normally a second conditional is enough:

If Condition Then
If Not x = x Then
End If
End If


mfG
--> stefan <--
 
Del said:
What is the downside to exiting a procedure before an IF statement is
completed? Like this:

If x = x then
Exit Sub
End If


The only downside that I know of is the possibility of obscuring the logical
structure of the code. Purists will probably tell you never to use Exit Sub
for premature exit, as you can accomplish the same thing with conditional
statements (If ... Then ... Else, or Select Case). However, when I have a
procedure that has a number of up-front conditions that must be met if the
main body of the procedure is to be executed, I will often put the tests for
those conditions at the top of the procedure, with Exit Sub used to bypass
further execution if they aren't met. In my opinion, using this method to
avoid deep nesting of Else clauses makes the procedure's logic easier to
understand, not harder.

That said, I won't Exit Sub from deep in the main body of the procedure, as
that does obscure the logic, IMO.
 
I have the exact same rules.
I would also suggest a comment line pointing out the exit would be a good
idea.
 
Dirk said:
The only downside that I know of is the possibility of obscuring the logical
structure of the code. Purists will probably tell you never to use Exit Sub
for premature exit, as you can accomplish the same thing with conditional
statements (If ... Then ... Else, or Select Case). However, when I have a
procedure that has a number of up-front conditions that must be met if the
main body of the procedure is to be executed, I will often put the tests for
those conditions at the top of the procedure, with Exit Sub used to bypass
further execution if they aren't met. In my opinion, using this method to
avoid deep nesting of Else clauses makes the procedure's logic easier to
understand, not harder.

That said, I won't Exit Sub from deep in the main body of the procedure, as
that does obscure the logic, IMO.


A couple of times I've needed to bail out of a deeply nested
set of conditions from several points. It looked distinctly
strange but I used something like:

Do 'Not a loop
If ,,,
If ...
Exit Do
End If
. . .
Else
. . .
Exit Do
End If
While False

AllDone:
Exit Sub
ErrorHandler:
. . .
 
Marshall Barton said:
A couple of times I've needed to bail out of a deeply nested
set of conditions from several points. It looked distinctly
strange but I used something like:

Do 'Not a loop
If ,,,
If ...
Exit Do
End If
. . .
Else
. . .
Exit Do
End If
While False

AllDone:
Exit Sub
ErrorHandler:
. . .


Shouldn't that have been "While True"?

I've used that sort of logic upon occasion. As for the "Exit Do": sure,
I've used that, too, but that's a bit more gentle than "Exit Sub, if similar
in concept.
 
Dirk said:
"Marshall Barton" wrote


Shouldn't that have been "While True"?

I've used that sort of logic upon occasion. As for the "Exit Do": sure,
I've used that, too, but that's a bit more gentle than "Exit Sub, if similar
in concept.


Actually, since it is an artificial construct, it should be

Do
...
Loop While False

because I never want it to repeat.

As I said, looks strange, but at least it avoids the
ridiculed GoTo ;-)
 
Actually, since it is an artificial construct, it should be

Do
...
Loop While False

because I never want it to repeat.

Oh, I get it now. I thought you were actually intending to loop, but
reserve judgement as to when to bail out.
 
Back
Top