Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5944

"Return" statements should be used instead of assigning values to function names

    XMLWordPrintable

    Details

    • Message:
      Hide
      * Use a 'Return' statement; assigning returned values to function names is obsolete.
      * Do not make use of the implicit return value.
      Show
      * Use a 'Return' statement; assigning returned values to function names is obsolete. * Do not make use of the implicit return value.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      VB.Net
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      Functions can return values using two different syntaxes. The modern, and correct, way to do it is to use a Return statement. The VB6 way, i.e. old way, is to assign a return value to the function's name .

      The VB6 syntax is obsolete as it was introduced to simplify migration from VB6 projects. The compiler will create a local variable which is implicitly returned when execution exits the function's scope.

      Return statement should be used instead as they are easier to read and understand.

      Noncompliant Code Example

      Public Function FunctionName() As Integer
          FunctionName = 42 ' Noncompliant
      End Function
      
      Public Function FunctionNameFromVariable() As Integer
          Dim Value As Integer = 42
          FunctionNameFromVariable = Value ' Noncompliant
      End Function
      

      Compliant Solution

      Public Function FunctionName() As Integer
          Return 42
      End Function
      
      Public Function FunctionNameFromVariable() As Integer
          Dim Value As Integer = 42
          Return Value
      End Function
      

      See

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            pavel.mikula Pavel Mikula
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: