S3385 — "Exit" statements should not be used

Language
VB.NET
Type
Code smell
Severity
Major
Tags
brain-overload, bad-practice

Why is this an issue?

Exit Function, Exit Property, and Exit Sub are all poor, less-readable substitutes for a simple Return, and if used with code that should return a value (Exit Function and in some cases Exit Property) they could result in a NullReferenceException.

This rule raises an issue for all their usages.

Noncompliant code example


Public Class Sample

  Public Sub MySub(Condition As Boolean)
    If Condition Then Exit Sub                  ' Noncompliant
    ' ...
  End Sub

  Public Function MyFunction(Condition As Boolean) As Integer
    If Condition Then
        MyFunction = 42
        Exit Function              ' Noncompliant
    End If
    ' ...
  End Function

End Class

Compliant solution


Public Class Sample

  Public Sub MySub(Condition As Boolean)
    If Condition Then Return                  ' Noncompliant
    ' ...
  End Sub

  Public Function MyFunction(Condition As Boolean) As Integer
    If Condition Then Return 42
    ' ...
  End Function

End Class

↑ Back to top