Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

The goto-less goto!

, 2 Apr 2011
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Jose David Pujo
Software Developer (Senior) Prosoft
Spain Spain
No Biography provided

Comments and Discussions

 
GeneralFirst It Does Work and Two I Think your missing Your If Stat... Pinmembercharles henington7-Apr-11 18:22 
Generalhow about this bool bFailed = false; if (!bFailed) bFailed ... Pinmembercharles henington6-Apr-11 14:32 
GeneralRe: Good approach, but I think that it doesn't work: bFailed = ... PinmemberJose David Pujo6-Apr-11 21:12 
Good approach, but I think that it doesn't work:
 
bFailed = condition1_fails = condition2_fails = condition3_fails;
 
Doesnt's shortcircuit and maybe you get a Syntax error
Better:
 
bFailed = condition1_fails || condition2_fails || condition3_fails;
GeneralReason for my vote of 4 For simple code, this would work wel... PinmemberEFEaglehouse5-Apr-11 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 2 Apr 2011
Article Copyright 2011 by Jose David Pujo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid