Re: Code Feedback Wanted (Generating more garbage)

From: David Sletten (david_at_slytobias.com)
Date: 03/24/04


Date: Wed, 24 Mar 2004 12:13:17 GMT

David Steuber wrote:

> ;;;; Just for the record, I hate global variables because they
> ;;;; break modularity by being non-local.
>
> ;;; global variables related to the fractional bits in our fixed radix numbers
> (defvar *fractional-bits* 16
> "Number of fractional bits in our number")
> (defvar *escape-value* (ash 4 *fractional-bits*)
> "Numbers larger than this have escaped and are not part of the Mandelbrot Set")
> (defvar *my-two* (ash 2 *fractional-bits*))
>
> (defun set-fractional-bits (fb)
> "A nice way to set both *fractional-bits* and *escape-value* at the same time"
> (setf *escape-value* (ash 4 fb)
> *my-two* (ash 2 fb)
> *fractional-bits* fb))
>

I'll let someone with more guts tackle your program in detail, but
here's one suggestion. To avoid 'global' variables, you can create a
closure with local variables and only those functions which need access
to them. For instance:
(let* ((fractional-bits 16)
        (escape-value (ash 4 fractional-bits))
        (my-two (ash 2 fractional-bits)))
   (defun initialize (fb)
     (setf escape-value (ash 4 fb) my-two (ash 2 fb) fractional-bits fb))
...)

<Rest of program out here--can't access FRACTIONAL-BITS, etc...>

This gives you the convenience of not having to pass the 'globals' as
args to each function, yet still provides some encapsulation.

You can't change these variables directly from the top-level
interactively, but you do have a setter function--INITIALIZE (or
SET-FRACTIONAL-BITS, whatever you call it).

David Sletten



Relevant Pages

  • Re: A question (confusion) about closure
    ... If you could, put out a snippet of code of closure, in emacs lisp. ... To implement closures you just cannot use global variables. ... Saying that closures are done with globals, ...
    (comp.lang.lisp)
  • Where Should Variables be Declared?
    ... Scope of variables should be determined by their role. ... That would certainly explain the result of the closure example being ... identifier is "mungeable", ... Putting all identifiers as globals won't help performance. ...
    (comp.lang.javascript)
  • Re: Allocation of a multidimensional array of structures
    ... so I've tried debugging my program ... Or to turn the question around, why should I use that prototyping ... just good practice and helps avoid undefined behavior. ... Well, while I'm aware that it's better to avoid using globals, it's ...
    (comp.lang.c)
  • Re: Allocation of a multidimensional array of structures
    ... multidimensional array allocation code: ... In case you're refering to writing void lut_alloc; ... Well, while I'm aware that it's better to avoid using globals, it's ...
    (comp.lang.c)
  • Re: Pass locatioin to Secondary Form
    ... that I am going to avoid the globals and already have recoded a couple ... passing some limited variables to another form. ... would be passing the location of a second form. ...
    (microsoft.public.dotnet.languages.vb)