<?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"
	>
<channel>
	<title>Comments on: Lazy Initialization Smalltalk Style</title>
	<atom:link href="http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/feed/" rel="self" type="application/rss+xml" />
	<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/</link>
	<description>thoughts on Smalltalk and programming in general...</description>
	<pubDate>Thu, 22 May 2008 19:42:26 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9410</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Tue, 18 Dec 2007 06:21:55 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9410</guid>
		<description>Appreciate a good comment anytime, especially the ones that make me think!</description>
		<content:encoded><![CDATA[<p>Appreciate a good comment anytime, especially the ones that make me think!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amos</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9408</link>
		<dc:creator>Amos</dc:creator>
		<pubDate>Tue, 18 Dec 2007 05:49:07 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9408</guid>
		<description>The great thing about Smalltalk (well, one of them anyway) is that there's no clearly defined "right" and "wrong" way of doing things (at least not globally, though of course there can be set standards within a company; I'm reminded of my team leader and code-reviewer at my first Smalltalk job whom we used to call "The Code-Nazi"... I learned a heck of a lot from him!), as long as one keeps in mind that one's code may live much longer and be read by many more people than one would think while writing it. That awareness can be a great aid in writing cleaner, more elegant code, and in keeping "the bigger picture" in mind. I'm glad you didn't take my comment the wrong way - many people in, say, Java, would have - and totally agree with you about Smalltalkers caring enough to discuss pros and cons of tiny differences in coding styles.

Incidentally, I don't just read other people's blogs to make nitpicky comments about code ;-), I've actually been getting a lot out of your tutorials and explanations these past few days, trying to learn about Seaside, so keep it up, mate, great stuff!</description>
		<content:encoded><![CDATA[<p>The great thing about Smalltalk (well, one of them anyway) is that there&#8217;s no clearly defined &#8220;right&#8221; and &#8220;wrong&#8221; way of doing things (at least not globally, though of course there can be set standards within a company; I&#8217;m reminded of my team leader and code-reviewer at my first Smalltalk job whom we used to call &#8220;The Code-Nazi&#8221;&#8230; I learned a heck of a lot from him!), as long as one keeps in mind that one&#8217;s code may live much longer and be read by many more people than one would think while writing it. That awareness can be a great aid in writing cleaner, more elegant code, and in keeping &#8220;the bigger picture&#8221; in mind. I&#8217;m glad you didn&#8217;t take my comment the wrong way - many people in, say, Java, would have - and totally agree with you about Smalltalkers caring enough to discuss pros and cons of tiny differences in coding styles.</p>
<p>Incidentally, I don&#8217;t just read other people&#8217;s blogs to make nitpicky comments about code ;-), I&#8217;ve actually been getting a lot out of your tutorials and explanations these past few days, trying to learn about Seaside, so keep it up, mate, great stuff!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9406</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Tue, 18 Dec 2007 05:09:56 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9406</guid>
		<description>You make quite a few good points, enough to make me think about it.  I agree with you about initializeX having a return value being a suprise, and you've a good argument for not doing that.  Maybe defaultX is a better idea because it's clear a value is being returned and there are no suprises.  

"So if a default is common to all instances, why re-define the value for every instance and add unnecessary overhead?"

A good point, however, thinking about it a bit, I think I value the organization and symmetry better if instance side defaults are defined on the instance side and class side defaults are defined on the class side.

Thinking about it even more, I could easily have the same documentation advantage by having a two method categories for defaults on the class side, instance defaults and class defaults.

I think I'll have to go back the defaultX style, but on the class side organized via method categories.  Appreciate the comment Amos.  These are the kinds of little idioms I love so much about good Smalltalk, naming and organization are so important and Smalltalker's seem to really care enough to make the effort.</description>
		<content:encoded><![CDATA[<p>You make quite a few good points, enough to make me think about it.  I agree with you about initializeX having a return value being a suprise, and you&#8217;ve a good argument for not doing that.  Maybe defaultX is a better idea because it&#8217;s clear a value is being returned and there are no suprises.  </p>
<p>&#8220;So if a default is common to all instances, why re-define the value for every instance and add unnecessary overhead?&#8221;</p>
<p>A good point, however, thinking about it a bit, I think I value the organization and symmetry better if instance side defaults are defined on the instance side and class side defaults are defined on the class side.</p>
<p>Thinking about it even more, I could easily have the same documentation advantage by having a two method categories for defaults on the class side, instance defaults and class defaults.</p>
<p>I think I&#8217;ll have to go back the defaultX style, but on the class side organized via method categories.  Appreciate the comment Amos.  These are the kinds of little idioms I love so much about good Smalltalk, naming and organization are so important and Smalltalker&#8217;s seem to really care enough to make the effort.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amos</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9401</link>
		<dc:creator>Amos</dc:creator>
		<pubDate>Tue, 18 Dec 2007 00:48:27 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9401</guid>
		<description>Not to split hairs with you ;-), but when making available your code for others rather than just programming for yourself, you should avoid "acceptable idioms" and hidden assumptions as much as possible. If you want to do it that way, I'd recommend at least putting a comment in the initializeX method that there are senders that assume a certain return value, so that others who don't know about your assumptions will know what to watch out for when extending your code. Such things should also be written in the class comment. A more elaborate (but less refactored) version of the method I mentioned would be

commission
  commission isNil
    ifTrue: [self initializeCommission].
  ^commission

The reason behind it is the same as adding #yourself to the end of a cascade of messages sent to a collection when you want to assign the result to a variable, even if the particular message prior to #yourself for the particular collection you're using happens to return the collection. Other collections may do it differently, and somewhere down the track someone else may alter/extend your code to use a different collection.

Don't get me wrong, I've got nothing against Sebastian's pattern itself, and if the code works for you the way you had it, great - I was just pointing out that certain undocumented assumptions may lead to problems for others. Code is most often written once by one person, but read many times by many different people. ;-)

As for the defaultX methods being on the instance or the class side (note I didn't say the initializeX methods belong on the class side), of course the class is an object itself (a singleton instance), but it's also a blueprint for every instance based on it. So if a default is common to all instances, why re-define the value for every instance and add unnecessary overhead? Define it on the class side, add an accessor to return "self class defaultX" on the instance side, and away you go. (If a default was common to all classes, you'd put it on Metaclass. Unless I misunderstood what you meant about a class' default clashing with an instance's default?) If the default value is in any way dependent on an instance variable, then of course the defaultX method belongs on the instance side. Otherwise, if it's "static" (also the Java word for "class side"), I'd put it on the class every time. No big deal for small classes of which there aren't going to be many instances, but when dealing with larger classes of which there may be hundreds or thousands of instances, a larger memory footprint becomes more important in my experience. And good habits live long, bad habits die hard ;-)</description>
		<content:encoded><![CDATA[<p>Not to split hairs with you ;-), but when making available your code for others rather than just programming for yourself, you should avoid &#8220;acceptable idioms&#8221; and hidden assumptions as much as possible. If you want to do it that way, I&#8217;d recommend at least putting a comment in the initializeX method that there are senders that assume a certain return value, so that others who don&#8217;t know about your assumptions will know what to watch out for when extending your code. Such things should also be written in the class comment. A more elaborate (but less refactored) version of the method I mentioned would be</p>
<p>commission<br />
  commission isNil<br />
    ifTrue: [self initializeCommission].<br />
  ^commission</p>
<p>The reason behind it is the same as adding #yourself to the end of a cascade of messages sent to a collection when you want to assign the result to a variable, even if the particular message prior to #yourself for the particular collection you&#8217;re using happens to return the collection. Other collections may do it differently, and somewhere down the track someone else may alter/extend your code to use a different collection.</p>
<p>Don&#8217;t get me wrong, I&#8217;ve got nothing against Sebastian&#8217;s pattern itself, and if the code works for you the way you had it, great - I was just pointing out that certain undocumented assumptions may lead to problems for others. Code is most often written once by one person, but read many times by many different people. ;-)</p>
<p>As for the defaultX methods being on the instance or the class side (note I didn&#8217;t say the initializeX methods belong on the class side), of course the class is an object itself (a singleton instance), but it&#8217;s also a blueprint for every instance based on it. So if a default is common to all instances, why re-define the value for every instance and add unnecessary overhead? Define it on the class side, add an accessor to return &#8220;self class defaultX&#8221; on the instance side, and away you go. (If a default was common to all classes, you&#8217;d put it on Metaclass. Unless I misunderstood what you meant about a class&#8217; default clashing with an instance&#8217;s default?) If the default value is in any way dependent on an instance variable, then of course the defaultX method belongs on the instance side. Otherwise, if it&#8217;s &#8220;static&#8221; (also the Java word for &#8220;class side&#8221;), I&#8217;d put it on the class every time. No big deal for small classes of which there aren&#8217;t going to be many instances, but when dealing with larger classes of which there may be hundreds or thousands of instances, a larger memory footprint becomes more important in my experience. And good habits live long, bad habits die hard ;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9389</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Mon, 17 Dec 2007 06:27:32 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9389</guid>
		<description>That's a good observation about initializeX normally being a command rather than query, however, I'd consider it an acceptable idiom because like #add: returning what's been added, it's damn useful.  An initializeX method returning the initialized value is easy to remember, and much nicer an option than...

&lt;pre&gt;
commission
  ^commission ifNil:
      [self initializeCommission.
      commission ]
&lt;/pre&gt;

And I disagree about them belonging on the class side, because the class is an object in its own right, and might have its own defaultX methods.  Sebastian's initialize approach makes this even clearer by extending the initialize pattern to the field level, on the instance side, where it belongs.  Having tried Sebastian's method for a short time, I find I quite like it, and plan on continuing to use it in the future.</description>
		<content:encoded><![CDATA[<p>That&#8217;s a good observation about initializeX normally being a command rather than query, however, I&#8217;d consider it an acceptable idiom because like #add: returning what&#8217;s been added, it&#8217;s damn useful.  An initializeX method returning the initialized value is easy to remember, and much nicer an option than&#8230;</p>
<pre>
commission
  ^commission ifNil:
      [self initializeCommission.
      commission ]
</pre>
<p>And I disagree about them belonging on the class side, because the class is an object in its own right, and might have its own defaultX methods.  Sebastian&#8217;s initialize approach makes this even clearer by extending the initialize pattern to the field level, on the instance side, where it belongs.  Having tried Sebastian&#8217;s method for a short time, I find I quite like it, and plan on continuing to use it in the future.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amos</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9387</link>
		<dc:creator>Amos</dc:creator>
		<pubDate>Mon, 17 Dec 2007 01:46:10 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-9387</guid>
		<description>I feel compelled to point out the danger inherent in Sebastian's implementation: you are relying on the fact that the initializeX method returns the value that was just initialized. However, an initializeX method is typically a "do something" method, not a "get something" method; that is, you shouldn't count on it returning anything specific (let Smalltalk return self by default). What happens if you or someone else extends the initializeX method in your example later on (in that class or a subclass) to something like

initializeCommission
    commission := 15.
    self changed: #commission

because you want to set up a dependency? Your accessor should therefore look more like

&lt;pre&gt;
commission
    ^commission ifNil:[
        self initializeCommission.
        commission ]
&lt;/pre&gt;

to return commission explicitly, irrespective of what initializeCommission returns.

I find the defaultX methods a good pattern to follow, though they really belong on the class side, not the instance side since they define behaviour common to the class and not variable from instance to instance. Personally, I tend to avoid lazy initialization unless it's a rarely-used instance variable with a more-than-trivial memory impact, but that's more due to preference than to having a definite reason against it.

Great blogs by the way, Ramon, very informative =o)</description>
		<content:encoded><![CDATA[<p>I feel compelled to point out the danger inherent in Sebastian&#8217;s implementation: you are relying on the fact that the initializeX method returns the value that was just initialized. However, an initializeX method is typically a &#8220;do something&#8221; method, not a &#8220;get something&#8221; method; that is, you shouldn&#8217;t count on it returning anything specific (let Smalltalk return self by default). What happens if you or someone else extends the initializeX method in your example later on (in that class or a subclass) to something like</p>
<p>initializeCommission<br />
    commission := 15.<br />
    self changed: #commission</p>
<p>because you want to set up a dependency? Your accessor should therefore look more like</p>
<pre>
commission
    ^commission ifNil:[
        self initializeCommission.
        commission ]
</pre>
<p>to return commission explicitly, irrespective of what initializeCommission returns.</p>
<p>I find the defaultX methods a good pattern to follow, though they really belong on the class side, not the instance side since they define behaviour common to the class and not variable from instance to instance. Personally, I tend to avoid lazy initialization unless it&#8217;s a rarely-used instance variable with a more-than-trivial memory impact, but that&#8217;s more due to preference than to having a definite reason against it.</p>
<p>Great blogs by the way, Ramon, very informative =o)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-6147</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Mon, 08 Oct 2007 03:19:35 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-6147</guid>
		<description>Interesting approach, in the same scenario I'd just nil the existing layout knowing it would reload when accessed again.  

Your approach does create some symmetry between initializing for the whole class and initializeX for each instance var that I like.  It also has the attribute of being more of a command, where my #defaultX's are a request, I like this as well, it feels more OO and as you say, makes it more amenable to reuse.  

My approach requires I know the variable is lazy loaded, not a huge deal, but perhaps less encapsulated than yours.  I think I might like #initializeX better than #defaultX, I'll have to try it on for a bit and see how it feels.  What other idioms have you found and adopted?</description>
		<content:encoded><![CDATA[<p>Interesting approach, in the same scenario I&#8217;d just nil the existing layout knowing it would reload when accessed again.  </p>
<p>Your approach does create some symmetry between initializing for the whole class and initializeX for each instance var that I like.  It also has the attribute of being more of a command, where my #defaultX&#8217;s are a request, I like this as well, it feels more OO and as you say, makes it more amenable to reuse.  </p>
<p>My approach requires I know the variable is lazy loaded, not a huge deal, but perhaps less encapsulated than yours.  I think I might like #initializeX better than #defaultX, I&#8217;ll have to try it on for a bit and see how it feels.  What other idioms have you found and adopted?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sebastian</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-6090</link>
		<dc:creator>Sebastian</dc:creator>
		<pubDate>Fri, 05 Oct 2007 20:51:27 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-6090</guid>
		<description>for some Seaside components I've just used it again like this:

layout
   ^ layout ifNil:[self initializeLayout]

initializeLayout
  ^ layout := self makeLayout

several subclasses implement #makeLayout as they find proper for it's needs as we already wrote about.

  What is new here is that when you need to remake the layout at every refresh of after an updater for instance. As the layout it's customized at the #makeLayout method, the method #initializeLayout can be used as if it where a #reset and any subclass is guaranteed to have a fresh custom layout. By the way, for syntactic edulcorated rigurosity one can do at superclass:

resetLayout
  ^ self initializeLayout</description>
		<content:encoded><![CDATA[<p>for some Seaside components I&#8217;ve just used it again like this:</p>
<p>layout<br />
   ^ layout ifNil:[self initializeLayout]</p>
<p>initializeLayout<br />
  ^ layout := self makeLayout</p>
<p>several subclasses implement #makeLayout as they find proper for it&#8217;s needs as we already wrote about.</p>
<p>  What is new here is that when you need to remake the layout at every refresh of after an updater for instance. As the layout it&#8217;s customized at the #makeLayout method, the method #initializeLayout can be used as if it where a #reset and any subclass is guaranteed to have a fresh custom layout. By the way, for syntactic edulcorated rigurosity one can do at superclass:</p>
<p>resetLayout<br />
  ^ self initializeLayout</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-6068</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Fri, 05 Oct 2007 00:37:58 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-6068</guid>
		<description>That's good too, I picked up my #default technique from Lukas in Magritte, I got to really like looking for #defaultX to override.</description>
		<content:encoded><![CDATA[<p>That&#8217;s good too, I picked up my #default technique from Lukas in Magritte, I got to really like looking for #defaultX to override.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sebastian</title>
		<link>http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-6065</link>
		<dc:creator>Sebastian</dc:creator>
		<pubDate>Thu, 04 Oct 2007 18:10:58 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/lazy-initialization-smalltalk-style/#comment-6065</guid>
		<description>I like to do this to lazy initialization of instVars:

Budget&#62;&#62;commission
  ^ commission ifNil:[self initializeCommission]

Budget&#62;&#62;initializeCommission
  ^ commission := 15

 I've seen that from a colleague and adopted it because it allows to refine when you scale design. If I had a subclass of budget with different  default commission for this silly example.

I'm sure I have not such imagination to give a real good example right now, but I use it systematically and I have no complains of this practice.</description>
		<content:encoded><![CDATA[<p>I like to do this to lazy initialization of instVars:</p>
<p>Budget&gt;&gt;commission<br />
  ^ commission ifNil:[self initializeCommission]</p>
<p>Budget&gt;&gt;initializeCommission<br />
  ^ commission := 15</p>
<p> I&#8217;ve seen that from a colleague and adopted it because it allows to refine when you scale design. If I had a subclass of budget with different  default commission for this silly example.</p>
<p>I&#8217;m sure I have not such imagination to give a real good example right now, but I use it systematically and I have no complains of this practice.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
