Results 1 to 9 of 9

Thread: Function return coding

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    503

    Function return coding

    I have come across many code examples like this one, where the Return statement is inside an If-Then-Else-EndIf block or a Select Case block.

    Code:
    Private Function UltimateParent(ByVal control as Control) As Control
    
      Do
        If Nothing Is control.Parent
          Return control
        Else
          control = control.Parent
        End If
      Loop
    
    End Function
    The Error List window of the IDE shows "Function 'UltimateParent' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used."

    Modifying this code will eliminate this error from the Error List window.

    Code:
    Private Function UltimateParent(ByVal control as Control) As Control
    
      Do
        If Nothing Is control.Parent
          Exit Do
        Else
          control = control.Parent
        End If
      Loop
    
      Return control
    
    End Function
    What is the "proper" way to code a function's Return?
    Last edited by Mark@SF; Jun 10th, 2024 at 01:02 PM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,621

    Re: Function return coding

    Why is it in a do loop? Just need two lines. Return control if the parent is nothing, otherwise return the control.Parent....

    Code:
    If control.Parent is Nothing then Return control
    Return control.Parent

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,281

    Re: Function return coding

    Quote Originally Posted by Mark@SF View Post
    I have come across many code examples like this one.

    Code:
    Private Function UltimateParent(ByVal control as Control) As Control
    
      Do
        If Nothing Is control.Parent
          Return control
        Else
          control = control.Parent
        End If
      Loop
    
    End Function
    The Error List window of the IDE shows "Function 'UltimateParent' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used."

    Modifying this code will eliminate this error from the Error List window.

    Code:
    Private Function UltimateParent(ByVal control as Control) As Control
    
      Do
        If Nothing Is control.Parent
          Exit Do
        Else
          control = control.Parent
        End If
      Loop
    
      Return control
    
    End Function
    What is the "proper" way to code a function's Return?
    The proper way is to always return a value. Now, technically, in your first example, it is seemingly impossible for that loop to be infinite, since at some point a "parentless" control will be encountered, which will properly return a value. But the compiler can't know that.

    You could do this with recursion rather than the loop, something like this:

    Code:
    Private Function UltimateParent(ByVal control as Control) As Control
      If control.Parent Is Nothing
        Return control
      Else
        Return UltimateParent(control.Parent)
      End If
    End Function
    A loop is fine as well, but yours could be trimmed down to be more straightforward and succinct like this:

    Code:
    Private Function UltimateParent(ByVal control as Control) As Control
    
      Do Until control.Parent Is Nothing
          control = control.Parent
      Loop
    
      Return control
    
    End Function

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,281

    Re: Function return coding

    Quote Originally Posted by techgnome View Post
    Why is it in a do loop? Just need two lines. Return control if the parent is nothing, otherwise return the control.Parent....

    Code:
    If control.Parent is Nothing then Return control
    Return control.Parent

    -tg
    I think the goal is that if there is multiple levels of "parents", to return the "topmost" parent.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,621

    Re: Function return coding

    Quote Originally Posted by OptionBase1 View Post
    I think the goal is that if there is multiple levels of "parents", to return the "topmost" parent.
    Gotcha ... wasn't obvious at first.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    503

    Re: Function return coding

    Quote Originally Posted by OptionBase1 View Post
    I think the goal is that if there is multiple levels of "parents", to return the "topmost" parent.
    Yes, the UltimateParent function finds the top-most control of the target control (usually the form that contains the target control).

    @OptionBase1: Thank you for your code improvement suggestions. I don't think my question was specific enough (so I've added a bit more detail to clarify my original post), but you got the gist of it. Looking at your suggestions, my take-away is that one should construct their code in the most efficient fashion, so that only one Return statement is necessary.
    Last edited by Mark@SF; Jun 10th, 2024 at 01:42 PM.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,276

    Re: Function return coding

    That's a philosophical debate. Some folks feel that every function should have one and only one, return statement. I don't believe in that rule entirely. If there is an early out, I don't have a problem with that. For example, I would write:
    Code:
    If earlyOut Then
     Return
    End If
    While I could also write:
    Code:
    If earlyOut Then
     'set some flag, but don't return
    Else
     'Everything else here.
    End if
    
    Return 'Only here.
    These two will do the same thing, and the cost difference is insignificant. Also, with modern IDE's, where you can easily see the If matched with each End If, this isn't so hard to read. However, I still don't find it as clean, and if there are multiple early out possibilities, this can get quite messy.

    So, I would say the rule is: Try to have one return statement, but don't do anything too drastic to get to that point.
    My usual boring signature: Nothing

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,281

    Re: Function return coding

    Quote Originally Posted by Mark@SF View Post
    Yes, the UltimateParent function finds the top-most control of the target control (usually the form that contains the target control).

    @OptionBase1: Thank you for your code improvement suggestions. I don't think my question was specific enough (so I've added a bit more detail to clarify my original post), but you got the gist of it. Looking at your suggestions, my take-away is that one should construct their code in the most efficient fashion, so that only one Return statement is necessary.
    It's ok to have multiple Return statements when it makes sense to. But it is necessary to have all bases covered with regards to Return statements. Take the following code example:

    Code:
        Private Function GetText() As String
            Dim a As Integer = 1
            a = 2 * a
    
            If a = 2 Then
                Return "Got here"
            End If
        End Function
    Now, examining the code it is obvious that the function will always return the String "Got here". But I still get a compiler warning of "warning BC42105: Function 'GetText' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used." That assessment by Visual Studio is clearly wrong, a null reference exception can never occur here. But it appears possible to VS because the only Return statement is inside of an If statement, and the compilation process isn't "smart" enough to recognize that the inside of that If statement will be reached 100% of the time. My example is a more "visible" case of your original code.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,584

    Re: Function return coding

    If you have a situation where you know it’ll return 2 or three values

    Code:
    Private Function getAString(ByVal aVariable As Integer) As String
        Select Case aVariable
            Case 1 To 3
                Return “String1”
            Case 4 To 6
                Return “String2”
            Case 7 To 9
                Return “String3”
            Case Else
                Return “DefaultString”
        End Select
        Return Nothing
    End Function
    Nothing will never be returned, as there’s a Case Else. It is just necessary to add that in code to satisfy the Returns a value on all paths when the compiler reads it…

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width