Hacker News new | ask | show | jobs
by 4dd3r 5165 days ago
I use goto all the time, but only for one coding pattern. It's a very powerful pattern, which allows me to do memory cleanup completely on a per-function basis:

int abc(void *ctx) { int rc = -1;

  if (outcome_of_first() != SUCCESS) {
    my_log(MY_LEVEL, "Log my error");
    goto over;
  }

  if (outcome_of_second() != SUCCESS) {
    my_log(MY_LEVEL, "Log my error");
    goto over;
  }

  ...

  rc = 0;
over: if (something_allocated != NULL) free(something_allocated);

  if (something_else_allocated != NULL)
    free(something_else_allocated);

  return rc;
}