1

I am curious if Exit Sub, Exit Function, among others should be avoided when coding? I have read in various places that it is a bad practice and that you should, if possible, avoid it.

I have provided a function below to demonstrate what I mean:

Function Test()

    Dim X As Integer
    X = 5

    If X = 10 Then
        Test = True
        Exit Function
    Else
        Test = False
        Exit Function
    End If

End Function

In the case above, the Exit Function isn't necessary since the entire code will be able to finish the entire routine without an issue. But by adding it, will it cause some issues?

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Elias Wick
  • 483
  • 6
  • 21
  • 3
    If it is a `Function` then you should simply **Return**? – Dave B Sep 28 '17 at 07:21
  • An Exit or Return that can avoid nesting If statements tends to improve readability of the code. But can also cause bugs by accidentally bypassing code. Use your programming instinct to make the right choice. – Hans Passant Sep 28 '17 at 07:39

4 Answers4

2

The style you're using is from the VB5 and VBA era, which didn't support Return statement and we needed to assign the return value directly to the function name. In VB.NET you should always use Return assignment. Return will do both the assignment and the exit call in one statement.

Regarding your particular example, my impression is that the Exit Function statement does nothing more than doing a GoTo to the nearest End Function line. So these statements in your case are redundant and might get striped off by the compiler (not sure about this one).

Note that Exit statements become a nightmare from code-readability point of view. I have recently been a victim of this particular problem. Visual Basic is such a verbose language and Exit statements go unnoticed by the reader. A structured If/Else block is FAR more readable.

dotNET
  • 33,414
  • 24
  • 162
  • 251
1

This depends on the current context of your code. If you are within a block (be that a loop, Sub or function etc.) and are attempting to leave, the Exit Statement will do fine.

The Return Statement will also work the same as a Exit Function or Exit Sub, see the answer to this SO question. But personally I prefer the exit statement in a function when I wish to return control to the calling code, but have no value to return.

Michael Hancock
  • 2,673
  • 1
  • 18
  • 37
1

In practically every case, there's a "better" approach than using Exit Function.

The following example will give the same result, but is shorter, and in my opinion much clearer.

Function Test() As Boolean
    Dim X As Integer
    X = 5

    Return (X = 10)
End Function
MatSnow
  • 7,357
  • 3
  • 19
  • 31
0

To clarify a little, as @dotNET said. This is pre .net code. In dot net, you would change your code slightly to

Function Test() As Boolean

    Dim X As Integer
    X = 5

    If X = 10 Then
        Return True
    Else
        Return False
    End If

End Function

This defines test as a function that returns a Boolean result and you use the Return statement to err.. return the result back to the calling statement and exits the function immediately at the aforementioned Return statement

for example ..

Dim result As Boolean
result = test
David Wilson
  • 4,369
  • 3
  • 18
  • 31