locked
Calling A Function Like a Sub RRS feed

  • Question

  • User-814398470 posted

    I'm sure this is a pretty simple question but I can't seem to find the answer on Google. Say I've got a function that does some stuff and returns a string. I can assign its return value to a variable:

     

    Dim result As String = MyFunction()

     

    But sometimes I want to perform the actions within the function but I don't care about the return value. I know I can simply call the function from code.

     

    Sub Main()

        ' Code
        MyFunction()

        ' More Code

    End Sub

     

    This works and does exactly what I need it to. My question is - are there any issues or pitfalls in doing this? Particularly I want to make sure I'm not introducing any performance or memory leak issues. Thanks.

    Tuesday, March 15, 2011 4:33 PM

Answers

  • User-1179452826 posted

    You're safe as in there are no issues. I would however question the design. I'm very much for command query separation - something that changes something doesn't return anything and something that returns something doesn't change anything. This may not always be applicable, but a violation of this might indicate a method is doing two things (hence sometimes you use the result while at others you don't). So while there's no syntactic error, there may be a semantic one which may lead to brittle design and rigid code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2011 4:51 PM

All replies

  • User551462331 posted

    i doubt there would be any cosiderable performance issue if function is used instead of sub...

    but, if there no possibility or requirment from function to return anything... then u should sub.

    hope this helps...

    Tuesday, March 15, 2011 4:51 PM
  • User1182340765 posted

    I don't see any pitfall or issue with this method. You just ignore the returned value. You could see this kind of thing happens everywhere including in many books and professional software's source code.

    Tuesday, March 15, 2011 4:51 PM
  • User-1179452826 posted

    You're safe as in there are no issues. I would however question the design. I'm very much for command query separation - something that changes something doesn't return anything and something that returns something doesn't change anything. This may not always be applicable, but a violation of this might indicate a method is doing two things (hence sometimes you use the result while at others you don't). So while there's no syntactic error, there may be a semantic one which may lead to brittle design and rigid code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2011 4:51 PM
  • User-814398470 posted

    Thanks for the feedback all. In the specific instance I'm referring to, the code logs error information into a database and returns a more user-friendly error message to be displayed to the client. However, there are instances where I want to log errors in lower layers and not in the UI, and I didn't want to rewrite the code that pretty much does everything I need.

     

    However based on the responses I'll create a sub that does the logging, and then call that from the fucntion when I need to display information to the client,

    Wednesday, March 16, 2011 7:17 AM