Click here to Skip to main content
12,396,461 members (36,915 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

12.9K views
4 bookmarked
Posted

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.

You may also be interested in...

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.160721.1 | Last Updated 25 Jan 2011
Article Copyright 2011 by Philippe Mori
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid