On Sat, 21 Jul 2001, Mike Breuer wrote: >----- Original Message ----- >From: "George Greer" <greerga@CIRCLEMUD.ORG> > >> I'd rather have: >> >> val = badval; >> if ((foo = blah()) == fail) >> return val; >> if ((bar = blarg()) == fail) >> goto cleanup1; >> if ((baz = blarf()) == fail) >> goto cleanup2; >> if ((qux = barg()) == fail) >> goto cleanup3; >> do_something >> val = goodval; >> cleanup(qux); >> cleanup3: >> cleanup(baz); >> cleanup2: >> cleanup(bar); >> cleanup1: >> cleanup(foo); >> return val; > >Interesting example in that it is only relevant if baz, bar and foo are >global. Otherwise, it doesn't matter if you clean them all up at the end, >or clean them up as you go, since blarg(), blarf() and barg() don't use the >previously retrieved values. So, if they are all locals, you can get just >as much mileage out of: This is a separate issue, that 'goto' and global variables aren't always evil, and that power supply labelled "do not open" isn't so dangerous if you know what you're doing. The issue is knowing when you actually know enough to be more than dangerous. The metavariables would be local pointers in the above case, BTW. >if ((foo = blah()) == fail || cleanup(foo) || > (bar = blarg()) == fail || cleanup(bar) || > (baz = blarf()) == fail || cleanup(baz) || > (qux = barg()) == fail || cleanup(qux)) > return badval; >return goodval; This was Novell NetWare client library code so it was much more convoluted than that and wouldn't work in a single if(). In particular, I couldn't just bend the API to my whim and make the cleanup functions return the appropriate values. >As long as cleanup returns zero, this works just as well. It's about the >same efficiency when compiled. I also cleaned up qux, but then you know >better as to whether that is necessary, since it's your example. But >then, we're all just playing Devil's advocate here, right? :-) Yep, forgot cleanup(qux) above 'cleanup3:'. The idea being that the 'goto' example scales for arbitrary functions while the other ways get messy. Your if() works when the planets and API are aligned on proper return values. Tangents... -- George Greer greerga@circlemud.org -- +---------------------------------------------------------------+ | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html | | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html | +---------------------------------------------------------------+
This archive was generated by hypermail 2b30 : 12/06/01 PST