On Wed, Jun 1, 2011 at 9:23 PM, Peter Stuge <[email protected]> wrote: > Indeed what you get with good use of goto. The rule is basically to > only goto in the forward direction within a function. It's very > simple, and it can make code *tremendously* more readable. > > int thing(params) { > if (error) > goto handle_errors; > > if (other_error) > goto handle_errors; > > if (third_error) > goto handle_errors; > > /* all conditions have been checked, it's safe to do the thing */ > > for (num = 0; num < times; num++) > do_thing(); > > return 0; > > handle_errors: > clean_up(); > return 1; > }
I tend to agree on this. This method is used many times and seems like very good coding practice to me and one of the very rare examples where gotos are actually useful. I would stick to this whenever possible. Except that I would declare one status variable in the beginning, int rc = SUCESS, and then change it during the function. In case of the error I would do rc = FAILURE; goto handle_errors; and then instead of : > handle_errors: > clean_up(); > return 1; > } I'd do : handle_errors: clean_up(); return rc; } It depends, off course. BR, Drasko _______________________________________________ Openocd-development mailing list [email protected] https://lists.berlios.de/mailman/listinfo/openocd-development
