[Tutor] When to use classes

Alan Gauld alan.gauld at yahoo.co.uk
Sat Aug 19 03:52:31 EDT 2017


On 19/08/17 05:26, boB Stepp wrote:

> related methods needs to share the same values and a class would tidy
> this up nicely without the need for any globals or needless passing of
> the exact same values around as parameters/arguments.

Indeed, but its important to remember that class attributes
are in effect just global data confined to a smaller context,
and they share many of the same issues of global data.
Functions(methods) can (and usually do) change the data
as a side effect of being called, without it being obvious
that self.foo() is modifying say, self.bar and self.baz, internally.

The advantage of a class is that at least it constrains the
candidate culprits to other members of our class, but the underlying
problem of changes by side-effect remains, even if reduced.

Classes don't remove the problems with globals, they just
limit the damage.

>> possible, and hence make the program less complex. OO design tries to
>> encapsulate the coupling within (hopefully) a single class, but it does
>> nothing to reduce the coupling or complexity.

exactly so.

> I really enjoyed reading this brief article.  He explained things
> rather clearly.  

Kent was a long time member of this group for many years.
I think he left just about the same time Steve appeared.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list