Click here to Skip to main content
15,895,494 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.6K   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
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 
GeneralRe: Reason for my vote of 1People who put more than one return ... Pin
pasztorpisti10-Oct-13 3:21
pasztorpisti10-Oct-13 3:21 
So you are saying that programming in C# or Java, Delphi, python, ... is counterproductive? I wouldn't think so, exception handling has been chosen as an error handling mechanism in these languages with a reason. The extreme static analyzation you are talking about is quite rarely needed in practice: in case of some embedded devices (that are usually programmed in C and this language is primitive engough to be relatively easy to analyze). There are some other fields where software has to be "theoretically correct" and bug free like in case of financial auto-traders. A lot of companies that do this are using haskell that has superb static analyzer tools and the code is automatically free of side-effect related bugs and is a good candidate for easy to parallelization.

Even if a piece of software has to be statically analyzed, thrown exceptions for functions are much well defined in modern languages and exceptions affect only a few analyzation techniques. In C++ exception handling is not too well defined/designed. Static analyzation is not a good excuse for not using exceptions. Exceptions may make the work for a static analyzer harder but they make the work for a programmer much easier and while a static analyzer has to be written only once, programmers write much more code with the aid of exceptions. Besides this exceptions affect only a set of the features of a static analyzers.

We regularly run a static analyzer on our C++ codebase (every month) but its much worse (even without using exceptions) than a static analyzer for a modern language that has a reason: C++ is very hard to parse language with full of heritage features and not well designed exception handling mechanism. It is a very hard to analyze in contrast to a modern language including the flow control induced by exception handling. I've used static analyzers for java too and they were much more reliable and better than any analyzer for C++ despite the fact that exception handling is a very core feature of java. If exceptions are well defined then the possible flows of control can be tracked very well by an analyzer. I wouldn't compare the usability and reliability of C++ dev tools to that of java or C# tools and the only reason is the unnecessary complexity of C/C++ that accumulated through the years.
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.