Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
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
J706 - Java - Objects and Classes
  [2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
  [2422] Looking inside Java classes - javap and javadoc - (2009-09-25)
  [2169] When should I use OO techniques? - (2009-05-11)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1906] Long, Longer, Longest in Java - (2008-11-25)
  [1500] First Class Java. First step and moving forward. - (2008-01-10)
  [1296] An example of Java Inheritance from scratch - (2007-08-08)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [477] Class, static and unbound variables - (2005-10-25)
  [96] Variable Scope - (2004-10-22)

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

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

R105 - Ruby - Classes and Objects
  [4009] Clear, concise examples - Ruby classes and objects. - (2013-02-17)
  [3421] Ruby off the Rails? - (2011-09-06)
  [2609] Scope of variables - important to Ruby on Rails - (2010-01-31)
  [2603] Ruby objects - a primer - (2010-01-29)
  [2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
  [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)


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
4350 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 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., 2014: 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/2651_Cal ... ched-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat