<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 19 May 2013 13:12:38 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>mikeash - 2010-04-11 01:40:05</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;...dot-notation makes it harder... to realize that you're actually using an accessor.&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;This is, incidentally, one of the major reasons why I never use dot notation.</description><guid isPermaLink="true">fee190812b4dc6de18538a6f2879c27d</guid><pubDate>Sun, 11 Apr 2010 01:40:05 GMT</pubDate></item><item><title>Malte Tancred - 2010-04-10 12:09:56</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>Consider this:
&lt;br /&gt;&lt;code&gt;- (void)dealloc {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;self.worker.delegate = nil;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;self.worker = nil;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;[super dealloc];
&lt;br /&gt;}&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;If this is from a subclass of NSThread, and you're non-GC, there might not be an autorelease pool in place to handle the retained and autoreleased 'worker' object. It doesn't matter if you use the new dot-notation or the normal brackets, i.e., &lt;code&gt;[[self worker] setDelegate:nil]&lt;/code&gt;, but the dot-notation makes it harder I think, to realize that you're actually using an accessor when resetting the worker's delegate.</description><guid isPermaLink="true">991a5962d3e60d1fbbdd8d27e6a8c292</guid><pubDate>Sat, 10 Apr 2010 12:09:56 GMT</pubDate></item><item><title>Steven - 2010-03-16 05:21:22</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>Good article.  I have meen a long fan (&amp;gt;10 years) of using the accessors in the init and dealloc specifically because it tends to decrease code significantly.  Oddly, I find people trying to use the subclass example of how that might break but it is equally easy to break the "Apple recommended" way in a subclass as well.  For example, the subclass destroys an object (such as a timer), sets it to nil and the superclass goes to invalidate it but no longer has access to it.  That is the specific reason I QUIT following Apple's advice on accessors.
&lt;br /&gt;
&lt;br /&gt;In short, it is ALWAYS the subclasses responsibility to get the interaction to the superclass correct.  I have found it is MUCH easier to do this by keeping Accessors simply that.  Accessors.  Likewise, doing code like:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;- (id)initWithWhatever: (id)whatever
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;self = [self init];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[self setWhatever: whatever];
&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return self;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;whenever I can (something you simply cannot do if you access the ivars directly) to really simplify the code substantially.
&lt;br /&gt;
&lt;br /&gt;Add to this, the ease of memory management, and I see few reasons to NOT use the accessors at all times.  Yes there are exceptions, but they are minor.</description><guid isPermaLink="true">ab69afe57c808d11d5523a01e684dd1d</guid><pubDate>Tue, 16 Mar 2010 05:21:22 GMT</pubDate></item><item><title>mikeash - 2009-11-29 22:52:54</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>The vast majority of the time, when you write a statement like this:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;if(a = b)&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;It's a mistake. You actually wanted &lt;code&gt;==&lt;/code&gt;, but forgot one of them.
&lt;br /&gt;
&lt;br /&gt;Because this mistake is so common and so nasty, any reasonable compiler will catch it and warn about it. Gcc will do this if you compile with &lt;code&gt;-Wall&lt;/code&gt;.
&lt;br /&gt;
&lt;br /&gt;Of course, this is annoying when you &lt;i&gt;actually&lt;/i&gt; want to assign in the if statement. The double parentheses just tell the compiler (and any humans reading the code) that you really did mean to assign, and not to compare.</description><guid isPermaLink="true">c2e78d4a99d9401d1895275f9cc7caf9</guid><pubDate>Sun, 29 Nov 2009 22:52:54 GMT</pubDate></item><item><title>Kevin - 2009-11-29 21:59:15</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I added support for this dealloc pattern [ivar release], ivar = nil; in Accessorizer 1.3.  You can also turn accessors on or off in both init and dealloc.  So for those of you who want this stuff done automatically for you, you might check it out.
&lt;br /&gt;&lt;a href="http://www.kevincallahan.org/software/accessorizer.html"&gt;http://www.kevincallahan.org/software/accessorizer.html&lt;/a&gt;
&lt;br /&gt;</description><guid isPermaLink="true">891e0b3aa99678da1098e499f7393942</guid><pubDate>Sun, 29 Nov 2009 21:59:15 GMT</pubDate></item><item><title>Jose Vazquez - 2009-11-29 21:03:56</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I just noticed that you used a double set of parentheses around the statement:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;if ((self = [self init]))&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;But I can't figure out why. I went back to you post on initializers (&lt;a href="http://mikeash.com/?page=pyblog/the-how-and-why-of-cocoa-initializers.html"&gt;http://mikeash.com/?page=pyblog/the-how-and-why-of-cocoa-initializers.html&lt;/a&gt;) and I see you did that there too, but I see no explanation why. I also tried looking up Apple's documentation, and they seem to use a single set of parentheses. 
&lt;br /&gt;
&lt;br /&gt;Is there any reason you use double parentheses? </description><guid isPermaLink="true">000f6aa8c8e04f64de1992415aa664dd</guid><pubDate>Sun, 29 Nov 2009 21:03:56 GMT</pubDate></item><item><title>mikeash - 2009-11-29 19:25:30</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I'm amused at the prevalent "Apple knows best" attitude. Bindings, garbage collection, NSOperationQueue, and so many other things, Apple has gotten wrong and burned me in the process. I always trust my own evaluation over their recommendations.</description><guid isPermaLink="true">9914ea7f3d56e7e0bf7abb77ddc36e4c</guid><pubDate>Sun, 29 Nov 2009 19:25:30 GMT</pubDate></item><item><title>Vincent Gable - 2009-11-29 19:07:18</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>One benefit to using properties instead of ivars in &lt;code&gt;dealloc&lt;/code&gt; is that you can use the obj-c runtime to automatically discover and free them for you:
&lt;br /&gt;&lt;a href="http://vgable.com/blog/2008/12/20/automatically-freeing-every-property/"&gt;http://vgable.com/blog/2008/12/20/automatically-freeing-every-property/&lt;/a&gt;
&lt;br /&gt;That way you can't forget to &lt;code&gt;release&lt;/code&gt; something, and it's less code.
&lt;br /&gt;
&lt;br /&gt;But, I've gone to a &lt;code&gt;[ivar release], ivar = nil;&lt;/code&gt; pattern after hearing Apple's recommendation. In my experience forgot-to-release-an-ivar bugs  are relatively easy to track down and fix. But a side-effecting  property being called in dealloc can be &lt;i&gt;very&lt;/i&gt; nasty indeed!</description><guid isPermaLink="true">25173c8a5bbe3b080c18d30b8e5074ba</guid><pubDate>Sun, 29 Nov 2009 19:07:18 GMT</pubDate></item><item><title>Sean M - 2009-11-29 17:20:36</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I also used to use accessors in init/dealloc but no longer do, figuring Apple knows better than I.
&lt;br /&gt;
&lt;br /&gt;Also, with GC, there is no dealloc and rarely a need for finalize, so half the problem is solved there.  For init, the coding error in "Pros of Accessors" isn't an error at all.  Point being, using ivars directly in GC is not as error prone as with non-GC.</description><guid isPermaLink="true">a34380f5390076cd344f5bc160fb516e</guid><pubDate>Sun, 29 Nov 2009 17:20:36 GMT</pubDate></item><item><title>Louis Gerbarg - 2009-11-28 18:09:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>r/setPrimitave:valueForObject:/setPrimitiveValue:forObject:/
&lt;br /&gt;
&lt;br /&gt;[self setNeedsCaffeine]; 
&lt;br /&gt;</description><guid isPermaLink="true">61cabef7ccd0ee5998ad2987fc9365c7</guid><pubDate>Sat, 28 Nov 2009 18:09:11 GMT</pubDate></item><item><title>Louis Gerbarg - 2009-11-28 17:43:44</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I used to always use accessors in init, and I agree it is generally safe unless the class is intended to be subclassed.
&lt;br /&gt;
&lt;br /&gt;On the other hand, it royally bit me in the ass when I had a class that registered for KVOs after introspecting its property list, so that it could automatically do things with properties of subclasses. I think everyone can guess what happened there, it is basically the same problem that CoreData has internally, and they solved it with primative accessors.
&lt;br /&gt;
&lt;br /&gt;The thing to understand is that (in CoreData paralance):
&lt;br /&gt;
&lt;br /&gt;accessor == setValue:forObject:
&lt;br /&gt;no accessor == setPrimitave:valueForObject:
&lt;br /&gt;
&lt;br /&gt;Unfortunately, primative accessors are not a concept builtin at the KVC/KVO level, I really wish they were. If you are using automatic KVO then the system could just build you @dynamic primative accessors for all your KVC compliant accessors.</description><guid isPermaLink="true">e0626a6a135523d6f5668fd1b9f18a9d</guid><pubDate>Sat, 28 Nov 2009 17:43:44 GMT</pubDate></item><item><title>Jonathan Wight - 2009-11-28 08:34:34</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>@Kevin.
&lt;br /&gt;
&lt;br /&gt;So your co-worker wrote a accessor that had a bug in it - and you crashed. Calling setFoo: with nil could happen anywhere - not just in dealloc. Your co-worker shouldn't write buggy code.
&lt;br /&gt;
&lt;br /&gt;I'm in the "I used to use accessors, continued to use them after the apple warnings, then finally caved in" camp.</description><guid isPermaLink="true">c8d3722d7174d987206a83fda8b84aa9</guid><pubDate>Sat, 28 Nov 2009 08:34:34 GMT</pubDate></item><item><title>mikeash - 2009-11-28 00:43:28</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>Agreed. Which is ironic, because so many of Apple's classes do use accessors in their dealloc methods. (Of course, that could just mean that the recommendation comes from experience, and that they're unable to change this behavior because it would break compatibility.)</description><guid isPermaLink="true">a0de010b4eccd61fe5494e481e20c815</guid><pubDate>Sat, 28 Nov 2009 00:43:28 GMT</pubDate></item><item><title>Jens Ayton - 2009-11-28 00:29:14</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>The recommendation against using accessors seems to have migrated out from Apple’s framework groups, where the need to be defensive against weird subclasses is greater than just about anywhere else.</description><guid isPermaLink="true">cb9bcdcb719dfa8c9dfd15f68cb2b89b</guid><pubDate>Sat, 28 Nov 2009 00:29:14 GMT</pubDate></item><item><title>mikeash - 2009-11-28 00:01:34</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I don't quite understand that one. Did it have some extremely subtle and difficult to find bug when passed nil? Normally, it'll throw an exception or crash, and the backtrace will pass through dealloc, and the nature of the error will be obvious. This sort of thing is not interesting to me. Code which leads to obvious errors is nearly as good as code which doesn't lead to errors at all. It is only code which leads to subtle, difficult to find/fix errors which I'm wary of.</description><guid isPermaLink="true">d8b18caca83d50d6b838fc1ebbe2f43f</guid><pubDate>Sat, 28 Nov 2009 00:01:34 GMT</pubDate></item><item><title>Kevin Ballard - 2009-11-27 23:08:52</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I used to use accessors in init/dealloc, knowing I was going against the recommendation from Apple, until one day it screwed up. It turns out that one of my coworkers wrote a version of -setFoo: that did not handle being given a nil object properly. So when I called self.foo = nil; in my -dealloc method, my app crashed. It was fairly trivial to fix this problem, but it did highlight for me one of the issues with using accessors in init/dealloc, which is that even if you're well aware of the caveats with doing so, your coworkers probably aren't.</description><guid isPermaLink="true">6253e939b37cbf1d1878e90c85a797bc</guid><pubDate>Fri, 27 Nov 2009 23:08:52 GMT</pubDate></item><item><title>mikeash - 2009-11-27 20:57:00</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>Those are all very good points, and I would tend to agree. I think I had these thoughts before, but they were not on the surface of my mind.
&lt;br /&gt;
&lt;br /&gt;I think the ultimate question is this: does using accessors in init/dealloc increase or decrease coupling with the rest of your code? If it decreases it, then you should do it. If it decreases it, then you should not. And I think the answer to whether it increases or decreases coupling depends entirely on the situation at hand.</description><guid isPermaLink="true">932cac953bf1b5c624a1dd9a5768cd1f</guid><pubDate>Fri, 27 Nov 2009 20:57:00 GMT</pubDate></item><item><title>mfazekas - 2009-11-27 20:22:54</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I'm glad to see an article that isn't against using accessors in init/dealloc.
&lt;br /&gt;
&lt;br /&gt;IMHO the subclassing argument is a red herring as well. What it's saying is that you should complicate your classes, so that it's easier to subclass it.  This misses a few important points:
&lt;br /&gt;1. You very rarely subclass
&lt;br /&gt;2. If you follow this logic you shouldn't call any of the methods from init/dealloc as someone might override them...
&lt;br /&gt;3. Subclassing is the tightest coupling possible between classes. It's hard to get right, and you should consider a lot of issues when overriding methods. So using ivars directly instead of accessors will not make subclassing significantly easier, just a tiny bit.
&lt;br /&gt;
&lt;br /&gt;Also even if you subclass like this diagnosing the possible issues is not hard, and fixing it is easy as well. I think this is just a stupidly defensive programming, and the gains/losses are not in balance.
&lt;br /&gt;
&lt;br /&gt;</description><guid isPermaLink="true">37b41e5eae11323c79d47d741990590a</guid><pubDate>Fri, 27 Nov 2009 20:22:54 GMT</pubDate></item><item><title>foobaz - 2009-11-27 17:08:43</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html#comments</link><description>I use accessors in init/dealloc and haven't had any problems doing so.</description><guid isPermaLink="true">39fcdcea595d0047bf7c53e1de204cf6</guid><pubDate>Fri, 27 Nov 2009 17:08:43 GMT</pubDate></item></channel></rss>
