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

Tagged as

The goto-less goto!

, 25 Jan 2011 CPOL
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()){ ...
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)

Share

About the Author

Philippe Mori
Software Developer (Senior)
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 10:34 
GeneralRe: Surely anyone can program whatever he wants for himself and ... PinmemberWolfgang_Baron7-Apr-11 7:07 
GeneralRe: Clearly we have difference experiences - and debuggers. One ... PinmemberEFEaglehouse7-Apr-11 5:05 
GeneralRe: The first person wanting to add a debug statement at the end... PinmemberWolfgang_Baron7-Apr-11 2:45 
The first person wanting to add a debug statement at the end will prove you wrong.
GeneralSorry, but in C++ at least, not being able to add a debug st... Pinmemberjefito12-Apr-11 4:02 
GeneralMultiple returns look clear only to those, who have written ... PinmemberWolfgang_Baron9-Feb-11 6:32 
GeneralRe: Never say never. Sometimes the clearest code is the shortest... PinmemberEFEaglehouse5-Apr-11 5:32 
GeneralIn fact, with this pattern where the complex function is spl... PinmemberPhilippe Mori9-Feb-11 6:00 
GeneralReason for my vote of 1 People who put more than one return ... PinmemberWolfgang_Baron9-Feb-11 5:35 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti26-Aug-13 10:27 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron7-Oct-13 4:08 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti7-Oct-13 4:24 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron7-Oct-13 6:10 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti7-Oct-13 6:39 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron7-Oct-13 14:08 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti7-Oct-13 23:37 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron9-Oct-13 15:59 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberPhilippe Mori9-Oct-13 18:35 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti9-Oct-13 23:07 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberPhilippe Mori10-Oct-13 3:24 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti10-Oct-13 3:46 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron9-Oct-13 23:29 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberPhilippe Mori10-Oct-13 3:36 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pinmemberpasztorpisti9-Oct-13 23:18 
GeneralRe: Reason for my vote of 1People who put more than one return ... PinmemberWolfgang_Baron9-Oct-13 23: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 | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 25 Jan 2011
Article Copyright 2011 by Philippe Mori
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid