Re: Apple Sample directly accesses ivar. Heresy?
Re: Apple Sample directly accesses ivar. Heresy?
- Subject: Re: Apple Sample directly accesses ivar. Heresy?
- From: Scott Thompson <email@hidden>
- Date: Tue, 21 Feb 2006 08:52:54 -0600
On Feb 18, 2006, at 9:12 AM, Jerry Krinock wrote:
Over the past few months I have been getting into the habit of
always using
accessors to access instance variables (ivars) within my classes. A
minimalist by religion, I feel this is overly pedantic and
"inefficient",
however I have forced myself to do it, because people smarter than
me say
that I should, "the message overhead is small", and indeed this
discipline
has reduced the number of memory management errors I make.
But yesterday I was looking at some Apple sample code and saw that
somebody
at Apple broke the rule in subclassing NSCell:
snip
There is no explanation of this direct access in comments or ReadMe.
Is this a bad example, or did the author intentionally break the
rule in
order to get a little faster drawing performance, since NSCell
-drawWithFrame:inView: is going to get invoked many times in drawing a
table?
The advantage of accessors is that they allow your code to not care
about where the data comes from. For example, if you have a class
that "knows about" a list of words, if you store that list of words
in an array, and access it directly, it's really hard to change the
way that the list is stored in the future.
If you use an accessor then the list of words could be returned from
an array, taken from a file on disk, or generated by a trained
squirrel with a keyboard... so far as anyone else is concerned, when
they call the accessor they only need to know that they can ask for
the list as an array... how that array is generated is private to the
class that is returning it. This kind of encapsulation can be very
helpful.
Another advantage of accessors is that they can be overridden. You
could define an abstract superclass of your word list class which
defines the accessor and then you could have several subclasses each
of which generates the list in a different way (DictionaryWordList,
RandomWordList, etc...) The accessor offers you polymorphism
opportunities that don't exist if you access instance variables
directly.
Having said that, if there is no chance you are ever going to take
advantage of these features then using accessors can be meaningless
overhead. In the case you point out, it doesn't really seem that the
image would be generated dynamically, or that you would subclass the
NSCell so there isn't a really compelling reason to add an accessor
for the image.
Another important thing to note about your example is that it's
sample code. Sample code doesn't always follow the best programming
practices anyway so caveat implementor.
Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden