How to efficiently use dependent properties if dependence is computational costly?

47 views (last 30 days)
I want to design a class where some properties are dependent on others. I understand the general concept of Matlab OOP for dependent properties: the property is recalculated each time it is accessed.
But if these calculations/dependencies are costly and the dependent properties are often accessed by methods within the class, what are you suggesting to do. Basically, it would suffice to recalculated the property each time its parents properties are updated.
If I do so, I get a warning that accessing another property from the set method of an independent property is not recommended. What else can I do?
Cheers Michael

Accepted Answer

Jacob Halbrooks
Jacob Halbrooks on 8 May 2014
Edited: Jacob Halbrooks on 8 May 2014
I would recommend you add a method to compute and update the value of your dependent property and then call that method from the setters for each property that updates it. This single-sources the computation of that property, caches it to avoid unnecessary re-computation, and satisfies MATLAB Code Analyzer.
Here is an example, where Area of a Rectangle depends on Width and Height and is updated when each of those is set:
classdef Rectangle < handle
Width = 10;
Height = 20;
properties(SetAccess = private)
Area = 200;
function set.Width(obj, v)
obj.Width = v;
function set.Height(obj, v)
obj.Height = v;
methods(Access = private)
function updateArea(obj)
obj.Area = obj.Width * obj.Height;
  1 Comment
Chris on 22 Apr 2017
I just want to thank you for this example. The
properties(SetAccess = private)
bit is exactly what I was looking for. I think there is a lot of confusion around Dependent properties because people tend to begin learning about classes "on the job" and have an application in mind. Dependent can mean a lot of things, and I think many approach Dependent properties as if they simply mean "Derived from some function of other properties". In fact, it means "Always computed on the fly". You could use an active variable to keep an always-accurate list of files in a directory, for example, which need not interact with any other properties. Once I started thinking about Dependent properties as "Active", and internalized the significance that set methods cannot be used in conjunction with them, it all became more clear.
Most examples utilize a private "mirror" of the critical property that is not dependent, and then have the get method associated with public-facing dependent variable reference that private mirror (e.g., this similar thread). It is kind of a mess, and gives the false impression that a Dependent property is essential for the use case where you want a variable to update every time some other properties are set. In other words, every other example I have seen has forced Dependent properties into the solution, but they are not needed.
So, if you want a property to update whenever another property is set, and prevent it being directly changed by the user, the example provided above is a great template. If, on the other hand, you want a property to never store a value, but to instead essentially store a method that will generate a value on the fly when referenced, then you should use a Dependent property. These imply are quite different use cases, both involving dependency... which comes around to my main point that "Dependent" is kind of an unfortunate bit of language here.

Sign in to comment.

More Answers (0)


Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by