Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Screw it or Glue it? Access to Object variables - a warning

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 under
Q909 - Object Orientation and General technical topics - Object Orientation: Composite Objects
  [3609] How do classes relate to each other? Associated Classes - (2012-02-12)
  [3251] C++ - objects that are based on other objects, saving coding and adding robustness - (2011-04-17)
  [3152] Jargon busting - (2011-01-30)
  [3142] Private and Public - and things between - (2011-01-22)
  [2922] Getting the OO design write - with PHP a example - (2010-08-14)
  [2865] Relationships between Java classes - inheritance, packaging and others - (2010-07-10)
  [2641] Object Oriented Programming in PHP - (2010-02-19)
  [2170] Designing a heirarcy of classes - getting inheritance right - (2009-05-11)
  [1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
  [592] NOT Gone phishing - (2006-02-05)
  [477] Class, static and unbound variables - (2005-10-25)

Y106 - Object Oriented Python
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1306] Python class rattling around - (2007-08-16)
  [900] Python - function v method - (2006-10-20)
  [834] Python makes University Challenge - (2006-08-15)


Back to
Wireless Internet Access at hotels - an update and some pitfalls
Previous and next
or
Horse's mouth home
Forward to
Cash is not an acceptable way of paying
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 Region
Cash is not an acceptable way of paying
Screw it or Glue it? Access to Object variables - a warning
Wireless Internet Access at hotels - an update and some pitfalls
Evening, Devizes and Sells Green
Catching up on indexing our resources
Melksham Laundrette
3603 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2012: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1348_Scr ... rning.html • PAGE BUILT: Fri Feb 3 14:16:04 2012 • BUILD SYSTEM: wizard