Results 1 to 7 of 7

Thread: Exit Sub... or If then statement better programming?

  1. #1

    Thread Starter
    Addicted Member Dim A's Avatar
    Join Date
    Jul 2000
    Posts
    201
    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?

  2. #2
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602
    I usually prefer to use Exit Sub. Just a personal preference I guess.

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    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

  4. #4
    Guest
    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]

  5. #5
    Guest
    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

  6. #6
    Guest
    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

  7. #7
    Guest
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width