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
completed? Like this:
If x = x then
Exit Sub
End If
There is no exit statement for a conditional, because either aWhat is the downside to exiting a procedure before an IF statement is
completed? Like this:
If x = x then
Exit Sub
End If
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
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.
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:
. . .
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.