Click here to Skip to main content
11,432,241 members (60,391 online)
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 Pin
pasztorpisti26-Aug-13 10:34
memberpasztorpisti26-Aug-13 10:34 
GeneralRe: Surely anyone can program whatever he wants for himself and ... Pin
Wolfgang_Baron7-Apr-11 7:07
memberWolfgang_Baron7-Apr-11 7:07 
GeneralRe: Clearly we have difference experiences - and debuggers. One ... Pin
EFEaglehouse7-Apr-11 5:05
memberEFEaglehouse7-Apr-11 5:05 
GeneralRe: The first person wanting to add a debug statement at the end... Pin
Wolfgang_Baron7-Apr-11 2:45
memberWolfgang_Baron7-Apr-11 2:45 
GeneralSorry, but in C++ at least, not being able to add a debug st... Pin
jefito12-Apr-11 4:02
memberjefito12-Apr-11 4:02 
GeneralMultiple returns look clear only to those, who have written ... Pin
Wolfgang_Baron9-Feb-11 6:32
memberWolfgang_Baron9-Feb-11 6:32 
GeneralRe: Never say never. Sometimes the clearest code is the shortest... Pin
EFEaglehouse5-Apr-11 5:32
memberEFEaglehouse5-Apr-11 5:32 
GeneralIn fact, with this pattern where the complex function is spl... Pin
Philippe Mori9-Feb-11 6:00
memberPhilippe Mori9-Feb-11 6:00 
GeneralReason for my vote of 1 People who put more than one return ... Pin
Wolfgang_Baron9-Feb-11 5:35
memberWolfgang_Baron9-Feb-11 5:35 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti26-Aug-13 10:27
memberpasztorpisti26-Aug-13 10:27 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron7-Oct-13 4:08
memberWolfgang_Baron7-Oct-13 4:08 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti7-Oct-13 4:24
memberpasztorpisti7-Oct-13 4:24 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron7-Oct-13 6:10
memberWolfgang_Baron7-Oct-13 6:10 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti7-Oct-13 6:39
memberpasztorpisti7-Oct-13 6:39 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron7-Oct-13 14:08
memberWolfgang_Baron7-Oct-13 14:08 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti7-Oct-13 23:37
memberpasztorpisti7-Oct-13 23:37 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron9-Oct-13 15:59
memberWolfgang_Baron9-Oct-13 15:59 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Philippe Mori9-Oct-13 18:35
memberPhilippe Mori9-Oct-13 18:35 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti9-Oct-13 23:07
memberpasztorpisti9-Oct-13 23:07 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Philippe Mori10-Oct-13 3:24
memberPhilippe Mori10-Oct-13 3:24 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti10-Oct-13 3:46
memberpasztorpisti10-Oct-13 3:46 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron9-Oct-13 23:29
memberWolfgang_Baron9-Oct-13 23:29 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Philippe Mori10-Oct-13 3:36
memberPhilippe Mori10-Oct-13 3:36 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti9-Oct-13 23:18
memberpasztorpisti9-Oct-13 23:18 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron9-Oct-13 23:59
memberWolfgang_Baron9-Oct-13 23:59 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti10-Oct-13 0:17
memberpasztorpisti10-Oct-13 0:17 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron10-Oct-13 3:40
memberWolfgang_Baron10-Oct-13 3:40 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti10-Oct-13 4:21
memberpasztorpisti10-Oct-13 4:21 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti7-Oct-13 23:56
memberpasztorpisti7-Oct-13 23:56 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron9-Oct-13 15:09
memberWolfgang_Baron9-Oct-13 15:09 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti9-Oct-13 23:38
memberpasztorpisti9-Oct-13 23:38 
GeneralReason for my vote of 5 The first alternate is the most read... Pin
NicolasG1-Feb-11 9:21
memberNicolasG1-Feb-11 9:21 
GeneralReason for my vote of 5 IMHO the superior technique. I don't... Pin
dmjm-h31-Jan-11 8:31
memberdmjm-h31-Jan-11 8:31 

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
Web02 | 2.8.150428.2 | Last Updated 25 Jan 2011
Article Copyright 2011 by Philippe Mori
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid