Click here to Skip to main content
15,915,855 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 39K   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 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 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti9-Oct-13 22:38
pasztorpisti9-Oct-13 22:38 
Wolfgang_Baron wrote:
Ah, you are so predictable Smile | :) , so here we go ...
This is a technical forum and besides this after exchanging a few posts I think we should avoid being personal. Could you please concentrate on the technical stuff?
Wolfgang_Baron wrote:
- 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.
Not true. Ask if you are interested my opinion in one of these points.
Wolfgang_Baron wrote:
- exceptions slow down your code and unnecessarily increase its size
Partly true, table driven exceptions slow you down only in case of actually thrown exceptions that happens rarely in some programs that use this to indicate errors (like IOExcpeption). Besides this, most programs are not performance critical and the advantages you gain with exception handling outweigh the disadvantages by far.
Wolfgang_Baron wrote:
- safety critical software (avionics, cars, defense etc.) forbid exceptions (and they know what they are doing and why they are doing it)
Not only safety critical software, the largest codebase I'm currently working on forbids exceptions because it is extremely performance critical, but again, this is a problem only in case of the minority of applications. Exceptions are forbidden because of a requirement/technical limitation and not because they wouldnt be able to make the source code and error handling better.

Facts:
- Some languages (C#, java, python, ...) use exceptions and force you to use exceptions to handle errors at their core. The libraries are using exceptions so you have to handle them. In C++ some software and a lot of libraries don't use it (probably because a lot of these libs are rather C libs written by C programmers). Do you work with any modern languages?
- Most applications are simply not performance critical and having a clean and easy-to-maintain source code is the most important. Exception handling helps in that, helps in centralizing error handling, makes sure that errors are not ignored (!!!) and chained exceptions/stacktraces are extremely helpful in locating errors (!!!) even in RPC/networking programs -> stacktraces and chained exceptions can flow over networks from the server to the client and vice versa!!! (Ruins the usage of RPC??? Ridiculous, have you ever used networking/rmi in java or the same in C#???)
- What do you think, why is exception handling a core feature of a lot of modern general purpose programming languages? (C#, Java, ...)
- I don't need to read pros and cons because I've already read enough and besides this I have my own opinions too and I'm happy to discuss the advantages and disadvantages. Fact: Sometimes the advantages simply outweigh the disadvantages. This is a general truth, most decisions balance between advantages and disadvantages especially in case of programming.
Wolfgang_Baron wrote:
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.
Anything can be abused (even a simple if statement). A knife is a nice tool until you start cutting your own fingers with it...
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.