Jason Spence ([info]jspence) wrote,
@ 2007-11-22 08:00:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
One of the style things that programmers in {}; (C, Java, ...) languages run into when they enter the real world is the fight to have exceptional or non-exceptional code first. An example of exceptional-first:

rc = dostuff();
if(BAD(rc)) {
  goto out;
}

rc = do_more_stuff();
if(BAD(rc)) {
  goto out;
}

rc = yet_more_stuff();
if(BAD(rc)) {
  goto out;
}

finish_up();

out:
if(did_stuff) { free_dostuff(); }
if(did_do_more_stuff) { cleanup_do_more_stuff(); }
if(did_yet_more_stuff) { undo_yet_more_stuff(); }
The other way:

rc = dostuff();
if(GOOD(rc)) {
  rc = do_more_stuff();
  if(GOOD(rc)) {
    rc = yet_more_stuff();
    if(GOOD(rc)) {
      finish_up();
    }
    undo_yet_more_stuff();
  }
  clean_up_do_more_stuff();
}
free_dostuff();
There are many arguments for and against either style, and I'm going to analyze them over the next few days. For now, think about your initial reactions to both styles.



Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…