65.9K
CodeProject is changing. Read more.
Home

The goto-less goto!

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (5 votes)

Jan 31, 2011

CPOL
viewsIcon

9311

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

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 fails
   ...
   && conditionn
   )
{
   PerformActionOnAllSuccesses(); 
   DoNormalCleanUp(); 
}  // if
else
   DoFailedCleanUp();