Doing some AS2 programming, and I think I have a case for using privately scoped getter/setters. The AS2 compiler crashes as it is considered improper use of get and set.
Situation: A (private) variable's definition is dependent on some other (asynchronous) event.
Technique#1: Have a channel of communication notifying for the event, and then defining the variable. I find event model code a bit obtuse in larger classes, and a bit hard to understand in some situations.
Technique#2: Every time variable is encountered in the main code, ensure it is valid. This can start out as one check, but then grow to be a heap of checks over time -i.e. a heap of unnecessary code.
Alternative: Private getter function that checks to see if the dependency has been met on an as needed basis. Their is no asynchronous communication functions, and the main code is kept clean.
The idea behind getter/setters is to allow the class to perform operations on the variable, without having to expose its "guts" to the outside word.
It's good practice to define everything you can in the constructor - but this sometimes isn't possible at that point in time, if something a given variable depends has not been defined yet.
The idea behind private getter/setters is to parcel a dependent variable definition to a getter function. This means the operations involved are not in your main code.
Saturday, April 12, 2008
Subscribe to:
Posts (Atom)