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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Roy S194 reputationPosted Aug 2, 2011, 1:39 AM EDT
void method()
{
if (Something)
{
return;
//The method ends now.
}
//...
}
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 2, 2011, 1:37 AM EDT
return;
This is good for methods returning void. Otherwise do return
Zoran
Guest UserRank #1,422 · 1.2K reputationPosted Aug 2, 2011, 12:01 PM EDT
Maybe I'm old-school but that's just my personal preference.
Mike JonsonRank #7,584 · 238 reputationPosted Aug 2, 2011, 12:02 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Aug 2, 2011, 1:05 PM EDT
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.
Mahesh ChandRank #1 · 393.3K reputationPosted Aug 2, 2011, 1:10 PM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 2, 2011, 1:17 PM EDT
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
Sam HobbsRank #107 · 20.5K reputationPosted Aug 2, 2011, 1:55 PM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Aug 2, 2011, 2:31 PM EDT