Click here to Skip to main content
Click here to Skip to main content

The goto-less goto!

By , 25 Jan 2011
 
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
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Surely anyone can program whatever he wants for himself and ...memberWolfgang_Baron7 Apr '11 - 6:07 
GeneralRe: Clearly we have difference experiences - and debuggers. One ...memberEFEaglehouse7 Apr '11 - 4:05 
GeneralRe: The first person wanting to add a debug statement at the end...memberWolfgang_Baron7 Apr '11 - 1:45 
GeneralSorry, but in C++ at least, not being able to add a debug st...memberjefito12 Apr '11 - 3:02 
GeneralMultiple returns look clear only to those, who have written ...memberWolfgang_Baron9 Feb '11 - 5:32 
GeneralRe: Never say never. Sometimes the clearest code is the shortest...memberEFEaglehouse5 Apr '11 - 4:32 
GeneralIn fact, with this pattern where the complex function is spl...memberPhilippe Mori9 Feb '11 - 5:00 
GeneralReason for my vote of 1 People who put more than one return ...memberWolfgang_Baron9 Feb '11 - 4:35 
GeneralReason for my vote of 5 The first alternate is the most read...memberNicolasG1 Feb '11 - 8:21 
Reason for my vote of 5
The first alternate is the most readable and robust way to do this. I always use this method.
GeneralReason for my vote of 5 IMHO the superior technique. I don't...memberdmjm-h31 Jan '11 - 7:31 

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

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