Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

The goto-less goto!

, 3 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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) ...
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)     
          return;  
     bFailed=false;  
     PerformActionOnAllSuccess();
}
finally
{
     if(bFailed)     
          DoFailedCleanup();  
     else     
          DoNormalCleanup();
}
 
But I don't think it's a good practice.
We shouldn't use goto not because it's an ugly word. We shouldn't use goto because its behavior implies poor code readability, and all alternatives that just change the words, but not the bad behavior, should be avoided.

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralIt´s not a tired cliche. It´s a statement. I'm not talking a... Pinmemberguilherme heringer5-Apr-11 9:07 
GeneralIt's a tired cliche that mere use of a goto makes your code ... PinmemberEFEaglehouse5-Apr-11 4:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 3 Feb 2011
Article Copyright 2011 by guilherme heringer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid