-
I have a structural question... I'm guessing that it'll come down to a matter of tastes, but...
Is it better (looking, working, compiling, etc...) to do an exit sub, or to allow the sub to finish normaly by the use of if statements?
i.e. -
Code:
'Hard or Forced Method
Public Sub example()
If (conditions are not right) then exit sub
'Do subroutine code...
End sub
or
Code:
'Soft Method
Public Sub example()
If (conditions are right) then
'Do subroutine code...
End If
End sub
What do you guys think?
Any efficiency concerns with something like this?
-
I usually prefer to use Exit Sub. Just a personal preference I guess.
-
I'd Say Exit Sub, Especcially if you had lots of them, If you're going to do that then somewhere you'll have a chouce between 3 exit subs or 3 nested ifs, also an exit sub can be inside a liip or if statement, You can't do that with an If Statement
-
I'd say use the Soft Method or something like:
Code:
Public Sub example()
If (conditions are right) then
'Do subroutine code...
Else
Exit Sub
End If
End Sub
But..if you have more than one code or something..than go with Sam's idea.
[Edited by Matthew Gates on 07-15-2000 at 12:03 AM]
-
I prefer to use the Select Case statement when possible, it looks better.
Code:
Dim Variable As String
Select Case Variable
Case "String"
MsgBox "String"
Case "Form2"
Form2.Show
Case "Exit Sub"
Exit Sub
Case "End"
Dim F As Form
For Each F In Forms
Unload F
Next F
End Select
-
There is no difference in the code implementation when vb hits the bottom of that sub routine it hits end sub which is the exact equivalant of exit sub. When the If then statement is ran the code is very flexible afterwards for the easability to add more code to it. If you use the exit sub routine you can not add extra parameters as easily. Also coding structure is good to follow. I try to avoid skipping around in my code and let it be ran from top to bottom(how its suppose to be).
This is a personal opinion though
-
Either is fine with me. They are basically doing the same thing. The Forced method exits the Sub at the beginning and the soft method exits the Sub at the end. Neither is more flexible then the other, the only difference is that they are inverted.