>>>>> "BM" == Brandon McCaig <bamcc...@gmail.com> writes:

  BM> On Thu, Apr 14, 2011 at 1:15 PM, Uri Guttman <u...@stemsystems.com> wrote:

  >> that can still be done very cleanly without gotos. one technique is to
  >> collect all the resources into a structure as you initialize. at any
  >> point when it fails, you return to an outer func which is given
  >> pass/fail results. if it failed, the outer func can free up the
  >> resources in the struct (which it has originally). simple and
  >> clean. also a better way to deal with error handling than goto the end
  >> for cleanup. subs make for useful flow control beyond simple abstraction
  >> and reuse.

  BM> That seems like a lot of extra code to achieve the same result.
  BM> An extra structure for every function, or at least every related
  BM> function, and if you don't want to duplicate the cleanup code
  BM> then also an extra function for every such structure to release
  BM> its resources... This is how I envision your suggestion in code:

  BM> result_t * result = foo_release(foo(arg1, arg2));

  BM> if(result == 0)
  BM> {
  BM>     fprintf(stderr, "Failed to foo.\n");
  BM>     return 1;
  BM> }

  BM> Or maybe:

  BM> foo_result_t * foo_result = foo(arg1, arg2);

  BM> if(!foo_result->success)
  BM> {
  BM>     foo_result_release_all(&foo_result);
  BM>     fprintf(stderr, "Failed to foo.\n");
  BM>     return 1;
  BM> }

  BM> result_t * result = foo_result_release(&foo_result);

  BM> Is this what you had in mind or something completely different?

possibly. i haven't coded in c in too long (after 20 years of it). as i
said, design is more important and you can design around such issues and
not need goto to cleanup. 

  BM> It seems to me that it's much simpler and more reliable to just
  BM> clean it up in one place, inside of the function that failed.
  BM> Returning the bad state to the caller unnecessarily puts the
  BM> responsibility to clean things up on the caller. So instead of
  BM> having a little bit of fuss in one self-contained box you'd be
  BM> spreading fuss throughout the entire code base. :-/

not always. i won't go further here as this is getting way off topic for
beginning perl. simple enough to say goto is never needed and good
designs don't need it either.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to