Click here to Skip to main content
13,053,994 members (64,359 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3K views
2 bookmarked
Posted 31 Jan 2011

The goto-less goto!

, 3 Feb 2011
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

This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

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

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 3 Feb 2011
Article Copyright 2011 by guilherme heringer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid