[Tutor] Getting rid of global variables

Paul Sidorsky paulsid@shaw.ca
Wed, 08 May 2002 03:21:14 -0600


Scot Stevenson wrote:

> I have the feeling I should be doing something with objects here - use a
> "mailbox" and/or "message" object that gets passed around. I don't see how
> this would solve the problem, though: When I am waiting for the user to
> make up his mind, who takes care of those objects if they are not a global
> variable?
> 
> The other idea I've had is to make the whole program an object, using the
> constructor to set up the graphics part, but I'm not sure I can really get
> my mind around that. And it somehow feels like cheating =8).
> 
> I think I'm missing some basic concept here and would be grateful if
> somebody could point out which ones of those trees make up the forest...

In C++ I've found that it often ends up that the most straightforward
way to do things is to "cheat" with a global variable or two.  Otherwise
I'd end up with a bunch of "manager" classes that make the program much
harder to understand than it would have been had I just made everything
global to begin with.

To me the the situation you're describing sounds similar.  I hope my
tiredness isn't causing me to misunderstand the problem, but the "who
takes care of..." question suggests to me a "manager" class might be
called for.  

The big problem with manager classes is that to use them you end up
jumping through many hoops in order to do a very simple task.  It can
lead to a kind of "OO-spaghetti code" that makes your program really
hard to understand.

Part of this is because the manager class ends of duplicating the
functionality of the class.  For example, a situation I've often faced
in the past in working with C++ is how to handle a "master collection"
of objects that I'd ideally like to make global.  The temptation was to
make some sort of ListManager class that handled the list.  This class
would have to provide methods for add, delete, find, etc.  But why
bother when lists have this built in?  

So the next thought is to just put the list inside a class and make it a
public variable.  This is simpler, but BZZZT - it's cheating!  Making
variables public is a violation of good OO-style.  So at this point I
had to choose which OO convention to violate.  I chose to make the lists
global, my reasoning being that making it global left the cleanest code.

The nice thing is that Python programmers tend to have a more liberal
attitude towards globals, so using them is not really considered
flagrant cheating like it would be in C++.  :-)

Anyhow, try to keep these tips in mind:

- DO keep the logic and graphics as separate as possible.

- DO get rid of as many global variables as you can because the book is
right:  globals are something that should be avoided whenever possible.

- DON'T read a book like Code Complete and feel you have to
revolutionize your way of doing things overnight.  It would be foolish
for a craftsperson to buy a tool kit and then try to learn to use them
all on a current project, so why try to do that when coding?  Apply what
you can understand now - this will still make your code that much
better.  Later on you might find a better way to do things, and if the
situation calls for it you can upgrade the program.  If not, at least
you saved yourself a bunch of frustration.

Having said all of this, it's probably the case that there's an entirely
suitable way to solve your problem that both of us are just missing.  If
so I look forward to hearing it because it means I'll get to learn
something too!

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/