rspec/rules/S5944/vbnet/rule.adoc

51 lines
1.3 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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.
2021-04-28 16:49:39 +02:00
== 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
----
2021-04-28 16:49:39 +02:00
== 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
----
2021-04-28 16:49:39 +02:00
== See
* https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/function-statement#returning-from-a-function[.Net documentation - Returning from a Function]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]