Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!utcsri!flaps From: flaps@utcsri.UUCP Newsgroups: comp.lang.c Subject: Re: goto's in C: an opinion... Message-ID: <5120@utcsri.UUCP> Date: Tue, 21-Jul-87 22:43:41 EDT Article-I.D.: utcsri.5120 Posted: Tue Jul 21 22:43:41 1987 Date-Received: Thu, 23-Jul-87 00:41:55 EDT References: <3289@bigburd.PRC.Unisys.COM> <7571@beta.UUCP> <6603@think.UUCP> Reply-To: flaps@utcsri.UUCP (Alan J Rosenthal) Organization: University of Toronto Lines: 66 Keywords: C, goto, style Summary: In article <6603@think.UUCP> rst@godot.think.com.UUCP (Robert Thau) writes: >Error handling is another spot where goto's are often the lesser evil. >Take the code: > > some_func() > { > lock_the_lock(); open_the_faucet(); > > while(...) { > if ((buf = malloc(bufsize)) == NULL) goto error; > if (readabuf(myfile, buf) == ERRORVALUE) goto error; > ... process the buffer ... > if (... the data was bogus ...) goto error; > ... do some more ... > } > while (some_condition) prepare_the_report(); > print_the_report(); > error: > close_the_faucet(); unlock_the_lock(); > } > >There are two ways to eliminate the goto's that I'm aware of. [two not very appealing ways] Here's the nice way to eliminate the gotos. No extra charge for the improved vertical spacing. :-) some_func() { lock_the_lock(); open_the_faucet(); process_until_error(); close_the_faucet(); unlock_the_lock(); } process_until_error() { while(...) { if ((buf = malloc(bufsize)) == NULL) return; if (readabuf(myfile, buf) == ERRORVALUE) return; ... process the buffer ... if (... the data was bogus ...) return; ... do some more ... } while (some_condition) prepare_the_report(); print_the_report(); } -- // Alan J Rosenthal // \\ // flaps@csri.toronto.edu, {seismo!utai or utzoo}!utcsri!flaps, \// flaps@toronto on csnet, flaps at utorgpu on bitnet. "To be whole is to be part; true voyage is return."