Click here to Skip to main content
15,901,373 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

The goto-less goto!

Rate me:
Please Sign up or sign in to vote.
4.95/5 (12 votes)
25 Jan 2011CPOL 38.8K   4   33
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)


Written By
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

 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti9-Oct-13 23:17
pasztorpisti9-Oct-13 23:17 
Wolfgang_Baron wrote:
They both complicate the flow of control. In fact, people used to do long jumps (non local gotos) to do some of the things people use exceptions for these days.
Exceptions "complicate" the flow in a well defined way while gotos can be used to break the flow in a lot of unexpected/buggy ways (like jumping into a for loop without running its pre-init expression, jumping back, here and there). If you think that exceptions complicate the flow then you are simply not familiar with exception handling. Exceptions centralize error handling and by separating code that does the actual work and the code that handles errors it makes the flow much cleaner and the source becomes easier to read and there is no silently unhandled error (that would be a bug). The flow is much cleaner, in case of successful execution its basically linear and in case of error its a lot of automatic multiple returns (with the execution of some cleanup finally blocks) that is the usual thing someone wants in case of error.
Wolfgang_Baron wrote:
you are not obliged to use a language feature, just because the language has it
In a language where exception handling is a core mechanism (C#, java, python, ...) The whole standard library returns errors in the form of exceptions. You can't do a small step (like opening/writing/closing a file) without having to handle exceptions. In java with checked exceptions you are even more forced to handle them. In modern languages exception usage is not a choice. In C++ where most libs (even stl) can operate without exceptions its a choice.
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron10-Oct-13 2:40
professionalWolfgang_Baron10-Oct-13 2:40 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti10-Oct-13 3:21
pasztorpisti10-Oct-13 3:21 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti7-Oct-13 22:56
pasztorpisti7-Oct-13 22:56 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron9-Oct-13 14:09
professionalWolfgang_Baron9-Oct-13 14:09 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti9-Oct-13 22:38
pasztorpisti9-Oct-13 22:38 
GeneralReason for my vote of 5 The first alternate is the most read... Pin
NicolasG1-Feb-11 8:21
NicolasG1-Feb-11 8:21 
GeneralReason for my vote of 5 IMHO the superior technique. I don't... Pin
dmjm-h31-Jan-11 7:31
dmjm-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.