<?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: Flaw: Constructor does Real Work</title>
	<atom:link href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Fri, 30 Jul 2010 03:59:03 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: fqqdk</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-3451</link>
		<dc:creator>fqqdk</dc:creator>
		<pubDate>Tue, 04 May 2010 17:56:48 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-3451</guid>
		<description>Hi Misko!

I&#039;d like to add to this flaw another bad pattern that I&#039;ve seen in PHP code. In PHP you can write code in the global scope and not just in function and method bodies. When you use PHPUnit, every time a file that has this kind of global code, it runs when a test includes the file or when the coverage report&#039;s collector includes it. That means that possibly global state gets modified, and even strange errors may occur. Maybe the file just wants to include another file that is not on the include_path when the tests run, because the tests run from a directory and the file assumes that it will be run from another directory. So generally you at first make sure that this kind of code in the global scope will never ever get included (and I&#039;ve seen codebases where it&#039;s really hard to filter them) and at second you should ensure that this kind of code is kept to a minimum, like for entry points of your webapp (index.php and the others) and every other logic is in method (or at least function) bodies.
Basically it&#039;s the same problem that arises when you use static initialization blocks in java (which you already bashed to death somewhere else :)).

That&#039;s my two cents on this. Keep up the good evangelizing :)</description>
		<content:encoded><![CDATA[<p>Hi Misko!</p>
<p>I&#8217;d like to add to this flaw another bad pattern that I&#8217;ve seen in PHP code. In PHP you can write code in the global scope and not just in function and method bodies. When you use PHPUnit, every time a file that has this kind of global code, it runs when a test includes the file or when the coverage report&#8217;s collector includes it. That means that possibly global state gets modified, and even strange errors may occur. Maybe the file just wants to include another file that is not on the include_path when the tests run, because the tests run from a directory and the file assumes that it will be run from another directory. So generally you at first make sure that this kind of code in the global scope will never ever get included (and I&#8217;ve seen codebases where it&#8217;s really hard to filter them) and at second you should ensure that this kind of code is kept to a minimum, like for entry points of your webapp (index.php and the others) and every other logic is in method (or at least function) bodies.<br />
Basically it&#8217;s the same problem that arises when you use static initialization blocks in java (which you already bashed to death somewhere else <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<p>That&#8217;s my two cents on this. Keep up the good evangelizing <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: miluch</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-2761</link>
		<dc:creator>miluch</dc:creator>
		<pubDate>Wed, 06 Jan 2010 08:50:29 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-2761</guid>
		<description>Hi 
Thanks for a link to your post. I have read it 3 times and I am still not convinced.
Hopefully, after reading comments of this post i can see that I am not the only one who disagrees with you.
The thing i do not understand the most is
&quot;Now there is a difference if the API is meant for my internal consumption or is part of an external API&quot;
How can you guarantee that none of your colleagues will call your &quot;internal consumption API&quot;  ? Use access level modifiers ?


I can agree with guys who proposed using some stubs/mocks  to pass to House constructor.
I would do the same in this case ( using  DI would be even better) but in my triangle scenario it is not about collaborators validation 
but validating internal consistency of object.</description>
		<content:encoded><![CDATA[<p>Hi<br />
Thanks for a link to your post. I have read it 3 times and I am still not convinced.<br />
Hopefully, after reading comments of this post i can see that I am not the only one who disagrees with you.<br />
The thing i do not understand the most is<br />
&#8220;Now there is a difference if the API is meant for my internal consumption or is part of an external API&#8221;<br />
How can you guarantee that none of your colleagues will call your &#8220;internal consumption API&#8221;  ? Use access level modifiers ?</p>
<p>I can agree with guys who proposed using some stubs/mocks  to pass to House constructor.<br />
I would do the same in this case ( using  DI would be even better) but in my triangle scenario it is not about collaborators validation<br />
but validating internal consistency of object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-2756</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Wed, 06 Jan 2010 00:32:09 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-2756</guid>
		<description>see: http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/</description>
		<content:encoded><![CDATA[<p>see: <a href="http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/" rel="nofollow">http://misko.hevery.com/2009/02/09/to-assert-or-not-to-assert/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: miluch</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-2754</link>
		<dc:creator>miluch</dc:creator>
		<pubDate>Tue, 05 Jan 2010 19:48:20 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-2754</guid>
		<description>Hi

I am wondering what&#039;s your opinion about checking invariants in constructor.
Let&#039;s say you want to create triangle object, feeding it&#039;s constructor with degrees of 3 angles.

Triangle(int angle1, int angle2, int angle3)

For triangle all interior angles total to 180 degrees. 
Since I truly believe that objects should be responsible for ensuring validity of its state, checking  if sum of user provided angles equals to 180 should be done in constructor - you would end up having some conditional logic in your constructor...

If invariant is violated i would throw IllegalArgumentException...

This approach seems to be very attractive  - you will always work with valid triangles, but it kills testability:
To test any of triangle behavior (for example checking if triangle is right-angled) you first need to instantiate it feeding it with correct angle values.
For triangle example it does not seem to be the case, but in more complex scenarios instantiating object to be tested can be quite hard - not because of its hard coded collaborators but because of its invariants.</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>I am wondering what&#8217;s your opinion about checking invariants in constructor.<br />
Let&#8217;s say you want to create triangle object, feeding it&#8217;s constructor with degrees of 3 angles.</p>
<p>Triangle(int angle1, int angle2, int angle3)</p>
<p>For triangle all interior angles total to 180 degrees.<br />
Since I truly believe that objects should be responsible for ensuring validity of its state, checking  if sum of user provided angles equals to 180 should be done in constructor &#8211; you would end up having some conditional logic in your constructor&#8230;</p>
<p>If invariant is violated i would throw IllegalArgumentException&#8230;</p>
<p>This approach seems to be very attractive  &#8211; you will always work with valid triangles, but it kills testability:<br />
To test any of triangle behavior (for example checking if triangle is right-angled) you first need to instantiate it feeding it with correct angle values.<br />
For triangle example it does not seem to be the case, but in more complex scenarios instantiating object to be tested can be quite hard &#8211; not because of its hard coded collaborators but because of its invariants.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Einführung in TDD &#38; Clean Code Talks &#124; geekcloud.org</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-2430</link>
		<dc:creator>Einführung in TDD &#38; Clean Code Talks &#124; geekcloud.org</dc:creator>
		<pubDate>Sun, 22 Nov 2009 15:07:57 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-2430</guid>
		<description>[...] Flaw #1: Constructor does Real Work [...]</description>
		<content:encoded><![CDATA[<p>[...] Flaw #1: Constructor does Real Work [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Trimming the fat around constructors &#171; Curly Braces</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-2347</link>
		<dc:creator>Trimming the fat around constructors &#171; Curly Braces</dc:creator>
		<pubDate>Mon, 16 Nov 2009 09:44:24 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-2347</guid>
		<description>[...] 1. Misko Hevery &#8211; The Testability Explorer Blog [...]</description>
		<content:encoded><![CDATA[<p>[...] 1. Misko Hevery &#8211; The Testability Explorer Blog [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giorgio Sironi</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-2036</link>
		<dc:creator>Giorgio Sironi</dc:creator>
		<pubDate>Wed, 07 Oct 2009 18:56:20 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-2036</guid>
		<description>Bill,
in my opinion you should not do work in the constructor of a client, and the possible cases are two:
- the Client needs a collaborator with the same lifecycle. So pass it as an argument of the constructor of the Client.
- the Clicnt needs to create an object at a certain point of execution, in a method. So pass in a Factory in the Client&#039;s constructor.</description>
		<content:encoded><![CDATA[<p>Bill,<br />
in my opinion you should not do work in the constructor of a client, and the possible cases are two:<br />
- the Client needs a collaborator with the same lifecycle. So pass it as an argument of the constructor of the Client.<br />
- the Clicnt needs to create an object at a certain point of execution, in a method. So pass in a Factory in the Client&#8217;s constructor.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Sanders</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-2002</link>
		<dc:creator>Bill Sanders</dc:creator>
		<pubDate>Sun, 04 Oct 2009 09:24:05 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-2002</guid>
		<description>Does any of this apply to a Client class? (I use the term &#039;Client&#039; in the way GoF does.) Following the principle of having the constructor do no real work has solved all kinds of problems in my patterns, but when I go to call the design, I have to use the constructor method in the Client to instantiate objects. In other words, the constructor in the Client does real work.

Thanks,
Bill</description>
		<content:encoded><![CDATA[<p>Does any of this apply to a Client class? (I use the term &#8216;Client&#8217; in the way GoF does.) Following the principle of having the constructor do no real work has solved all kinds of problems in my patterns, but when I go to call the design, I have to use the constructor method in the Client to instantiate objects. In other words, the constructor in the Client does real work.</p>
<p>Thanks,<br />
Bill</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wicket is complicated &#171; Lumpynose&#8217;s Weblog</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-1918</link>
		<dc:creator>Wicket is complicated &#171; Lumpynose&#8217;s Weblog</dc:creator>
		<pubDate>Tue, 22 Sep 2009 06:35:30 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-1918</guid>
		<description>[...] Sometimes in the constructor they call methods on the dao.  That just seems wrong. For why, see this article by Miško Hevery. [...]</description>
		<content:encoded><![CDATA[<p>[...] Sometimes in the constructor they call methods on the dao.  That just seems wrong. For why, see this article by Miško Hevery. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MeBigFatGuy</title>
		<link>http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/comment-page-1/#comment-1867</link>
		<dc:creator>MeBigFatGuy</dc:creator>
		<pubDate>Fri, 11 Sep 2009 21:15:29 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?page_id=321#comment-1867</guid>
		<description>Kicking off a thread in a ctor is no good either.</description>
		<content:encoded><![CDATA[<p>Kicking off a thread in a ctor is no good either.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
