| Jason Spence ( @ 2007-11-22 08:00:00 |
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:
The other way: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(); }
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.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();