Results 1 to 7 of 7

Thread: Getting out

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Getting out

    Is this a good way of getting out of a function?

    Code:
                    If f1.AppendLine(strTextMsg, strText) = True Then
                        Return True
                        Exit Function
                    Else
                        Return False
                        Exit Function
                    End If
    This is in code that I'm taking over and finishing and I'm just wondering if I should be skeptical of this logic. The programmer seemed to overly-use "Exit Function" (like right before an exception handler would be called as if he thought if he didn't have it he'd fall right through into the exception handler even when there was no exception).

    Personally, I would've just coded: Return(f1.AppendLine(strTextMsg, strText))

    Thanks.
    Last edited by MMock; Jun 28th, 2010 at 07:11 AM. Reason: Thought I hit preview post...
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Getting out

    Return True
    Exit Function
    Return statement anyways gets you out of the function. So no need to call Exit Function after that. That line will never be executed.

    Return(f1.AppendLine(strTextMsg, strText))
    I also like this way of returning when possible. Just remove the brackets
    Code:
    Return f1.AppendLine(strTextMsg, strText)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Getting out

    while it is valid... and works... I consider it a bad habit. But that could be just me. I subscribe to the single entry, single exit design when it comes to functions/methods.

    -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??? *

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Getting out

    Then hold that thought and I will post more later.

    The issue is it is valid and it does work, but I agree with you. My task is to convert VB6 to .NET and as I am doing that I will try to also improve it. I just have an issue with what was already converted, because to me it's a bit sloppy but I can't really justify the time "fixing" something that isn't broken. But I will be back to ask your advice about some loop control I've been pondering over when I am at that point again.

    Thanks.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

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

    Re: Getting out

    It's probably in the state it's in because it was converted... not rewritten... which aren't the same thing (imho). In which case, the use of exit function (especially right before an error handler), makes sense why it was used so prevalently.

    But, OK, I'll hold that thought.

    -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
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Getting out

    You could put an apostrophe before the Exit Function line, as it does nothing now, but would serve as a minimally functional comment.
    My usual boring signature: Nothing

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Getting out

    That's true... how ever... in this case, I would do one of two things... re-write the function into a one-liner as mentioned in the first post, along with comments on the reason for it. -OR- I'd re-write it as a on-liner, but then take the one line out, remove the function entirely, then replace all occurrences of the function calls with the one-liner... Sometimes, there comes a point, where you just have to say "Really? We *need* a function for that?" ... maybe in VB6 it made sense, but in .NET... really... who are we kidding?

    -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??? *

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