Subject: Re: release time?

Re: release time?

From: Peter Stuge <peter_at_stuge.se>
Date: Thu, 21 Jan 2010 21:22:13 +0100

Alexander Lamaison wrote:
> > Would anyone veto changes that reduce mindless code duplication such
> > as above with the use of forward gotos to the end of functions?
>
> Rather than gotos, how about a helper function e.g.:

A function brings a new scope which may not always work. goto is not
a bad thing if used right. I think forward gotos for error handling
is an excellent way to keep code clean, coherent and short:

main() {
  int ret=1;
  char *ptr=NULL;
..
  if(some_error)
    goto done;
  ptr=malloc(1024*1024);
  if(NULL==ptr) {
    perror("malloc");
    goto done;
  }

  /* main stuff */
  ret=0;

done:
  if(ptr)
    free(ptr);
  return ret;
}

> Or am I missing the need for gotos (not got the code in front of me)?

Cleaning up ptr in the above example is the point. Instead of
duplicating cleanup code in every single error handling branch within
main stuff, all cleanup code is in one single location.

That way, there is much less code overall and writing error handling
is greatly simplified, which means more likely to be done and much
more likely to not introduce new bugs because of overlooked cleanup.

//Peter
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-01-21