Stop function

S

shapper

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
 
X

xzzy

the following pseudo code is pretty safe:

private bool MyF()
{
bool result = false;
try
{
if ( Condition )
{
// do this
result = true;
}
else
{
// write to log that condition was false
}
}
catch ( Exception ex)
{
}
return result;
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

shapper said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,482
Latest member
AshleighMo

Latest Threads

Top