<?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: Lesson from Object Bootcamp - The Null-Object Pattern</title>
	<atom:link href="http://www.fuzzylizard.com/archives/2007/02/27/869/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuzzylizard.com/archives/2007/02/27/869/</link>
	<description>My thoughts on Agile, Java and Ruby on Rails (mostly)</description>
	<pubDate>Thu, 20 Nov 2008 16:11:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Josh Graham</title>
		<link>http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11363</link>
		<dc:creator>Josh Graham</dc:creator>
		<pubDate>Mon, 05 Mar 2007 09:36:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11363</guid>
		<description>Hmmm.

Nate's example is nice for testing (stub-a-licious). Probably not nice for production code.

Fail fast and NPE if there's not a useful implementation. Defaulting to a null object seems rather dangerous and makes it harder to find subtle bugs.

Use dependency injection of a "simple" or "does nothing" or "null" implementation is really really what you're after. It should respond with "situation normal" just like a real implemenation would.

In your scenario, if there was no mail sending aparatus available, the "null" emailer probably should behave like everything was fine. Returning false would indicate something should have happened but didn't and the code needs to deal with it (Exceptions, anyone?). If you've explicitly wired in a "do nothing but be happy about it" implementation then it should return true and the code continues as everything has eventuated as desired.

Perhaps. :)</description>
		<content:encoded><![CDATA[<p>Hmmm.</p>
<p>Nate&#8217;s example is nice for testing (stub-a-licious). Probably not nice for production code.</p>
<p>Fail fast and NPE if there&#8217;s not a useful implementation. Defaulting to a null object seems rather dangerous and makes it harder to find subtle bugs.</p>
<p>Use dependency injection of a &#8220;simple&#8221; or &#8220;does nothing&#8221; or &#8220;null&#8221; implementation is really really what you&#8217;re after. It should respond with &#8220;situation normal&#8221; just like a real implemenation would.</p>
<p>In your scenario, if there was no mail sending aparatus available, the &#8220;null&#8221; emailer probably should behave like everything was fine. Returning false would indicate something should have happened but didn&#8217;t and the code needs to deal with it (Exceptions, anyone?). If you&#8217;ve explicitly wired in a &#8220;do nothing but be happy about it&#8221; implementation then it should return true and the code continues as everything has eventuated as desired.</p>
<p>Perhaps. <img src='http://www.fuzzylizard.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nate</title>
		<link>http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11156</link>
		<dc:creator>Nate</dc:creator>
		<pubDate>Tue, 27 Feb 2007 22:05:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11156</guid>
		<description>Oh, and on your example:  Java isn't quite that concise, and having a method return it rather than a constant won't ensure you have only one copy.  I think this is what you meant:

public class Email {
  public static final Email NONE = new Email() {
    public boolean sendEmail(String message) {
      return false;
    }
  };

  private String emailAddress;

  public boolean sendEmail(String message) {
    // send out email using Javamail or something
  }
}</description>
		<content:encoded><![CDATA[<p>Oh, and on your example:  Java isn&#8217;t quite that concise, and having a method return it rather than a constant won&#8217;t ensure you have only one copy.  I think this is what you meant:</p>
<p>public class Email {<br />
  public static final Email NONE = new Email() {<br />
    public boolean sendEmail(String message) {<br />
      return false;<br />
    }<br />
  };</p>
<p>  private String emailAddress;</p>
<p>  public boolean sendEmail(String message) {<br />
    // send out email using Javamail or something<br />
  }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nate</title>
		<link>http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11154</link>
		<dc:creator>Nate</dc:creator>
		<pubDate>Tue, 27 Feb 2007 21:51:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11154</guid>
		<description>I would replace the 'never' with 'never, except at your module boundary'.  Returning a NullObject to someone outside of your module is at least as bad as returning a null.  NullObjects are generally quite specialized to exactly the operations you need them to quietly ignore.  As soon as someone needs to check for a NullObject or it makes your code complex, you've lost.</description>
		<content:encoded><![CDATA[<p>I would replace the &#8216;never&#8217; with &#8216;never, except at your module boundary&#8217;.  Returning a NullObject to someone outside of your module is at least as bad as returning a null.  NullObjects are generally quite specialized to exactly the operations you need them to quietly ignore.  As soon as someone needs to check for a NullObject or it makes your code complex, you&#8217;ve lost.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Johnston</title>
		<link>http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11151</link>
		<dc:creator>Chris Johnston</dc:creator>
		<pubDate>Tue, 27 Feb 2007 21:13:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11151</guid>
		<description>Good point. And I get to learn something else. I have updated my post and removed the last few bits to reflect your comment. I agree, the point of the pattern is to ensure that you do not need to check what was returned. You can simply assume that the returned object is good and rely on the defaults to prevent any problems.</description>
		<content:encoded><![CDATA[<p>Good point. And I get to learn something else. I have updated my post and removed the last few bits to reflect your comment. I agree, the point of the pattern is to ensure that you do not need to check what was returned. You can simply assume that the returned object is good and rely on the defaults to prevent any problems.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nat</title>
		<link>http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11149</link>
		<dc:creator>Nat</dc:creator>
		<pubDate>Tue, 27 Feb 2007 20:40:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/02/27/869/#comment-11149</guid>
		<description>The whole point of the Null Object pattern is that you *never* check if the object is null, you just call a method and it does the right thing -- nothing if the object is "null" and something if the object is "real".

E.g. if you have statements like "if (returnedEmailAddress==noEmailAddress) {... " in your code you are not following the Null Object pattern (or OO design principles) fully. The branches of the if statement should be moved into the appropriate implementors of the Email interface and the explicit conditional replaced by a polymorphic call.</description>
		<content:encoded><![CDATA[<p>The whole point of the Null Object pattern is that you *never* check if the object is null, you just call a method and it does the right thing &#8212; nothing if the object is &#8220;null&#8221; and something if the object is &#8220;real&#8221;.</p>
<p>E.g. if you have statements like &#8220;if (returnedEmailAddress==noEmailAddress) {&#8230; &#8221; in your code you are not following the Null Object pattern (or OO design principles) fully. The branches of the if statement should be moved into the appropriate implementors of the Email interface and the explicit conditional replaced by a polymorphic call.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
