• 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: Accessor method styles
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessor method styles


  • Subject: Re: Accessor method styles
  • From: Sean Murphy <email@hidden>
  • Date: Tue, 29 Aug 2006 12:10:05 -0400

On Aug 29, 2006, at 10:53 AM, Andrew Merenbach wrote:

Hi, all. I've seen threads where this has been beaten to death, but I am still left with major questions.

Be sure to check out the ADC documentation on accessor methods, which briefly mentions the advantages/tradeoffs of certain styles: <http:// developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/ Articles/mmAccessorMethods.html#//apple_ref/doc/uid/TP40003539>


The most significant tradeoff among the four methods you're examining is uncovered when this question is asked: "what happens if setFoo is called with the same object that myFoo already points to?" Three out of the four perform unnecessary work in the case of an identical object. Please note that thread-safety also takes accessors a step further, in which case you'll need to use locks around instance variables.

I have seen several kinds of accessor/mutator methods:

- (void)setFoo:(AMFoo *)aFoo {
	[aFoo retain];
	[myFoo release];
	myFoo = aFoo;
}

If aFoo is the same object as myFoo, unnecessary retain/releases are performed. It is important to retain, then release, in that order.


- (void)setFoo:(AMFoo *)aFoo {
	if (aFoo != myFoo) {
		[myFoo release];
		myFoo = [aFoo  retain];
	}
}

This is the only style listed here that avoids an unnecessary retain and release. I stick with this convention.


- (void)setFoo:(AMFoo *)aFoo {
	id oldFoo = myFoo;
	myFoo = [aFoo retain];
	[oldFoo release];
}

This one is fine, although no check for identical objects is performed. If the two objects are identical, the retain and release is doing extra work again.



- (void)setFoo:(AMFoo *)aFoo { [myFoo autorelease]; myFoo = [aFoo retain]; }

The autorelease is a more expensive operation, so there is a performance overhead with this style. A problem resulting from this method will not crash until after the current event loop, meaning the error will be harder to trace back to it's origin.


When you decide on a style, consider letting Accessorizer do the leg work for you: <http://www.kevincallahan.org/software/ accessorizer.html>. It can be customized to work with just about whatever accessor philosophy you adopt.

Speaking of Accessorizer, here's some documentation on the subject that Kevin recommends:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html - mmalcolm crawford
http://www.stepwise.com/Articles/Technical/2002-06-11.01.html - mmalcolm crawford
http://www.stepwise.com/Articles/Technical/MemoryManagement.html - manu iyengar
http://www.stepwise.com/Articles/Technical/HoldMe.html - don yacktman
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/ index.html - Apple (developer Documentation)
http://homepage.mac.com/mmalc/CocoaExamples/controllers.html (cocoa bindings) - mmalcolm crawford
http://developer.apple.com/documentation/Cocoa/Conceptual/ CocoaBindings/index.html - Apple (developer documentation)
http://theobroma.treehouseideas.com/document.page/18 - scott stevenson
http://developer.apple.com/documentation/Cocoa/Conceptual/ KeyValueCoding/index.html -Apple (developer documentation)
http://developer.apple.com/documentation/Cocoa/Conceptual/ KeyValueObserving/index.html - Apple (developer documentation)



- Sean

Note: Since I started typing this email, many great explanations have already been shared. In the hope that my message can add to what has already been covered (and because I spent some time typing it up), I'll send it anyway. Sorry it it repeats anything!
_______________________________________________
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
References: 
 >Accessor method styles (From: Andrew Merenbach <email@hidden>)

  • Prev by Date: Re: Re: Accessor method styles
  • Next by Date: Re: compression of NSData... how to?
  • Previous by thread: Re: Re: Accessor method styles
  • Next by thread: runModalSession:relativeToWindow: alternative?
  • Index(es):
    • Date
    • Thread