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

The goto-less goto!

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
2 Apr 2011CPOL 9.2K   2   4
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

License

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


Written By
Software Developer (Senior) Freelancer
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralFirst It Does Work and Two I Think your missing Your If Stat... Pin
charles henington7-Apr-11 18:22
charles henington7-Apr-11 18:22 
Generalhow about this bool bFailed = false; if (!bFailed) bFailed ... Pin
charles henington6-Apr-11 14:32
charles henington6-Apr-11 14:32 
GeneralRe: Good approach, but I think that it doesn't work: bFailed = ... Pin
Jose David Pujo6-Apr-11 21:12
Jose David Pujo6-Apr-11 21:12 
GeneralReason for my vote of 4 For simple code, this would work wel... Pin
EFEaglehouse5-Apr-11 4:00
EFEaglehouse5-Apr-11 4:00 
Reason for my vote of 4
For simple code, this would work well. But this doesn't fit my typical coding style. Why set a flag to a condition and have a separate test when you can just test the condition directly and execute a goto. As a practical matter, this boils down to personal preference.

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.