• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Instance variables: access directly vs. getter / setter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Instance variables: access directly vs. getter / setter


  • Subject: Re: Instance variables: access directly vs. getter / setter
  • From: Jens Alfke <email@hidden>
  • Date: Fri, 9 Oct 2009 21:23:12 -0700


On Oct 9, 2009, at 2:30 AM, Graham Cox wrote:

In init and dealloc, it is usual & recommended to access the ivar directly - not because self isn't defined (it is), but because at those times you are usually only concerned with setting the ivar's initial value (possibly to an object), or releasing it, without side effects.

Specifically, calling a setter in an -init method can cause trouble if a subclass has overridden that setter. The base class's -init method calls the setter, which invokes a method in the subclass, which runs ... but the subclass's own initializer hasn't run yet. So if the overridden setter depends on something that was initialized in the subclass's initializer, things break.



On Oct 9, 2009, at 2:01 AM, Matthias Arndt wrote:

2.) ... are there significant performance issues or other reasons to prefer the direct reference to (or direct assignment of) instance variables? At least the code of the setters / getters might impact the performance, but I'd sacrifice it for the sake of a consistent code.

This is kind of a religious issue. An individual Obj-C message-send isn't noticeably expensive, although it's still something like ten times the cost of a C++ virtual function call. So in most cases, using accessors instead of instance variables won't cause a problem. But what worries me is that this is the type of thing that creates tiny slowdowns in multiple places all over the code, and those add up. It's "death by a thousand cuts". To some extent this will show up in profiling as time spent in objc_msgsend; but it has other overhead that isn't centralized and won't show up in a profile. If you access a variable directly, the compiler can optimize that at the machine-code level and make the code more efficient than if there's a function-call required.


In my code I tend to use direct access to instance variables of self, except when the accessors are expected to be override-able. But other people make different choices.

—Jens_______________________________________________

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


References: 
 >Instance variables: access directly vs. getter / setter (From: Matthias Arndt <email@hidden>)
 >Re: Instance variables: access directly vs. getter / setter (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: Are these Apple or 3rd party classes?
  • Next by Date: Re: How to allocate and delegate; or how to keep GC and the static analyzer happy
  • Previous by thread: Re: Instance variables: access directly vs. getter / setter
  • Next by thread: Updating application info plist
  • Index(es):
    • Date
    • Thread