****SPAM(11.2)**** [Tutor] Larger program organization

Brian van den Broek bvande at po-box.mcgill.ca
Wed Feb 16 23:04:25 CET 2005

Terry Carroll said unto the world upon 2005-02-16 16:18:
> On Fri, 11 Feb 2005, Bob Gailer wrote:
>>Whenever you find yourself writing an if statement ask whether this
>>would be better handled by subclasses. Whenever you find yourself about
>>to write a global statement, consider making the variables properties of
>>a class.
> Bob -- 
> Brian already asked for an explanation of your first statement, and I 
> found the ensuing discussion very instructive.
> Can you explain the second?  As an aesthetic point, I hate globals, and 
> I'd love a discussion with some examples of using class variables as a way 
> of avoiding this.
> Terry

Hi Terry and all,

I'm probably not the best person to explain this, but I've got a use 
case that might help illustrate.

The thing that finally got me to write my first Class statement was a 
procedural program where I had a hard to find bug.

I wrote a debug_report function which would print out an informative 
report about the state of all the objects I suspected were implicated 
in the bug. Then, I put debug_report calls at the suspicious places. 
But, my procedural code had function call chains 4 or 5 links deep, 
and at no level did any of the functions have access to all of the 
objects of interest.

To make it work in purely procedural, I had to either make many 
objects global or litter my functions with passing objects up and down 
as parameters that weren't needed for the function's tasks, but simply 
so debug_report could see them.

I made a class, put my functions in it, and suddenly, they all could 
`see' the objects of interest without the passing and returning of 
objects just for the sake of visibility. Some `self's sprinkled around 
did the work instead.

So, even if you don't make use of further OOP features (as I am 
learning to do on other threads), classes give you a namespace option 
between globals and locals -- `continentals' if you like ;-)

HTH, and looking forward to more expert explanations,

Brian vdB

More information about the Tutor mailing list