Click here to Skip to main content
15,888,527 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.2K   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
Philippe Mori10-Oct-13 2:24
Philippe Mori10-Oct-13 2:24 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti10-Oct-13 2:46
pasztorpisti10-Oct-13 2:46 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron9-Oct-13 22:29
professionalWolfgang_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
Philippe Mori10-Oct-13 2:36 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti9-Oct-13 22:18
pasztorpisti9-Oct-13 22:18 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
Wolfgang_Baron9-Oct-13 22:59
professionalWolfgang_Baron9-Oct-13 22:59 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti9-Oct-13 23:17
pasztorpisti9-Oct-13 23:17 
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 
pasztorpisti wrote:
If you think that exceptions complicate the flow then you are simply not familiar with exception handling.

If you are not familiar with the code complexity caused by the usage of exceptions, then you are simply not familiar with exception handling and software analysis. Maybe you have that cool, bullet proof software analysis tool, which tells you, what happens behind the scenes and in which places things can happen, which you would not expect. Please tell us about it.
To trace the usage of exceptions, compilers would have to make use of exception specifications, which even Visual C++ does not, as you can read on http://msdn.microsoft.com/en-us/library/wfa0edys.aspx[^]. It's either "the function throws nothing" or "the function may throw anything", which just ruins your day.
As I said, I like the concept of exceptions, but in real life, given the current support by programming tools, the costs usually outweigh the benefits. Code, which I had to refactor, because exceptions were not allowed always came out easier to understand and easier to reason about and thus faster and safer to modify, extend or otherwise maintain.
pasztorpisti wrote:
In a language where exception handling is a core mechanism (C#, java, python, ...) The whole standard library returns errors in the form of exceptions. ...

Wolfgang_Baron wrote:
If you are forced to incorporate code, that makes use of exceptions, you will, of course, have to deal with them.

All right, as I am starting to answer your quotes with my quotes, I will stop answering your never ending praise for your counterproductive programming practices (which I used to practice too, until I discovered, that it did not serve me well). You can either try out my advice or just stick to whatever you did before. I have no problem with that.
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.