Re: Poll: global buffers

From: George Greer (greerga@circlemud.org)
Date: 07/20/01


On Sat, 21 Jul 2001, Thomas Jones wrote:

>George Greer wrote:
>
>>Oh, why? What's wrong with global variables in your opinion?
>>
>>Not that I'm encouraging anything, but I'm interested in knowing why.
>
>Probably nothing more than it being beaten into me by a professor in
>college.  As I said, I tend to use them but cringe everytime I do.

I tend to believe the warnings on 'goto' and global variables are sometimes
overstated.  Sometimes their use makes perfect sense.  Frequently it
doesn't.  The difference is in knowing when.

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;

        cleanup3:
          cleanup(baz);
        cleanup2:
          cleanup(bar);
        cleanup1:
          cleanup(foo);
          return val;

than:

          if ((foo = blah()) == fail)
            return badval;
          if ((bar = blarg()) == fail) {
            cleanup(foo);
            return badval;
          }
          if ((baz = blarf()) == fail) {
            cleanup(bar);
            cleanup(foo);
            return badval;
          }
          if ((qux = barg()) == fail) {
            cleanup(baz);
            cleanup(bar);
            cleanup(foo);
            return badval;
          }

          do_something
          cleanup(baz);
          cleanup(bar);
          cleanup(foo);
          return goodval;

or even:

          if ((foo = blah()) != fail) {
            if ((bar = blarg()) != fail) {
              if ((baz = blarf()) != fail) {
                if ((qux = barg()) != fail) {
                  do_something
                  cleanup(baz);
                  cleanup(bar);
                  cleanup(foo);
                  cleanup(qux);
                  return goodval;
                }
                cleanup(baz);
              }
              cleanup(bar);
            }
            cleanup(foo);
          }
          return badval;

(The last example I hit in some real-life code.)

>Actually, variable such as the various bufs is an acceptable use for
>globals if you ask me.  The globals I tend to frown upon are ones that
>actually store values that need to be kept.  It is too easy to overwrite
>them accidentally. Especially if more than one coder is involved.

True, but how long do you need to keep the string buffers?  Until the end
of the function? Until the next function call? Until send_to_char?  It is
somewhat bounded at least.

--
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