<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Dec 21, 2013 at 1:01 PM, Chris Muller <span dir="ltr">&lt;<a href="mailto:ma.chris.m@gmail.com" target="_blank">ma.chris.m@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">&gt; 1) each class that declares instance variables has exactly one<br>
&gt; initialization method<br>
<br>
</div>Well then you can&#39;t have more than one constructor type then.  Not all<br>
objects can follow a path down to a single constructor on the<br>
class-side that ends up calling #initializeWtih:... </blockquote><div><br></div><div>Why not? I&#39;ve never found this to be an issue. </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In several cases,<br>
you&#39;ll need #initializeWithThis: and #initializeWithThat:.  And, in<br>
both cases, you want to call #initialize from the top of them.  That&#39;s<br>
repeating yourself and unconventional.</blockquote><div><br></div><div>Right. That&#39;s why the rule is one initializer. </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">
&gt; 2) each initialization method must leave the instance in a valid state<br>
<br>
</div>Of course, as does any initialization method like CPM methods.</blockquote><div><br></div><div>Not so. If your class has ivars &#39;foo&#39; and &#39;bar&#39; and it needs both of them to have values for the object to be valid, then neither #setFoo: nor #setBar: leave the object in a valid state. You have to call both of them to get a fully-initialized object. But #initializeWithFoo:bar: leaves the object in a valid state. </div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
&gt; 3) each initialization method is named verbosely starting with<br>
&gt; &#39;initializeWith&#39; and describes its parameters<br>
<br>
</div>The ONLY reason you call it &quot;initializeWithThis:that:other:thing:&quot;<br>
instead of &quot;setThis:that:other:thing:&quot; is because you want to repeat<br>
yourself by calling #initalize at the top of each method.<br></blockquote><div><br></div><div>The reason to call it #initializeWithThis:that: is that it initializes the object with this and that. If you like consider this an instance of &quot;intention-revealing selector&quot;.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Also, your #1 said &quot;exactly-one initialization method,&quot; but here you<br>
say, &quot;each initialization method.&quot;</blockquote><div><br></div><div><span style="color:rgb(80,0,80)">To quote in full, &quot;each class that declares instance variables has exactly one</span><span style="color:rgb(80,0,80)"> initialization method&quot; so if you have more than one class, you&#39;ll have more than one initializer. </span><br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
&gt; 4) each initialization method must call the initialization method of its<br>
&gt; super class<br>
<br>
</div>So who calls &quot;super initialize&quot; then?<br></blockquote><div> </div><div>Whichever class introduces the first instance variables. Typically this class inherits from Object, but that isn&#39;t necessary.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">&gt; 6) there is one or more class-side constructors which send #basicNew and the<br></div><div class="im">
&gt; initialization message<br>
<br>
</div>Why basicNew instead of new?  Honestly, there&#39;s no good reason for<br>
that and it forces you to repeat yourself in all of your<br>
#initializeWith: methods.</blockquote><div><br></div><div>Because #new calls #initialize. When following this pattern, #initialize still gets called, but by a subclass initializer, not by the constructor. We want to avoid calling #initialize twice.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><span style="color:rgb(34,34,34)">You and Frank both need to read the CPM pattern because you missed it.</span><br>
</div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 You&#39;re confusing CPM as a setter.</blockquote><div><br></div><div>You&#39;re saying we should name methods #setFoo: to make it clear that they&#39;re not setters?  Gimme a break. They&#39;re setters. <br></div><div>
<br></div><div>FWIW, I used CPM before Julian introduced me to this pattern. I think it&#39;s better than CPM, and it pushes my designs in beneficial directions. </div><div><br></div><div>Colin</div></div></div></div>