Click here to Skip to main content
15,883,883 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 37.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
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 
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 
Ah, you are so predictable Smile | :) , so here we go ...

In fact, I have always been very fond of exceptions. It's a nice idea and you can construct situations, where the could really appear to simplify your code. In reality, I never found a program, where they really were worth the trouble. Here are some downsides:

- in an evolving program, you really never stay in control of what code jumps where in which situations. With a lot of discipline you may construct a nicely working program using exceptions. After changing it for a while, you will notice, that exceptions are thrown in places, where you can't use them. So you have to get that under control with even more discipline. A watertight analysis tool would make this a lot easier, but I have not found one yet
- as you have noticed yourself, exceptions introduce something like multiple exits, making the analysis, what your program really does a lot harder
- trying to verify post conditions for methods or functions gets incredibly complicated, probably causing the quality of your code to degrade
- using exceptions is not compatible with common, modern programming models like event driven software
- exceptions work badly in multi threaded applications
- exceptions ruin the usage of RPC etc.
- exceptions slow down your code and unnecessarily increase its size
- safety critical software (avionics, cars, defense etc.) forbid exceptions (and they know what they are doing and why they are doing it)
- find more pros and cons at http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Exceptions#Exceptions[^]

That being said, there are of course nice things to say about exceptions and my point of view is, that you have to be ready to handle them (e.g. in constructors, dynamic allocations etc.) but use them with caution.
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.