gotos was: open source awareness [was Re: [FoRK] Fwd: [Processing] download the new beta release!)

Justin Mason jm at jmason.org
Fri May 20 14:33:58 PDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Joe Barrera writes:
> So what do you think about gotos?
> 
> A lot of people say they're bad, but I say they're good.
> 
> Just trying to start a stimulating and original technical discussion,
> I once wrote this program that NEEDED a goto, otherwise
> it would have been too slow for the customer.

excellent!  I can tie in both topics -- open sores and gotos,
with this link:

http://blogs.msdn.com/oldnewthing/archive/2005/01/14/352949.aspx

Raymond Chen (of MS) discusses the hazards of exceptions, and 
somewhere in the comments Christoph Hellwig (linux kernel hacker)
crops up with this:


  re: Cleaner, more elegant, and harder to recognize
  Monday, January 17, 2005 8:08 AM by Christoph Hellwig

  I'm a bit suprised no one brought the goto cleanup style used e.g. about
  everywhere in the Linux kernel up. I'd look something like (hope the
  pasting doesn't screw up the indentation):

  BOOL ComputeChecksum(LPCTSTR pszFile, DWORD* pdwResult)
  {
    BOOL fRc = FALSE;
    HANDLE h, hfm;
    void *pv;
    DWORD dwHeaderSum;

    h = CreateFile(pszFile, GENERIC_READ, FILE_SHARE_READ,
            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (h == INVALID_HANDLE_VALUE)
      goto out;

    hfm = CreateFileMapping(h, NULL, PAGE_READ, 0, 0, NULL);
    /* XXX(hch): why are we not checking for INVALID_HANDLE_VALUE here? */
    if (!hfm)
      goto out_close_h;

    pv = MapViewOfFile(hfm, FILE_MAP_READ, 0, 0, 0);
    if (!pv)
      goto out_close_hfm;

    if (!CheckSumMappedFile(pv, GetFileSize(h, NULL),
                &dwHeaderSum, pdwResult))
      goto out_unmap_pv;

    fRc = TRUE;

  out_unmap_pv:
    UnmapViewOfFile(pv);
  out_close_hfm:
    CloseHandle(hfm);
  out_close_h:
    CloseHandle(h);
  out:
    return fRc;
  }

  In my opinion this is a lot better to read than any of the variants
  above, OTOH religious goto-haters will probably try to kill me now.. 


FWIW, I agree with him, and commonly use this goto pattern.

- --j.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Exmh CVS

iD4DBQFCjlfGMJF5cimLx9ARAh34AJd3kVx1SnHrYcX27boVGWbtg98aAKCc13J1
Wd+G5By2JlPrYhy4eWw74Q==
=gn9V
-----END PGP SIGNATURE-----



More information about the FoRK mailing list