Loading ...
Sorry, an error occurred while loading the content.

Re: [XP] Never Use Globals? (Was: Code Complete: Things were Scary Back Then)

Expand Messages
  • Ron Jeffries
    ... I ll try, though my record today isn t good. I don t think all globals are bad, just the one I m tempted to set up right now. That is: globals are, in my
    Message 1 of 77 , Apr 1, 2002
      Around Monday, April 1, 2002, 12:54:22 PM, dbrady@... wrote:

      > Okay, I keep hearing this "never ever use globals" thing and I just
      > don't get it. I completely understand the desire to reduce the use
      > of global variables, but I get confused when people talk about
      > eliminating them entirely. Would someone volunteer to shed some more
      > light on this?

      I'll try, though my record today isn't good.

      I don't think all globals are bad, just the one I'm tempted to set up
      right now. That is: globals are, in my opinion, guilty until proven

      As you rightly point out, without globals we wouldn't be able to find
      main(). On the other hand, in languages and linkers with no modules
      and module-private names, the name space tends to get polluted with
      lots of names that one would mostly like not to know about.

      If we have a global g_Something, there are a number of alternatives to

      1. Keep it. Note that there will be lots of references to it. When we
      need to change them, we'll have a certain amount of work to do.

      2. Hammer the global when it needs to change. This leads to tricky
      code that saves the old value and restores it when done with the new.
      Not clean, but it can work pretty well.

      3. Use Singleton to avoid creating a new global name: the class has to
      be public enough for us all to see it. So Something.specialInstance()
      is just as accessible but rather more flexible.

      4. Admit to ourselves something that Beck said to me long ago when I
      asked this question:

      Ron: We have methods all over that need to get to this variable.
      Should we just let it be a global, or what?

      Kent: What is the word for an object outside "self" that a method
      needs access to?

      Ron: <thinks a long time> Parameter?

      Kent: Yes.

      Ron Jeffries
      You do ill if you praise, but worse if you censure,
      what you do not understand. --Leonardo da Vinci
    • dbrady@es.com
      ... Well, it s my fault we got into this, so I guess I should fess up. :-) The system that I am implementing has several devices attached to it that the user
      Message 77 of 77 , Apr 8, 2002
        > -----Original Message-----
        > From: Laurent Bossavit [mailto:laurent@...]
        > Sent: Friday, April 05, 2002 3:45 PM
        > So I'm looking for an instance of logging having actual value, which
        > would make it fall under the purview of "clean design". If we further
        > show that in such an instance we *still* need to have the logging
        > calls all over the place, *bingo* ! We have proved that globals are
        > good.

        Well, it's my fault we got into this, so I guess I should fess up. :-)

        The system that I am implementing has several devices attached to it that the user can interact with via a GUI and a command console. When a device needs to report something, its reports go to its "device monitor panel" on the GUI as well as to a log file. Some of the devices are notoriously cranky analog devices, so cranky that "I'm broken" is considered a normal status message and not a warning or error. :-) In addition, the user can send queries to the devices, and the ability to have the data find its way back to the customer is considered a key feature.

        So there's your reason for having a system monitor.

        Unfortunately for this thread (but largely BECAUSE of it), I have eliminated global access to it.

        *ducks the hail of raining keyboards*

        The Device Monitors ask for the System Monitor from the Application object, just long enough for them to tell the System Monitor that they exist and would like the GUI to send an observer by once in a while to collect its messages. Other than that, nobody talks to the System Monitor, ever, except for its observer. All messages in the system are handled by creating a device monitor and sending messages to it, all of which can be done locally and without involving the rest of the system.

        If you wanted to get into the fiddly bits, I think the System Monitor is still technically a global, if for no other reason than the fact that you can call AfxGetApp() to get at the app object, rather than forcing the child objcets to receive an app object as a parameter. It isn't much easier to stub out the access for testing, unless you stub out an Application object and override AfxGetApp() to return that instead.


        The design DOES feel a lot cleaner now.

        Thanks, everyone,

        David Brady
        "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." -- Martin Fowler
      Your message has been successfully submitted and would be delivered to recipients shortly.