|
-
Jun 28th, 2010, 07:09 AM
#1
Thread Starter
PowerPoster
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.
-
Jun 28th, 2010, 07:21 AM
#2
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)
-
Jun 28th, 2010, 07:54 AM
#3
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
-
Jun 28th, 2010, 08:01 AM
#4
Thread Starter
PowerPoster
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.
-
Jun 28th, 2010, 08:08 AM
#5
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
-
Jun 28th, 2010, 09:30 AM
#6
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
 
-
Jun 28th, 2010, 09:44 AM
#7
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|