<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: To Assert or Not To Assert</title>
	<atom:link href="http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=to-assert-or-not-to-assert</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Thu, 19 Jan 2012 16:42:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-8023</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Thu, 05 May 2011 15:59:38 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-8023</guid>
		<description>@ josh,

such defense programing may come from C world where NullPointerExceptions are not handled well. In your example there is very little difference, if the program fails with assertion or with NPE a few lines down. If all other things were equal, I say defensive programing is a good thing, but things are not equal, since defensive programing makes it harder to write a tests. So the real question is, what is more valuable, defensive programing, which prove very little, or tests, which prove that your code works as expected. Of corse, it is not either or, and many times, you can have both defensive programing, and tests, but when they are at adds, I think testing should win.</description>
		<content:encoded><![CDATA[<p>@ josh,</p>
<p>such defense programing may come from C world where NullPointerExceptions are not handled well. In your example there is very little difference, if the program fails with assertion or with NPE a few lines down. If all other things were equal, I say defensive programing is a good thing, but things are not equal, since defensive programing makes it harder to write a tests. So the real question is, what is more valuable, defensive programing, which prove very little, or tests, which prove that your code works as expected. Of corse, it is not either or, and many times, you can have both defensive programing, and tests, but when they are at adds, I think testing should win.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-8021</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Thu, 05 May 2011 15:55:02 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-8021</guid>
		<description>@kukudas,

it should be the responsibility of the house factory to make sure that the house is in the correct state. Also, your medium tests, should prove that it works as expected together.</description>
		<content:encoded><![CDATA[<p>@kukudas,</p>
<p>it should be the responsibility of the house factory to make sure that the house is in the correct state. Also, your medium tests, should prove that it works as expected together.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Go</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-7612</link>
		<dc:creator>Josh Go</dc:creator>
		<pubDate>Tue, 12 Apr 2011 03:40:45 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-7612</guid>
		<description>I think what makes this a great article is all the comments. There&#039;s a lot of disagreement, but also a lot of good ideas.


Here&#039;s my thoughts, and I agree with JF and Craig M above.

Defensive programming is a good thing. Putting assertions in the ctor makes the class robust, and can save you &quot;maalox&quot; moments down the road. Until the time comes to move the assertions, then I think it&#039;s safe to keep it there. A good question to keep in mind when designing is...&quot;what is the purpose of class?&quot;

If the purpose of the class is to never have NULLs, then it makes perfect sense to put the assertion in the ctor. However, if NULLs are allowable, then it&#039;s best to &quot;spread the defense&quot; by moving the assertions to the secure() function.

void secure()
{
  Assert.notNull(door);
  Assert.notNull(window);

  door.lock();
  window.close();
}

&quot;Early In, Early Out&quot;</description>
		<content:encoded><![CDATA[<p>I think what makes this a great article is all the comments. There&#8217;s a lot of disagreement, but also a lot of good ideas.</p>
<p>Here&#8217;s my thoughts, and I agree with JF and Craig M above.</p>
<p>Defensive programming is a good thing. Putting assertions in the ctor makes the class robust, and can save you &#8220;maalox&#8221; moments down the road. Until the time comes to move the assertions, then I think it&#8217;s safe to keep it there. A good question to keep in mind when designing is&#8230;&#8221;what is the purpose of class?&#8221;</p>
<p>If the purpose of the class is to never have NULLs, then it makes perfect sense to put the assertion in the ctor. However, if NULLs are allowable, then it&#8217;s best to &#8220;spread the defense&#8221; by moving the assertions to the secure() function.</p>
<p>void secure()<br />
{<br />
  Assert.notNull(door);<br />
  Assert.notNull(window);</p>
<p>  door.lock();<br />
  window.close();<br />
}</p>
<p>&#8220;Early In, Early Out&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kukudas</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-7569</link>
		<dc:creator>kukudas</dc:creator>
		<pubDate>Fri, 08 Apr 2011 12:22:14 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-7569</guid>
		<description>Hm.. i&#039;m having this problem right now. After some googeling i came here. Your article is quite good. In my opinion the House Object should not know anything about validation. You should be able to create a House without a kitchen, or even without roof. This means max. flexibility but also great danger.  As some comments stated now it is possible to create a House Object which might not be in a consistence state. But the Object which has the responsibility to create a specific House Object has to perform these Validations.</description>
		<content:encoded><![CDATA[<p>Hm.. i&#8217;m having this problem right now. After some googeling i came here. Your article is quite good. In my opinion the House Object should not know anything about validation. You should be able to create a House without a kitchen, or even without roof. This means max. flexibility but also great danger.  As some comments stated now it is possible to create a House Object which might not be in a consistence state. But the Object which has the responsibility to create a specific House Object has to perform these Validations.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piotr Tarsa</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-5683</link>
		<dc:creator>Piotr Tarsa</dc:creator>
		<pubDate>Fri, 07 Jan 2011 18:23:04 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-5683</guid>
		<description>I agree with previous commenters. Instead of: new LivingRoom(new Table(), new TV(), new Sofa()), we can write mock(LivingRoom.class). Replacing null with mock(Class) isn&#039;t a hard task.

I would also strongly avoid nulls, as they make things ugly.
http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare
Better use something like Null Object Pattern</description>
		<content:encoded><![CDATA[<p>I agree with previous commenters. Instead of: new LivingRoom(new Table(), new TV(), new Sofa()), we can write mock(LivingRoom.class). Replacing null with mock(Class) isn&#8217;t a hard task.</p>
<p>I would also strongly avoid nulls, as they make things ugly.<br />
<a href="http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare" rel="nofollow">http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare</a><br />
Better use something like Null Object Pattern</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Can you spot Java Puzzler in this snippet? at JAW Speak</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-1965</link>
		<dc:creator>Can you spot Java Puzzler in this snippet? at JAW Speak</dc:creator>
		<pubDate>Wed, 30 Sep 2009 20:00:10 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-1965</guid>
		<description>[...] and I saw this in a test with a particularly ugly constructor with 12 parameters. We were following Misko&#8217;s style of &#8220;use nulls in tests, but not in production code&#8221; and thus that Boolean [...]</description>
		<content:encoded><![CDATA[<p>[...] and I saw this in a test with a particularly ugly constructor with 12 parameters. We were following Misko&#8217;s style of &#8220;use nulls in tests, but not in production code&#8221; and thus that Boolean [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Should we enforce constructor args?! &#171; Journal of a software dev</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-1275</link>
		<dc:creator>Should we enforce constructor args?! &#171; Journal of a software dev</dc:creator>
		<pubDate>Sat, 27 Jun 2009 10:54:07 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-1275</guid>
		<description>[...] written and the information surrounding good OO and unit testing was, anyhow I came across this post which made me think about whether we should be asserting the collabarators in the constructor or [...]</description>
		<content:encoded><![CDATA[<p>[...] written and the information surrounding good OO and unit testing was, anyhow I came across this post which made me think about whether we should be asserting the collabarators in the constructor or [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giorgio Sironi</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-1145</link>
		<dc:creator>Giorgio Sironi</dc:creator>
		<pubDate>Sat, 30 May 2009 09:23:42 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-1145</guid>
		<description>You say you can write a test for the factory that will asserts that non-null values are passed to the constructor. But how can you write such a test? I know Guice will take care of, but the contract of my factories is &#039;you call create*(), I return an object&#039;. The object collaborators are private field references and I cannot access them to see if they are null. Maybe a method verifyNull() on the created object can do the work, but to me it seems ugly.</description>
		<content:encoded><![CDATA[<p>You say you can write a test for the factory that will asserts that non-null values are passed to the constructor. But how can you write such a test? I know Guice will take care of, but the contract of my factories is &#8216;you call create*(), I return an object&#8217;. The object collaborators are private field references and I cannot access them to see if they are null. Maybe a method verifyNull() on the created object can do the work, but to me it seems ugly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig McDonnell</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-739</link>
		<dc:creator>Craig McDonnell</dc:creator>
		<pubDate>Thu, 05 Mar 2009 23:04:24 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-739</guid>
		<description>From experience, defensive programming has helped me avoid long debugging sessions on many occassions. I&#039;m not sure I can agree with this post but it&#039;s certainly food for thought.
I&#039;ve been considering using the Findbugs annotations and perhaps the @NonNull annotation could be of use here.
With this annotation we can enforce the null check for client code under /src but not /test? -This could even have the added benefit of moving the constraint check from run-time to compile-time.</description>
		<content:encoded><![CDATA[<p>From experience, defensive programming has helped me avoid long debugging sessions on many occassions. I&#8217;m not sure I can agree with this post but it&#8217;s certainly food for thought.<br />
I&#8217;ve been considering using the Findbugs annotations and perhaps the @NonNull annotation could be of use here.<br />
With this annotation we can enforce the null check for client code under /src but not /test? -This could even have the added benefit of moving the constraint check from run-time to compile-time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mgira</title>
		<link>http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/comment-page-1/#comment-733</link>
		<dc:creator>mgira</dc:creator>
		<pubDate>Wed, 04 Mar 2009 10:17:42 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=374#comment-733</guid>
		<description>Hi Miško,first, let me say that i really enjoy reading your blog!Back to topic:If you want to test Objects in an &quot;inconsistent&quot; state (i.e. not completely initialized), maybe setter injection would be a more flexible approach.</description>
		<content:encoded><![CDATA[<p>Hi Miško,first, let me say that i really enjoy reading your blog!Back to topic:If you want to test Objects in an &#8220;inconsistent&#8221; state (i.e. not completely initialized), maybe setter injection would be a more flexible approach.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

