0

Should a function that originally doesn't return a value return Nothing anyway (as Visual Studio puts a green squiggle in the End Function statement asking if you'd forget to return a value), or I can ignore VS' tip? What is the impact in returning Nothing, or anything at all?

tl;dr: Should this example code return Nothing or it is not needed? Elucidations are really welcome!

    Public Shared sCaminhoGravacaoXML As String = "C:\XMLData\"

    Public Shared Function VerificaPastasXML()

        If Not IsNothing(sCaminhoGravacaoXML) And sCaminhoGravacaoXML <> "" Then

            Dim sRaiz As String = sCaminhoGravacaoXML

            If Not FileIO.FileSystem.DirectoryExists(sRaiz) Then
                FileIO.FileSystem.CreateDirectory(sRaiz)
            End If

            If Not Directory.Exists(sRaiz & "tempXML") Then
                Dim diInfo As DirectoryInfo = Directory.CreateDirectory(sRaiz & "tempXML")
                diInfo.Attributes = FileAttributes.Directory Or FileAttributes.Hidden
            End If

        Else
            sErroBaixaXML = "Não foi possível montar a estrutura de pastas para os arquivos" & vbCrLf & "XML de NFe, favor consultar os parâmetros da filial!"
        End If

    End Function

4 Answers4

5

Should this example code return Nothing or it is not needed?

Since it never returns a value, it should be a Sub. Function implies to the reader that a value is returned, so your function looks buggy as it is now, confusing everyone who has to maintain it.

Technically, a Function automatically returns the default value of the return data type if there is no explicit return statement. Thus, your function (which is implicitly declared with a return type of Object)¹ would return Nothing; a function with a return type of Integer would return 0, etc.

Since forgetting to return a value is a common cause of errors, the warning is here to help you. If you want to return the default value, it is good practice to use an explicit return statement to make that clear.


¹ Please consider activating Option Strict to avoid such implicit declarations.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

A function in vb.net must return something, compared with subs, that doesn't have a return value. If you don't want to return a value, just change it from Function to Sub.

Oscar
  • 13,594
  • 8
  • 47
  • 75
0

If you are not returning a value, and this is in fact VB.NET - consider using Sub instead of Function, which implies you are not returning a value.

However, in most other languages, it's ok to not return anything as the function will return null / Nothing on it's own.

Further, some code analysis tools will as you to always have a return statement one way or the other, though, so some developers will prefer you to explicitly state that for readability.

Jim Ankrom
  • 66
  • 1
  • 2
0
Public Shared Function VerificaPastasXML() As Boolean

and

    If String.IsNullOrEmpty(sCaminhoGravacaoXML) Then Return False
    Try
        ' ... doSomething (technically you should handle exceptions separately, or check for return values instead.
        Return True
    Catch ex As Exception
        sErroBaixaXML = "Não foi possível montar a estrutura de pastas para os arquivos" & vbCrLf & "XML de NFe, favor consultar os parâmetros da filial!"
        Return False
    End Try

you should return something to notify the calling procedure if the operation succeeded or failed.

tbh though, the logic to create the folder/file needs some work.

ie: If Not VerificaPastasXML() Then : MsgBox("the operation failed") : Else : doSomething() : End If.

porkchop
  • 401
  • 3
  • 8