shapper wrote:
Hello,
On the start of a function I need to verify a condition.
If it is false I want the rest of the code to not run.
My question is:
should I put all the function code inside an IF or is there a command
that I should use that interrupts the function?
My function is as follows:
Private Sub MyF()
End Sub
Thanks,
Miguel
What you use is mostly a matter of taste and conviction.
Some say that a method should only have one exit point. The reason is
that it's easier to follow the flow in the method if you know that it
always ends in the same way.
If you follow this advice is up to you, and how complex your method is.
A method that is only a few lines isn't that hard to follow even if it
has more than one exit point. On the other hand, if it's not hard to
accomplish, you can just as well follow the advice.
To exit from a Sub, use the Exit Sub command. Example:
Sub ASmallDemoSub(input as String)
If String.IsNullOrEmpty(input) Then
Exit Sub
End If
'... do something
End Sub
It might be just as easy to keep the single exit point:
Sub ASmallDemoSub(input as String)
If Not String.IsNullOrEmpty(input) Then
'... do something
End If
End Sub
--
Göran Andersson
_____
http://www.guffa.com