65.9K
CodeProject is changing. Read more.
Home

The goto-less goto!

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (2 votes)

Apr 1, 2011

CPOL
viewsIcon

9991

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

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 { 
  PerformActionOnAllSuccess(); 
  DoNormalCleanup();
} 

Pros

  • Short and simple to read
  • You can set debug breakpoints in any condition to test return values
  • No functions / exceptions needed