Click here to Skip to main content
13,901,425 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 31 Mar 2011
Licenced CPOL

The goto-less goto!

, 2 Apr 2011
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 {...


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

9 Feb 2011
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...
6 Apr 2011
bool bFailed = false; bFailed |= bFailed ? true : condition1; bFailed |= bFailed ? true : condition2; bFailed |= bFailed ? true : condition3; if( !bFailed ) { PerformActionOnAllSuccess(); ...
25 Jan 2011
Philippe 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()){ ...
31 Jan 2011
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...
28 Feb 2011
Member 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) ...
7 Feb 2011
Jö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...
24 Jan 2011
Ajay Vijayvargiya
15 alternatives  
The goto-less goto!
29 Apr 2010
Sauro 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",...
24 Jan 2011
Robert 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...
26 Jan 2011
Philippe 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; ...
3 Feb 2011
guilherme 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) ...
31 Jan 2011
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...
1 Feb 2011
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; ...
7 Feb 2011
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...
12 Oct 2011
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;...


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


About the Author

Jose David Pujo
Software Developer (Senior) Freelancer
Spain Spain
No Biography provided

| | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 2 Apr 2011
Article Copyright 2011 by Jose David Pujo
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid