Click here to Skip to main content
15,867,895 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

The goto-less goto!

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Feb 2011CPOL 6.3K   2   2
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:

C#
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)


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

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
guilherme 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
EFEaglehouse5-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.