Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

The goto-less goto!

By , 25 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Alternative 2 can be enhanced by returning a booleen value if we want to code something similar to the original example. A (early) return value of false would indicate a failure which could then be handle by checking the function result.
 
if (!DoAllActions())
{
   DoFailedCleanup();
}
 
bool DoAllActions()
{
   if (condition1_fails)
      return false;
   if (condition2_fails)
      return false;
   ...
   if(!conditionN_fails)
      retrun false;
   PerformActionOnAllSuccess();
   DoNormalCleanup();
   return true;
}
 
Alternatively, if each action are independant, a variation like this might be more appropriate:
if (DoAction1() && 
    DoAction2() && 
    ... && 
    DoActionN())
{
   PerformActionOnAllSuccess();
   DoNormalCleanup();
}
else 
{
   DoFailedCleanup();
}
 
bool DoAction1()
{
   bool condition1_fails = ...;
   return !condition1_fails;
}
...
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Philippe Mori
Web Developer
Canada Canada
Programmer at Maid LABS from 2003 (www.maidlabs.com)
 
Programmer-Analyst at Viasat Geo Technoligies from 1995 to 2002 (www.viasat-geo.com).
 
I have studied at École Polytechnique de Montréal in computer engineering.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberpasztorpisti26-Aug-13 9:34 
GeneralRe: Surely anyone can program whatever he wants for himself and ... PinmemberWolfgang_Baron7-Apr-11 6:07 
Surely anyone can program whatever he wants for himself and I am not insulted (though maybe a bit frustrated), if you continue to produce code with multiple returns. I was only pointing out, that if you want to be nice to other people by keeping your code readable, you will certainly refrain from using multiple returns.
 
As an example, look at the function QueryLocalGroupSIDs() in CoreCode.cpp of the codeproject article "GUI-Based RunAsEx". Clearly, a nice program and a good job. However, the cruel things the author does to the lpData2 and bSuccess variables results in two return statements inside a __finally section (which is undefined behavior during termination handling btw). Most of the code does nothing at all, because bSuccess can only be TRUE if lpData2 is not NULL. There is no point in using two return statements, because just calculating a result value and returning that at the end is always easier to read. You need 5 to 10 times more time to understand what this function actually does. I even go as far as to say, that the author did not understand his own code here, or he would not have produced so much non functional code.
 
Again, my point is only meant as a help for anyone, who is interested in the well being of his readers and of course, you can do with your life whatever you want Smile | :)
GeneralRe: Clearly we have difference experiences - and debuggers. One ... PinmemberEFEaglehouse7-Apr-11 4:05 
GeneralRe: The first person wanting to add a debug statement at the end... PinmemberWolfgang_Baron7-Apr-11 1:45 
GeneralSorry, but in C++ at least, not being able to add a debug st... Pinmemberjefito12-Apr-11 3:02 
GeneralMultiple returns look clear only to those, who have written ... PinmemberWolfgang_Baron9-Feb-11 5:32 
GeneralRe: Never say never. Sometimes the clearest code is the shortest... PinmemberEFEaglehouse5-Apr-11 4:32 
GeneralIn fact, with this pattern where the complex function is spl... PinmemberPhilippe Mori9-Feb-11 5:00 
GeneralReason for my vote of 1 People who put more than one return ... PinmemberWolfgang_Baron9-Feb-11 4:35 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti26-Aug-13 9:27 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron7-Oct-13 3:08 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti7-Oct-13 3:24 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron7-Oct-13 5:10 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti7-Oct-13 5:39 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron7-Oct-13 13:08 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti7-Oct-13 22:37 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron9-Oct-13 14:59 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberPhilippe Mori9-Oct-13 17:35 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti9-Oct-13 22:07 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberPhilippe Mori10-Oct-13 2:24 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti10-Oct-13 2:46 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron9-Oct-13 22:29 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberPhilippe Mori10-Oct-13 2:36 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti9-Oct-13 22:18 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron9-Oct-13 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 25 Jan 2011
Article Copyright 2011 by Philippe Mori
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid