🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3416

Re: GOTO single exit point (was: Re: How to eliminate GOTO?)

#3416
From: John Breckenridge
Date: Tue, 12 Sep 1995 14:56
97 lines
3067 bytes
In article <42q81a$s2t@rdsunx.crd.ge.com>, volpe@bart.crd.ge.com says...
>
>Suppose I want the program to print out appropriate error messages? My 
>program becomes:
>
>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? 

Not necessarily.  Just change your if statements to check for invalid data:

void manipulate_the_windows(Window *top)
{
  Window *A, *B, *C;

  if (top == NULL) {
    printf("Error: Top is NULL\n");
  }
  else 
  {
    do_something_with_window(top);
    A = get_subwindow(top);

    if (A == NULL) {
      printf("Error: A is NULL\n");
    }
    else
    {
      do_something_different_with_window(A);
      B = get_subwindow(A);

      if (B == NULL) {
        printf("Error: B is NULL\n");
      }
      else
      {
        do_something_with_window(B);
        C = get_subwindow(B);

        if (C == NULL) {
          printf("Error: C is NULL\n");
        }
        else
        {
          /* All windows I'm looking for are good, so do it */
          do_something_weird_with_four_windows(top,A,B,C);
        }
      }
    }
  }
}

-- 
John Breckenridge                                Boothroyd Dewhurst, Inc.
johnb@dfma.com                                   all opinions are my own
Technical Details
Message-ID: <9c7cb$93827.9b@orion.dfma.com>
Path: arc1-ca.pugleaf.net!orion.dfma.com!not-for-mail
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> <42q81a$s2t@rdsunx.crd.ge.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c,comp.lang.c
Path: arc1-ca.pugleaf.net!orion.dfma.com!not-for-mail
From: johnb@dfma.com (John Breckenridge)
Subject: Re: GOTO single exit point (was: Re: How to eliminate GOTO?)
Message-ID: <9c7cb$93827.9b@orion.dfma.com>
Date: Tue, 12 Sep 1995 14:56:39 GMT
Distribution: world
Organization: Boothroyd Dewhurst, Inc.
X-Newsreader: WinVN 0.99.5
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> <42q81a$s2t@rdsunx.crd.ge.com>
MIME-Version: 1.0
Content-Type: Text/Plain; charset=ISO-8859-1