Re: [newbie] Use of accessors versus direct access of ivars
Re: [newbie] Use of accessors versus direct access of ivars
- Subject: Re: [newbie] Use of accessors versus direct access of ivars
- From: Quincey Morris <email@hidden>
- Date: Thu, 8 Jan 2009 22:33:19 -0800
On Jan 8, 2009, at 21:27, Stuart Malin wrote:
I have a class with ivars for which there are accessors. I
understand that the importance of using a setter method (or to use
setValue:ForKey:) to set the ivar so that KVO operates. However, Is
there a compelling reason to use, in the methods of a class, a
getter method (or valueForKey:) versus directly accessing an ivar of
the class?
My official opinion: use a getter unless you have a compelling reason
to do otherwise.
But I'm a bit embarrassed to admit that my usual practice is the
opposite -- I tend to avoid the getters AND setters within the class
itself. However, in my own defense:
-- This only applies when the ivar and the property have identical
semantics. (In which case they have the same name. If the semantics of
one changes, but not the other, I can change the ivar name and then I
get compile errors for all the places I need to fix.)
-- This only applies to garbage-collection-enabled applications,
because then (in most situations) it's only the difference between
'xxx' and 'self.xxx' (whether getting or setting), and brevity favors
using the ivar. In retain/release applications, the code brevity
payoff tilts in favor of using the accessors.
FWIW
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden