Re: 3 Questions

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


On Wed, 31 Jan 2001, Daniel A. Koepke wrote:

>On Wed, 31 Jan 2001, George Greer wrote:
>
>> If you want variables in the middle of functions, then your functions
>> are too fat.
>
>Maybe.  But I can think of some places where it's desirable:
>
>  switch (cond) {
>  case val1:
>    int my_var; /* illegal */
>    . . .
>    break;
>  default:
>    . . .
>  }

See line 2684 of db.c

>or, as you said, long functions.  But C sort of mandates the use of long
>functions.  If the original function (which is too fat) has a lot of state
>and cannot be split-up reasonably, then breaking up the function into
>helpers requires rather inconvenient state-passing mechanisms.

I'd say that much state would usually be in a struct already.  It's
possible there are legitimate uses of that much state but you should be
asking yourself hard questions at that point.

>A better solution, of course, would be provided via a namespace or
>environment, providing us with "closures" (where the environment of the
>function is included with the function).  Allowing the state to be
>declared in an environment that is shared by the functions, rather than
>inside the functions.  Another way to do this, I guess, is with nested
>functions.

Portable state could be transcribed into a struct for passing around.
It's sort of like the idea of declaring all your global variables inside a
structure called 'g' so you can just do 'g.something'.  That has the
advantage of non-pollution and explicitly saying you're using a global
variable.

--
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/03/01 PST