If you screw two pieces of material together, you have the opportunity, later on, to unscrew them ... resuse them differently, install a waterproof membrane between them, and so on. But if you glue them together, later modification will be much harder or perhaps impossible. And in a similar way, if you're connecting two pieced of software together it's better to screw them than to glue them. What am I getting at?
If you access a member variable within an object directly from your main calling code, then you don't leave yourself the opportunity to intersperse some extra check or twist later on, whereas if you access all member variables via methods - even if they are really just empty wrappers, you leave yourself the flexibility for later. Let's see an example.
Here is a (Python) class:
class pidgeon:
def __init__(self, name, value):
self.name=name
self.value=value
def getvalue(self):
return self.value
which I can use like this:
george = pidgeon("John Smith",200)
gina = pidgeon("Jenny Smith",50)
print "Value of George is",george.getvalue();
print "Value of Gina is",gina.getvalue();
and it will print out 200 and 50. Here is an alternative class:
class woodpecker:
def __init__(self, name, value):
self.name=name
self.value=value
and I can access the data, more simply, as follows:
print "Value of Gerry is",gerry.value;
print "Value of Graeme is",graeme.value;
I now want to set a mimimum value of 75 pounds on all birds - pidgeons and woodpeckers. With pidgeons, it's easy - I just modify the getvalue method as follows:
def getvalue(self):
if self.value < 75: return 75
return self.value
but with my woodpecker class I'm stuck.
Pidgeons are like screws, and woodpeckers stick like glue!
Full Source code of example.
(written 2007-09-12)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y106 - Object Oriented Python [477] Class, static and unbound variables - (2005-10-25)
[834] Python makes University Challenge - (2006-08-15)
[900] Python - function v method - (2006-10-20)
[1306] Python class rattling around - (2007-08-16)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2017] Python - a truly dynamic language - (2009-01-30)
[2169] When should I use OO techniques? - (2009-05-11)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
[3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
[3878] From Structured to Object Oriented Programming. - (2012-10-02)
[3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
[4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
[4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
[4448] What is the difference between a function and a method? - (2015-03-04)
[4591] From single block to structure and object oriented programming - (2015-12-02)
[4650] Why populate object with values as you construct them? - (2016-02-18)
[4721] When to check an object type - Python isinstance example - (2016-11-03)
Q909 - Object Orientation and General technical topics - Object Orientation: Composite Objects [592] NOT Gone phishing - (2006-02-05)
[1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
[2170] Designing a heirarcy of classes - getting inheritance right - (2009-05-11)
[2641] Object Oriented Programming in PHP - (2010-02-19)
[2865] Relationships between Java classes - inheritance, packaging and others - (2010-07-10)
[2922] Getting the OO design write - with PHP a example - (2010-08-14)
[3142] Private and Public - and things between - (2011-01-22)
[3152] Jargon busting - (2011-01-30)
[3251] C++ - objects that are based on other objects, saving coding and adding robustness - (2011-04-17)
[3609] How do classes relate to each other? Associated Classes - (2012-02-12)
[3979] Extended and Associated objects - what is the difference - C++ example - (2013-01-18)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
[4394] Philosophy behind object design - and how I applied in to a Java example - (2015-01-14)
[4450] Deciding whether to use parameters, conditional statements or subclasses - (2015-03-05)
Some other Articles
Actionscript / Flash / Flex (IAQ/FAQ)Compressing web pages sent out from server. Is it worth it?FSB (Federation of Small Businesses) Western RegionCash is not an acceptable way of payingScrew it or Glue it? Access to Object variables - a warningWireless Internet Access at hotels - an update and some pitfallsEvening, Devizes and Sells GreenCatching up on indexing our resourcesMelksham Laundrette