<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Accessor Methods, Part 2</title>
	<atom:link href="http://nslog.com/2003/05/17/accessor_methods_part_2/feed" rel="self" type="application/rss+xml" />
	<link>http://nslog.com/2003/05/17/accessor_methods_part_2</link>
	<description>The Weblog of Erik J. Barzeski</description>
	<lastBuildDate>Wed, 23 May 2012 23:05:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Erik J. Barzeski</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3320</link>
		<dc:creator>Erik J. Barzeski</dc:creator>
		<pubDate>Sat, 21 Jun 2003 22:43:57 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3320</guid>
		<description>I&#039;d hardly call that more condensed.</description>
		<content:encoded><![CDATA[<p>I'd hardly call that more condensed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mmalc crawford</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3319</link>
		<dc:creator>mmalc crawford</dc:creator>
		<pubDate>Sat, 21 Jun 2003 17:54:54 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3319</guid>
		<description>Scott wrote:

&quot;You should see if you can get a condensed/simplifed version of this onto CocoaDevCentral.&quot;



There&#039;s been a &quot;more condensed version&quot; at &lt;a href=&quot;http://www.stepwise.com/Articles/Technical/2002-06-11.01.html/&quot;&gt;http://www.stepwise.com/Articles/Technical/2002-06-11.01.html&lt;/a&gt; for some time...



mmalc</description>
		<content:encoded><![CDATA[<p>Scott wrote:</p>
<p>"You should see if you can get a condensed/simplifed version of this onto CocoaDevCentral."</p>
<p>There's been a "more condensed version" at <a  href="http://www.stepwise.com/Articles/Technical/2002-06-11.01.html/">http://www.stepwise.com/Articles/Technical/2002-06-11.01.html</a> for some time...</p>
<p>mmalc</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Stevenson</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3318</link>
		<dc:creator>Scott Stevenson</dc:creator>
		<pubDate>Sun, 18 May 2003 22:49:51 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3318</guid>
		<description>You should see if you can get a condensed/simplifed version of this onto CocoaDevCentral. Maybe the same basic idea without the surrounding discussion.



   - Scott</description>
		<content:encoded><![CDATA[<p>You should see if you can get a condensed/simplifed version of this onto CocoaDevCentral. Maybe the same basic idea without the surrounding discussion.</p>
<p>   - Scott</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Personal vs. Private &#124; NSLog();</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3321</link>
		<dc:creator>Personal vs. Private &#124; NSLog();</dc:creator>
		<pubDate>Sun, 18 May 2003 21:49:54 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3321</guid>
		<description>Jamie&#039;s written in a far better manner than I could an entry discussing the self-censorship one must apply to their blogging. She also links to...
</description>
		<content:encoded><![CDATA[<p>Jamie's written in a far better manner than I could an entry discussing the self-censorship one must apply to their blogging. She also links to...</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3317</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Sun, 18 May 2003 01:20:28 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3317</guid>
		<description>Even then you are better off using the accessor. If the accessor only does return foo; you can save yourself the method dispatch, but once that accessor changes to become more complicated you have to find everywhere you accessed foo and go through the accessor. If the accessor does something more complicated than return foo; then not using the accessor means you have to replicate that logic all over the place.</description>
		<content:encoded><![CDATA[<p>Even then you are better off using the accessor. If the accessor only does return foo; you can save yourself the method dispatch, but once that accessor changes to become more complicated you have to find everywhere you accessed foo and go through the accessor. If the accessor does something more complicated than return foo; then not using the accessor means you have to replicate that logic all over the place.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik J. Barzeski</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3316</link>
		<dc:creator>Erik J. Barzeski</dc:creator>
		<pubDate>Sun, 18 May 2003 00:59:54 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3316</guid>
		<description>The general rule of thumb is to use your object&#039;s accessor methods, particularly in the set methods. After all, your set methods may do some sanity checking of their own (&lt;code&gt;!= nil&lt;/code&gt; for example) that you don&#039;t want to circumvent. Besides, you&#039;d have to inline your set method anyway (or you&#039;d leak or something). Customized &quot;gets&quot; aren&#039;t terribly abundant, but they can happen, and it&#039;s better to know later down the line that everything is going through your accessors than to track down the ones that aren&#039;t and add some logic/sanity-checks to those as well.



Very rarely would I recommend accessing instance variables directly.</description>
		<content:encoded><![CDATA[<p>The general rule of thumb is to use your object's accessor methods, particularly in the set methods. After all, your set methods may do some sanity checking of their own (<code>!= nil</code> for example) that you don't want to circumvent. Besides, you'd have to inline your set method anyway (or you'd leak or something). Customized "gets" aren't terribly abundant, but they can happen, and it's better to know later down the line that everything is going through your accessors than to track down the ones that aren't and add some logic/sanity-checks to those as well.</p>
<p>Very rarely would I recommend accessing instance variables directly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil Larson</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3315</link>
		<dc:creator>Phil Larson</dc:creator>
		<pubDate>Sun, 18 May 2003 00:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3315</guid>
		<description>On the topic of accessors: should you use your class&#039; accessors when you&#039;re writing other methods in that class or is it alright to call the member variables directly?</description>
		<content:encoded><![CDATA[<p>On the topic of accessors: should you use your class' accessors when you're writing other methods in that class or is it alright to call the member variables directly?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ssp</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3314</link>
		<dc:creator>ssp</dc:creator>
		<pubDate>Sat, 17 May 2003 20:51:49 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3314</guid>
		<description>Nice one.



Reminds me of why I&#039;ve been suspicious about Cocoa/Objective-C: You have to worry about memory stuff. 



Sometimes I actually started to &lt;em&gt;like&lt;/em&gt; that as it forces me to be more clear about object ownership etc. and to not simply rely on things &#039;going away&#039; at some stage. 



On the other hand, it does add an extra layer of complexity which I find quite absurd in the &lt;code&gt;[[object retain] autorelease]&lt;/code&gt;. Not very Ã¦sthetic. If you needn&#039;t worry about memory that much you could probably focus more on whether you want to set/retrieve the object or merely its data (i.e. &lt;code&gt;copy&lt;/code&gt; it). That is an important conceptual distinction, rather than a technical one: Do I need the data here or the correct reference?



As you point out, frequently you&#039;ll want a reference to the object, such as the superview or the delegate, in other cases you&#039;ll actually want the data and you&#039;ll want a copy of it that cannot be modified by anyone else. Thus I suggest adding



- (NSString *)title

{

    return [[title copy] autorelease];

}



to your zoo of accessor methods, for the case where you want to return the value rather than the object. I read somewhere the - very plausible - recommendation to use this wherever you are not 100% sure about the future usage of your class to prevent that kind  screwing things up you illustrate in the five step example.



I think the difference between sharing the actual object or only passing on its data isn&#039;t made clear in many texts. And I find that it&#039;s another place where it can be helpful to sit down and ask yourself &quot;What do I want to set/retrieve?&quot; and be more clear about what your program is doing.



Of course as a corollary of this, you&#039;ll end up being very keen to implement &lt;code&gt;NSCopying&lt;/code&gt; for your classes, and all those other nifty things :)



What do you think about &lt;code&gt;NSNumber&lt;/code&gt;s, btw? I&#039;m always disappointed that you can&#039;t do computations with them and have to use those old-fashioned &lt;code&gt;int&lt;/code&gt;s and &lt;code&gt;double&lt;/code&gt;s for that purpose, where you&#039;d want to use NSNumbers for everything else - meaning that you&#039;ll end up converting things back and forth which is ugly.



... and of course you&#039;ll have less lines of code starting with a bracket. I don&#039;t like those. They look like you&#039;re writing your code in C. Eeeek.</description>
		<content:encoded><![CDATA[<p>Nice one.</p>
<p>Reminds me of why I've been suspicious about Cocoa/Objective-C: You have to worry about memory stuff. </p>
<p>Sometimes I actually started to <em>like</em> that as it forces me to be more clear about object ownership etc. and to not simply rely on things 'going away' at some stage. </p>
<p>On the other hand, it does add an extra layer of complexity which I find quite absurd in the <code>[[object retain] autorelease]</code>. Not very Ã¦sthetic. If you needn't worry about memory that much you could probably focus more on whether you want to set/retrieve the object or merely its data (i.e. <code>copy</code> it). That is an important conceptual distinction, rather than a technical one: Do I need the data here or the correct reference?</p>
<p>As you point out, frequently you'll want a reference to the object, such as the superview or the delegate, in other cases you'll actually want the data and you'll want a copy of it that cannot be modified by anyone else. Thus I suggest adding</p>
<p>- (NSString *)title</p>
<p>{</p>
<p>    return [[title copy] autorelease];</p>
<p>}</p>
<p>to your zoo of accessor methods, for the case where you want to return the value rather than the object. I read somewhere the - very plausible - recommendation to use this wherever you are not 100% sure about the future usage of your class to prevent that kind  screwing things up you illustrate in the five step example.</p>
<p>I think the difference between sharing the actual object or only passing on its data isn't made clear in many texts. And I find that it's another place where it can be helpful to sit down and ask yourself "What do I want to set/retrieve?" and be more clear about what your program is doing.</p>
<p>Of course as a corollary of this, you'll end up being very keen to implement <code>NSCopying</code> for your classes, and all those other nifty things <img src='http://nslog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>What do you think about <code>NSNumber</code>s, btw? I'm always disappointed that you can't do computations with them and have to use those old-fashioned <code>int</code>s and <code>double</code>s for that purpose, where you'd want to use NSNumbers for everything else - meaning that you'll end up converting things back and forth which is ugly.</p>
<p>... and of course you'll have less lines of code starting with a bracket. I don't like those. They look like you're writing your code in C. Eeeek.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Buzz Andersen</title>
		<link>http://nslog.com/2003/05/17/accessor_methods_part_2#comment-3313</link>
		<dc:creator>Buzz Andersen</dc:creator>
		<pubDate>Sat, 17 May 2003 18:48:03 +0000</pubDate>
		<guid isPermaLink="false">http://nslog.com/2003/05/17/accessor_methods_part_2/#comment-3313</guid>
		<description>Nice!  An excellent summary, and very informative--thanks!</description>
		<content:encoded><![CDATA[<p>Nice!  An excellent summary, and very informative--thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

