Click here to Skip to main content
15,867,190 members
Articles / Programming Languages / C#

The goto-less goto!

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Oct 2011CPOL 5.4K  
The correct way to do this in C++ is to invoke some form of RAII whereby the failure logic happens automatically in an object's destructor.class CleanupObject{public: CleanupObject() : successState( false ) { } void completedOk() { successState = false;...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
9 Feb 2011Wolfgang_Baron
If you really have to put everything into a single function and want to keep the code analyzable and maybe want to be able put some common code at the end, you can always use a success variable. The code does not slow down, as the compiler optimizes the sequenced if-conditions away and produces...
Please Sign up or sign in to vote.
6 Apr 2011testy_proconsul
bool bFailed = false; bFailed |= bFailed ? true : condition1; bFailed |= bFailed ? true : condition2; bFailed |= bFailed ? true : condition3; if( !bFailed ) { PerformActionOnAllSuccess(); ...
Please Sign up or sign in to vote.
25 Jan 2011Philippe Mori
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()){ ...
Please Sign up or sign in to vote.
31 Jan 2011jsc42
It is much easier (and cleaner) to test for success than to test for failure.Viz:if ( condition1 // Note: Test for success, not for fail && condition2 // Will be short-circuited if condition1 fails && condition3 // Will be short-circuited if condition1 or condition 2...
Please Sign up or sign in to vote.
2 Apr 2011Jose David Pujo
Here goes my alternate.I am a C++ programmer. My goal is always to try to make code short and simple.bool bFailed=false;if (!bFailed) bFailed= condition1_fails;if (!bFailed) bFailed= condition2_fails;if (!bFailed) bFailed= condition3_fails;if (bFailed) DoFailedCleanup();else {...
Please Sign up or sign in to vote.
28 Feb 2011Member 4694807
My favorite is a variant of alternative 2.{ ... DoInit(); status=DoAllActions(); DoCleanup(status); ...}int DoAllActions(){ if (condition1_fails) return status1; ... if (condition2_fails) return status2; ... if(conditionN_fails) ...
Please Sign up or sign in to vote.
7 Feb 2011Jörgen Sigvardsson
Usually, gotos are used to clean up resources when exiting a function. I would recommend using the RAII[^] idiom. It also works works well in the presence of exceptions.For C# I would use IDisposable/using. If that's not possible, I'd use a finally clause to clean up.Everything else...
Please Sign up or sign in to vote.
24 Jan 2011Ajay Vijayvargiya 15 alternatives  
The goto-less goto!
Please Sign up or sign in to vote.
29 Apr 2010Sauro Viti
Use C++ exceptions:try{ if (condition1_fails) throw 1; ... if (condition2_fails) throw 2; ... ... if (conditionN_fails) throw N; PerformActionOnAllSuccess(); DoNormalCleanup();}catch (int condition){ printf("The condition %d fails!\n",...
Please Sign up or sign in to vote.
24 Jan 2011Robert S Sharp
I'm a C# programmer. I try to avoid goto at all costs, and generally I try to avoid throwing exceptions for the purpose of controlling the flow of execution. I've always been of the opinion that exceptions are for exceptional situations and indicate errors rather than expected conditions.I...
Please Sign up or sign in to vote.
26 Jan 2011Philippe Mori
If the number of conditions is relatively small, the following code would made sense: bool bFailed = true; if (!condition1_fails) { if(!condition2_fails) { ... if(!conditionN_fails) { bFailed=false; ...
Please Sign up or sign in to vote.
3 Feb 2011guilherme heringer
In C#, you can always use try..finally to add some finalization code.Just write:bool bFailed = true; try{ if(condition1_fails) return; ... if(condition2_fails) return; ... ... if(conditionN_fails) ...
Please Sign up or sign in to vote.
31 Jan 2011rxantos
For Cbool bFailed = true;// foreverfor(;;) { // do something here. if(condition1) { // exit the loop (goto) break; } // do something here. // .... // In case you want to goto to the top instead of exit. if(condition2) { // jump to top of the...
Please Sign up or sign in to vote.
1 Feb 2011abridges
Alternate 8, why not just do this:bool bFailed = true;// In case of an exception.try{ // while bFailed while (bFailed) { // do something here. if (condition1) { // exit the loop (goto) bFalse = false; ...
Please Sign up or sign in to vote.
7 Feb 2011sergiogarcianinja
As this piece of code is normally a function, should be more readable if all of it is put inside a function returning a bool, indicating success or failure.The cleanup function could be only resource deallocation, not a function at all. If it is intended to use with C++ or C#, a try..finally...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions