Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Calculation within objects - early, last minute, or cached?

When you create an object with the constructor, you may also set property [attribute] values within it ... or you may follow on with a series of method calls to set the initial values. Then when you pull back values / attributes / properties from your object you return those values, or the results of calculations on them.

Should you do the calculations at the time that you put the values into the object, or when you require to get them out?

In theory, the answer is it doesn't matter! ... the user of a class creates objects, calls values back, and lets the internals hide ("encapsulate") the storage and calculation. But in practise it CAN make a difference - in efficiency, for example, if there is significant calculation work involved in moving from the values put in to the results drawn out.

Lets look at the options

1. If you're creating a class in which you anticipate large numbers of objects being created, and with only a sparse readback of many of the more complex attributes, you'll be best advised to write the class so that the calculations are done "just in time" as results are called up

2. If, on the other hand, your class is likely to have a few objects created, but then the attributes will be called up many times over, you'll be best advised to do the calculations as the values are entered into the objects. And there's a third option ...

3. Just store the incoming attributes as they're entered, even if you expect them to be called back many times. When a result is called up for the first time, hold on to the result, and set a flag to say "we have the result already", then on subsequent requests for the same results you can short-circuit the calculation and just pass back the result.

If you're unsure about the "metrics" of your users and will have heavy computing involved, you'll be well advised to use the caching technique of option (3). Because your code is Object Oriented, you've got full control over all the methods used to access the data, so you can carefully set up your cached / stored flag to avoid problems with stale caches ... if your design is right, there will be no way of bypassing the methods that you've provided to ensure that the user's data access is robust.

We had a discussion on calculating early, or late, or caching on yesterday's Java Bootcamp Training Course and I've added a sample class that shows caching on our web site - source code [here].
(written 2010-02-26, updated 2010-03-03)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R105 - Ruby - Classes and Objects
  [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
  [2603] Ruby objects - a primer - (2010-01-29)
  [2609] Scope of variables - important to Ruby on Rails - (2010-01-31)
  [2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
  [3421] Ruby off the Rails? - (2011-09-06)
  [4009] Clear, concise examples - Ruby classes and objects. - (2013-02-17)
  [4502] Reading and parsing a JSON object in Ruby - (2015-06-01)

Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [507] Introduction to Object Oriented Programming - (2005-11-27)
  [1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17)
  [1864] Object Oriented Perl - First Steps - (2008-11-01)
  [2171] Cleaning up redundant objects - (2009-05-11)
  [2173] Basic OO principles - (2009-05-11)
  [2393] A first demonstration of OO, including polymorphism - (2009-09-04)
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [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)

P218 - Perl - More Objects
  [246] When to bless a Perl variable - (2005-03-15)
  [531] Packages in packages in Perl - (2005-12-16)
  [588] Changing @INC - where Perl loads its modules - (2006-02-02)
  [592] NOT Gone phishing - (2006-02-05)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [930] -> , >= and => in Perl - (2006-11-18)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
  [1435] Object Oriented Programming in Perl - Course - (2007-11-18)
  [1664] Example of OO in Perl - (2008-06-03)
  [1665] Factory method example - Perl - (2008-06-04)
  [1819] Calling base class constructors - (2008-10-03)
  [1949] Nuclear Physics comes to our web site - (2008-12-17)
  [2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2811] Igloos melt in the summer, but houses do not - (2010-06-15)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [3097] Making Perl class definitions more conventional and shorter - (2010-12-20)
  [3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
  [3377] What do I mean when I add things in Perl? - (2011-08-02)
  [3581] Perl - calls to methods that use => - what do they mean? - (2012-01-16)
  [3941] Building an object based on another object in Perl - (2012-12-03)
  [4096] Perl design patterns example - (2013-05-20)
  [4098] Using object orientation for non-physical objects - (2013-05-22)
  [4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)

J706 - Java - Objects and Classes
  [96] Variable Scope - (2004-10-22)
  [477] Class, static and unbound variables - (2005-10-25)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [1296] An example of Java Inheritance from scratch - (2007-08-08)
  [1500] First Class Java. First step and moving forward. - (2008-01-10)
  [1906] Long, Longer, Longest in Java - (2008-11-25)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2422] Looking inside Java classes - javap and javadoc - (2009-09-25)
  [4413] Binomial Coefficient (Pascal Triangle) objects in Java - (2015-02-03)
  [4422] Objects - from physical to virtual or abstract - Java - (2015-02-10)


Back to
Getting a phone line changed ...
Previous and next
or
Horse's mouth home
Forward to
Reading and writing cookies in Java Servlets and JSPs
Some other Articles
Python - what is going on around me?
Java - from applets to servlets. JSPs and more
Java Server Pages - putting it all together
Reading and writing cookies in Java Servlets and JSPs
Calculation within objects - early, last minute, or cached?
Getting a phone line changed ...
Length, size or capacity in Java?
Java arrays - are they true arrays or not?
Removing duplicates from a MySQL table
Compile but do not run PHP - syntax check only
4759 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, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2651_Cal ... ched-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb