Click here to Skip to main content
11,637,415 members (64,337 online)
Click here to Skip to main content

Tagged as

The goto-less goto!

, 7 Feb 2011 CPOL 1.1K 2
Rate this:
Please Sign up or sign in to vote.
As this piece of code is normally a function, should be more readable if all of it is put inside a function returning a bool, indicating success or failure.The cleanup function could be only resource deallocation, not a function at all. If it is intended to use with C++ or C#, a try..finally...
As this piece of code is normally a function, should be more readable if all of it is put inside a function returning a bool, indicating success or failure.

The cleanup function could be only resource deallocation, not a function at all. If it is intended to use with C++ or C#, a try..finally block should be more appropriate.

This piece of code use a try block, which is not super performatic at all, but ensure that all allocated resource are properly freed.

  bool DoOperations()
  { 
    try
    {
      if(condition1_fails)
      {
        return false;
      }
      ...
      if(condition2_fails)
      {
        return false;
      }
      ...
      ...
      if(conditionN_fails)
      {
        break false;
      }
 
      PerformActionOnAllSuccess();
 
      return true;
    }
    finally
    {
      if (condition1_resource != null)
      {
        // free condition1_resource
      }
 
      if (condition2_resource != null)
      {
        // free condition2_resource
      }
 
      ...
 
      if (conditionN_resource != null)
      {
        // free conditionN_resource
      }
    }
  }

License

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

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 7 Feb 2011
Article Copyright 2011 by sergiogarcianinja
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid