<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"><<a href="mailto:ma.chris.m@gmail.com" target="_blank">ma.chris.m@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">> 1) each class that declares instance variables has exactly one<br>
> initialization method<br>
<br>
</div>Well then you can'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'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'll need #initializeWithThis: and #initializeWithThat:. And, in<br>
both cases, you want to call #initialize from the top of them. That's<br>
repeating yourself and unconventional.</blockquote><div><br></div><div>Right. That'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">
> 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 'foo' and 'bar' 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">
> 3) each initialization method is named verbosely starting with<br>
> 'initializeWith' and describes its parameters<br>
<br>
</div>The ONLY reason you call it "initializeWithThis:that:other:thing:"<br>
instead of "setThis:that:other:thing:" 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 "intention-revealing selector".</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 "exactly-one initialization method," but here you<br>
say, "each initialization method."</blockquote><div><br></div><div><span style="color:rgb(80,0,80)">To quote in full, "each class that declares instance variables has exactly one</span><span style="color:rgb(80,0,80)"> initialization method" so if you have more than one class, you'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">
> 4) each initialization method must call the initialization method of its<br>
> super class<br>
<br>
</div>So who calls "super initialize" then?<br></blockquote><div> </div><div>Whichever class introduces the first instance variables. Typically this class inherits from Object, but that isn'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">> 6) there is one or more class-side constructors which send #basicNew and the<br></div><div class="im">
> initialization message<br>
<br>
</div>Why basicNew instead of new? Honestly, there'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're confusing CPM as a setter.</blockquote><div><br></div><div>You're saying we should name methods #setFoo: to make it clear that they're not setters? Gimme a break. They're setters. <br></div><div>
<br></div><div>FWIW, I used CPM before Julian introduced me to this pattern. I think it's better than CPM, and it pushes my designs in beneficial directions. </div><div><br></div><div>Colin</div></div></div></div>