Article View: comp.std.c
Article #3409Re: GOTO single exit point (was: Re: How to eliminate GOTO?)
From: Christopher R. Volpe
Date: Fri, 08 Sep 1995 20:11
Date: Fri, 08 Sep 1995 20:11
161 lines
4689 bytes
4689 bytes
In article <42q53c$9v9@news0.rain.rg.net>, Paul Long <plong@perf.com> writes:
>volpe@bart.crd.ge.com (Christopher R. Volpe) wrote:
>[snip:the dead horse that we are beating]
>>Silly contrived example using multiple returns:
>>
>>void manipulate_the_windows(Window *top)
>>{
>> Window *A, *B, *C;
>>
>> if (top == NULL) {
>> return;
>> }
>>
>> do_something_with_window(top);
>> A = get_subwindow(top);
>> if (A == NULL) {
>> return;
>> }
>>
>> do_something_different_with_window(A);
>> B = get_subwindow(A);
>> if (B == NULL) {
>> return;
>> }
>>
>> do_something_with_window(B);
>> C = get_subwindow(B);
>> if (C == NULL) {
>> return;
>> }
>>
>> /* All windows I'm looking for are good, so do it */
>> do_something_weird_with_four_windows(top,A,B,C);
>>
>> return; /* Omit this or not as a matter of taste */
>>}
>
>
>Silly contrived example using one (implicit) return path:
>
>void manipulate_the_windows(Window *top)
>{
> Window *A, *B, *C;
>
> if (top != NULL) {
> do_something_with_window(top);
> A = get_subwindow(top);
>
> if (A != NULL) {
> do_something_different_with_window(A);
> B = get_subwindow(A);
>
> if (B != NULL) {
> do_something_with_window(B);
> C = get_subwindow(B);
>
> if (C != NULL) {
> /* All windows I'm looking for are good, so do it */
> do_something_weird_with_four_windows(top,A,B,C);
> }
> }
> }
> }
>}
Exactly. Now which is easier to read? And which is easier to modify if you
decide you need to check for some other fatal error immediately after
do_something_different_with_window(A)?
Suppose I want the program to print out appropriate error messages? My
program becomes:
void manipulate_the_windows(Window *top)
{
Window *A, *B, *C;
if (top == NULL) {
printf("Error: Top is NULL\n");
return;
}
do_something_with_window(top);
A = get_subwindow(top);
if (A == NULL) {
printf("Error: A is NULL\n");
return;
}
do_something_different_with_window(A);
B = get_subwindow(A);
if (B == NULL) {
printf("Error: B is NULL\n");
return;
}
do_something_with_window(B);
C = get_subwindow(B);
if (C == NULL) {
printf("Error: C is NULL\n");
return;
}
/* All windows I'm looking for are good, so do it */
do_something_weird_with_four_windows(top,A,B,C);
}
What happens to your program?
void manipulate_the_windows(Window *top)
{
Window *A, *B, *C;
if (top != NULL) {
do_something_with_window(top);
A = get_subwindow(top);
if (A != NULL) {
do_something_different_with_window(A);
B = get_subwindow(A);
if (B != NULL) {
do_something_with_window(B);
C = get_subwindow(B);
if (C != NULL) {
/* All windows I'm looking for are good, so do it */
do_something_weird_with_four_windows(top,A,B,C);
}
else
{
printf("Error: C is NULL\n");
}
}
else
{
printf("Error: B is NULL\n");
}
}
else
{
printf("Error: A is NULL\n");
}
}
else
{
printf("Error: Top is NULL\n");
}
}
Notice how the diagnostics grow further and further away from the test which
causes them to be produced? This is what I find difficult to follow. I don't
know about you, but I've shot myself in the foot on more than one occasion as
a result of having one too many or one too few closing braces deeply nested. I
find it much easier to follow code that doesn't have five levels of
indentation.
--
Chris Volpe Phone: (518) 387-7766 (Dial Comm 8*833
GE Corporate R&D Fax: (518) 387-6560
PO Box 8, Schenectady, NY 12301 Email: volpecr@crd.ge.com
Technical Details
Message-ID:
<42q81a$s2t@rdsunx.crd.ge.com>Path:
arc1-ca.pugleaf.net!bart.crd.ge.com!volpeReferences:
<3vn1ht$kj8@martha.utk.edu> <41cke4$gtd@sparcserver.lrz-muenchen.de> <41glno$3a0@caip.rutgers.edu> <41iebe$kec@news0.rain.rg.net> <41pm80$skp@inferno.mpx.com.au> <DE02nC.B58@eskimo.com> <41t0sb$15e@news0.rain.rg.net> <DE6G2z.JJD@crdnns.crd.ge.com> <4258ak$cj7@news0.rain.rg.net> <42hmj1$96o@rdsunx.crd.ge.com> <42l7s7$2id@news0.rain.rg.net> <42n1t0$18q@rdsunx.crd.ge.com> <42q53c$9v9@news0.rain.rg.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!bart.crd.ge.com!volpe From: volpe@bart.crd.ge.com (Christopher R. Volpe) Newsgroups: comp.std.c,comp.lang.c Subject: Re: GOTO single exit point (was: Re: How to eliminate GOTO?) Date: 8 Sep 1995 20:11:22 GMT Organization: GE Corporate Research & Development Sender: volpe@bart (Christopher R. Volpe) Distribution: world Message-ID: <42q81a$s2t@rdsunx.crd.ge.com> References: <3vn1ht$kj8@martha.utk.edu> <41cke4$gtd@sparcserver.lrz-muenchen.de> <41glno$3a0@caip.rutgers.edu> <41iebe$kec@news0.rain.rg.net> <41pm80$skp@inferno.mpx.com.au> <DE02nC.B58@eskimo.com> <41t0sb$15e@news0.rain.rg.net> <DE6G2z.JJD@crdnns.crd.ge.com> <4258ak$cj7@news0.rain.rg.net> <42hmj1$96o@rdsunx.crd.ge.com> <42l7s7$2id@news0.rain.rg.net> <42n1t0$18q@rdsunx.crd.ge.com> <42q53c$9v9@news0.rain.rg.net> Reply-To: volpe@ausable.crd.ge.com NNTP-Posting-Host: bart.crd.ge.com