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

  BM> On Thu, Apr 14, 2011 at 10:47 AM, Uri Guttman <u...@stemsystems.com> 
wrote:
  >> then you don't know how to code well in c. it may not be as
  >> nice as perl for some flow control things but goto is never
  >> needed in c either. i have seen it used and there are always
  >> better ways to code it than with gotos. you just need to know
  >> other techniques and none of them are convoluted. a better
  >> design helps too.

  BM> I often use gotos in C for error handling within a function. If
  BM> you're allocating resources and something later on fails then you
  BM> usually want (or need) to clean up those resources before
  BM> returning. I've seen a lot of people duplicate the same cleanup
  BM> code over and over again. I hate doing that. Goto makes it much
  BM> cleaner. Just because you can make spaghetti with goto doesn't
  BM> mean goto always has to make spaghetti. You do need to be careful
  BM> though and assess any given situation carefully.

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.

it takes a certain mindset for a coder to see how to factor out error
handling into a higher sub but it isn't that hard. goto just shouldn't
be in your vocabulary. there is no reason for it at all in any decent
language, especially perl.

note: perl's magic goto doesn't count as it really isn't a classic
goto. it is a sub replacement call.

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