Hello
I need if the condition (if), need exit from Method.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Roy SPosted Aug 2, 2011, 5:39 AM
void method()
{
if (Something)
{
return;
//The method ends now.
}
//...
}
VulpesPosted Aug 2, 2011, 6:31 PM
Sam HobbsPosted Aug 2, 2011, 5:55 PM
So let me emphasize what I am saying. I think that if a complicated piece of logic is used to avoid using multiple return statements, then I think it is better to use multiple return statements.
Zoran HorvatPosted Aug 2, 2011, 5:17 PM
type method(args)
{
// In public/protected/internal methods verify arguments
// and throw argument related exceptions;
// in private methods do not spend resources to check this and do not throw exceptions
// Declare return variable
// Perform task and keep return variable up to date so that it contains result
// Return result if method returns non-void
}
I use similar structure in stored procedures as well.
This structure is very resistant to later changes in code and bugs are rare compared to arbitrary method structure and especially when compared to methods which return value from the middle.
Zoran
Mahesh ChandPosted Aug 2, 2011, 5:10 PM
Sam HobbsPosted Aug 2, 2011, 5:05 PM
Note that there is no difference between Zoran's and Roy's answer; I sure think that Zoran deserves the credit. My guess is that the two posts were made essentially in parrallel, which is very possible. Otherwise if it were me instead of Roy, I would have said that Zoran's reply is an answer before offering an alternative.
Mike JonsonPosted Aug 2, 2011, 4:02 PM
Guest UserPosted Aug 2, 2011, 4:01 PM
Maybe I'm old-school but that's just my personal preference.
Zoran HorvatPosted Aug 2, 2011, 5:37 AM
return;
This is good for methods returning void. Otherwise do return
Zoran