Hi Christ,
I wonder if this is good practice to set a variable in a getter, even when detected null. That's part of initialization.
Best regards, Geert.
Lazy initialization and chained data invalidation are two good reasons.
You can have a hidden object whose value (instance, inner properties) depend on the object holding it or some state that can change over time and requires the hidden object to change.
Just as simple examples (very similar on the logical side):
- A bitmap property like in the original article can be a cached image that MUST be invalidated (destroyed) when some state changes (e.g. width and height) and recreated as soon as a method need it.
- A collection of children might require a reference to the owner object (maybe for event callbacks) but the collection itself could be needed only at a later time (or never requested at all); thus, you prefer to delay its allocation to avoid unnecessary operations (and memory consumption) until you really need it; startup speed would benefit too.
- A property could yield some very complex object whose value depend on many other properties. Performance would benefit if you back it into a caching field that is invalidated every time any of the other (dependency) properties change.
- Two properties represent the same entity in different forms (e.g.: a Type object and its full type name as String, or a String path and its decomposition into an array of Strings) and you want them to be always in sync when queried, but resolving one from the other is time consuming, so you prefer to delay resolution until really necessary (inside the property getter).
cool,
personally I find it very handy to be able to refresh a property's value and get things done when a 'getter' is referenced.
but you do you and that's cool too.
which should, intuitively anyway, prevent the debugger from accessing this property .. but the Little Elf goes right on ahead and runs the bmp_Method() anyway ...
my code is perfect until i don't find a bug...
Last Visit: 31-Dec-99 18:00 Last Update: 21-Sep-24 1:53