<?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: How to Write 3v1L, Untestable Code</title>
	<atom:link href="http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Tue, 09 Mar 2010 04:29:26 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Pablo</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-2197</link>
		<dc:creator>Pablo</dc:creator>
		<pubDate>Tue, 27 Oct 2009 02:00:43 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-2197</guid>
		<description>It&#039;s a really fun and good post. I see many of these items everyday at work, I&#039;ll send it by e-mail tomorrow!

Thanks!</description>
		<content:encoded><![CDATA[<p>It&#8217;s a really fun and good post. I see many of these items everyday at work, I&#8217;ll send it by e-mail tomorrow!</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Houen</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-1749</link>
		<dc:creator>Houen</dc:creator>
		<pubDate>Wed, 26 Aug 2009 05:15:57 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-1749</guid>
		<description>This is hilarious reading - i will be SOO 3V1L on my next project. Thats right testers - work that overtime!</description>
		<content:encoded><![CDATA[<p>This is hilarious reading &#8211; i will be SOO 3V1L on my next project. Thats right testers &#8211; work that overtime!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: notepuddle : TDD and Commerce Server 2007: Getting Started</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-611</link>
		<dc:creator>notepuddle : TDD and Commerce Server 2007: Getting Started</dc:creator>
		<pubDate>Fri, 23 Jan 2009 04:23:07 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-611</guid>
		<description>[...] Context object pain. See Misko here [...]</description>
		<content:encoded><![CDATA[<p>[...] Context object pain. See Misko here [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-531</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Mon, 05 Jan 2009 10:36:01 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-531</guid>
		<description>It&#039;s already been said by almost every comment here.... but I just have to say it again! Assertions are a VERY good thing!!! How can you possibly take issue to this? I use them all the time, especially in library and bridge code. They&#039;re EXTREMELY useful in debugging... remember that? Debugging? Fixing bugs and issues??? Also, every language that I know of that has a mechanism for assertions also has a way to disable them at compile time or at run time. Given that fact, it absolutely boggles my mind that you would take a negative stance on them. Like Josh so eloquently states, you want me to ignore bad input data in my library just so that you can test that my library will accept the bad input data you wish to generate during testing, but you can&#039;t be bothered to test for the fact that my library will throw an assertion exception? Puh-leeze! That&#039;s really the kind of nonsense that gives TDD purists a bad name.</description>
		<content:encoded><![CDATA[<p>It&#8217;s already been said by almost every comment here&#8230;. but I just have to say it again! Assertions are a VERY good thing!!! How can you possibly take issue to this? I use them all the time, especially in library and bridge code. They&#8217;re EXTREMELY useful in debugging&#8230; remember that? Debugging? Fixing bugs and issues??? Also, every language that I know of that has a mechanism for assertions also has a way to disable them at compile time or at run time. Given that fact, it absolutely boggles my mind that you would take a negative stance on them. Like Josh so eloquently states, you want me to ignore bad input data in my library just so that you can test that my library will accept the bad input data you wish to generate during testing, but you can&#8217;t be bothered to test for the fact that my library will throw an assertion exception? Puh-leeze! That&#8217;s really the kind of nonsense that gives TDD purists a bad name.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lance</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-468</link>
		<dc:creator>lance</dc:creator>
		<pubDate>Mon, 15 Dec 2008 10:18:28 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-468</guid>
		<description>I don&#039;t understand how you can in one paragraph be advising the use of the polymorphic soup, and in the next saying avoid util classes? Is your objection to giving classes names with &#039;Util&#039;. Or are you talking about those monolithic massive util classes one sometimes sees, with hundreds of not-quite-related methods?

 Say no to the OO-ist. When you nest and branch conditionals, all you need to do is read the code from top to bottom. Like a great novel, one simple linear prose of code. With the OO-overboard paradigm, it’s like a terrible choose-your-own-adventure kid’s book. You’re constantly flipping between classes and juggling patterns and so many more complex concepts. Just if-things out and you’ll be fine. 
■Utils, Utils, Utils! - Code smell? No way - code perfume! Litter about as many util and helper classes as you wish. These folks are helpful, and when you stick them off somewhere, someone else can use them too. That’s code reuse, and good for everyone, right? Be forewarned, the OO-police will say that functionality belongs in some object, as that object’s responsibility. Forget it, you’re way to pragmatic to break things down like they want. You’ve got a product to ship after all!</description>
		<content:encoded><![CDATA[<p>I don&#8217;t understand how you can in one paragraph be advising the use of the polymorphic soup, and in the next saying avoid util classes? Is your objection to giving classes names with &#8216;Util&#8217;. Or are you talking about those monolithic massive util classes one sometimes sees, with hundreds of not-quite-related methods?</p>
<p> Say no to the OO-ist. When you nest and branch conditionals, all you need to do is read the code from top to bottom. Like a great novel, one simple linear prose of code. With the OO-overboard paradigm, it’s like a terrible choose-your-own-adventure kid’s book. You’re constantly flipping between classes and juggling patterns and so many more complex concepts. Just if-things out and you’ll be fine.<br />
■Utils, Utils, Utils! &#8211; Code smell? No way &#8211; code perfume! Litter about as many util and helper classes as you wish. These folks are helpful, and when you stick them off somewhere, someone else can use them too. That’s code reuse, and good for everyone, right? Be forewarned, the OO-police will say that functionality belongs in some object, as that object’s responsibility. Forget it, you’re way to pragmatic to break things down like they want. You’ve got a product to ship after all!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Szepietowski</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-431</link>
		<dc:creator>Josh Szepietowski</dc:creator>
		<pubDate>Tue, 02 Dec 2008 05:00:32 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-431</guid>
		<description>Hello, great post but I had issues with a few items. For a little background I am a bit new to TDD/XXDD and unit testing in general and I am in the games industry so my requirements may be a little different, but I feel we are *mostly* on the same page. :)

* Use Statics - This is an attack on both statics and free functions.. but it violates Meyer&#039;s Principle of least encapsulation: if a method depends only on the public interface of an object it is less binding to have it as a static/free function than it is a member function... object.Method() is a nicer syntax than Method(object), but it attaches less baggage to Object so it is more flexible and orthogonal. Trading this for easier test harnessing is not a good trade. That said.. This is a point you stress strongly so as my experience with unit testing grows I may come to agree with you. :)

* Be Defensive. This is a flat out assault on the tenants of Design By Contract and I won&#039;t stand for it! DBC and TDD go together like a hand and a glove! If passing NULL is an invalid action (a bug, a contract violation) than YES assert the fuck out of it! This doesn&#039;t mean the method is untestable: test to make sure it asserts on invalid input (do make sure the test framework can catch the asserts for standard fails and anticipate them as a pass condition in some cases)! And then give it valid input to test it succeeds on. I really don&#039;t see what you are getting at here. Do you want people to be able to pass in erroneous data and get back a valid result? Just plain wrong IMHO.

* Use Primitives Wherever Possible - The less &#039;cookie objects&#039; you introduce, the less dependencies your code has, the more orthogonal and modular it is. Wrapping a primitive in an object for the sake of mocking is just insane to me: what are you going to mock? It generating a given value? This is my 2nd least favorite point (behind the before-mentioned assault on DBC)

* Side Effects are the Way to Go - This wins my award for the line I agree with most. Side effects are something I see all the time in production code and they ALWAYS end up generating subtle bugs, injecting tendrils of hidden coupling, and making it really unpleasant to refactor things. They can be the result of Singleton overuse, global state flags, or a million other things. They are very very easy to add yourself (often when you need to add a little extra functionality to an existing system and don&#039;t want to muck things up too much). As such they are becoming the design flaw I watch out for most of all: Every class, every method, must have a laser-focus purpose. Any side effects are unacceptable and if it means a major refactoring when a tiny feature is added this is a price I must be willing to pay, because the seemingly cheaper path of just adding a few hidden state variables is far worse in the long run.

* Create Utility Classes - I take issue with this.. see my prior point about the &#039;static methods&#039; for my argument, because it is the same here. Reduce coupling! Meyer&#039;s! Yaaaaaghh!

* Use non-virtual - in the games industry we know.. we know. We know that virtual == overhead. We don&#039;t strive for bad design and we pay the piper where it makes sense, but we don&#039;t make things virtual for the sole sake of testability. But being virtual does enable mocking, so perhaps a compromise is a TEST_VIRTUAL macro that says &#039;virtual&#039; when in unit-test land and nothing in other cases, such that unit-test mockings can override it safely? I dunno, maybe that is a bad idea.. In any case if it is a class that you are gonna wanna mock, you are gonna wanna have the class implementing an abstract base class (interface) in the first place I would assume.

* #ifdef PROD is silly. #ifdef DEBUG is fine. Tests should be running which full debug code active IMO (even if optimizations are turned on) since this helps them do the tag-team of awesomeness with ASSERTs you are using for poor-mans DBC.</description>
		<content:encoded><![CDATA[<p>Hello, great post but I had issues with a few items. For a little background I am a bit new to TDD/XXDD and unit testing in general and I am in the games industry so my requirements may be a little different, but I feel we are *mostly* on the same page. <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>* Use Statics &#8211; This is an attack on both statics and free functions.. but it violates Meyer&#8217;s Principle of least encapsulation: if a method depends only on the public interface of an object it is less binding to have it as a static/free function than it is a member function&#8230; object.Method() is a nicer syntax than Method(object), but it attaches less baggage to Object so it is more flexible and orthogonal. Trading this for easier test harnessing is not a good trade. That said.. This is a point you stress strongly so as my experience with unit testing grows I may come to agree with you. <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>* Be Defensive. This is a flat out assault on the tenants of Design By Contract and I won&#8217;t stand for it! DBC and TDD go together like a hand and a glove! If passing NULL is an invalid action (a bug, a contract violation) than YES assert the fuck out of it! This doesn&#8217;t mean the method is untestable: test to make sure it asserts on invalid input (do make sure the test framework can catch the asserts for standard fails and anticipate them as a pass condition in some cases)! And then give it valid input to test it succeeds on. I really don&#8217;t see what you are getting at here. Do you want people to be able to pass in erroneous data and get back a valid result? Just plain wrong IMHO.</p>
<p>* Use Primitives Wherever Possible &#8211; The less &#8216;cookie objects&#8217; you introduce, the less dependencies your code has, the more orthogonal and modular it is. Wrapping a primitive in an object for the sake of mocking is just insane to me: what are you going to mock? It generating a given value? This is my 2nd least favorite point (behind the before-mentioned assault on DBC)</p>
<p>* Side Effects are the Way to Go &#8211; This wins my award for the line I agree with most. Side effects are something I see all the time in production code and they ALWAYS end up generating subtle bugs, injecting tendrils of hidden coupling, and making it really unpleasant to refactor things. They can be the result of Singleton overuse, global state flags, or a million other things. They are very very easy to add yourself (often when you need to add a little extra functionality to an existing system and don&#8217;t want to muck things up too much). As such they are becoming the design flaw I watch out for most of all: Every class, every method, must have a laser-focus purpose. Any side effects are unacceptable and if it means a major refactoring when a tiny feature is added this is a price I must be willing to pay, because the seemingly cheaper path of just adding a few hidden state variables is far worse in the long run.</p>
<p>* Create Utility Classes &#8211; I take issue with this.. see my prior point about the &#8217;static methods&#8217; for my argument, because it is the same here. Reduce coupling! Meyer&#8217;s! Yaaaaaghh!</p>
<p>* Use non-virtual &#8211; in the games industry we know.. we know. We know that virtual == overhead. We don&#8217;t strive for bad design and we pay the piper where it makes sense, but we don&#8217;t make things virtual for the sole sake of testability. But being virtual does enable mocking, so perhaps a compromise is a TEST_VIRTUAL macro that says &#8216;virtual&#8217; when in unit-test land and nothing in other cases, such that unit-test mockings can override it safely? I dunno, maybe that is a bad idea.. In any case if it is a class that you are gonna wanna mock, you are gonna wanna have the class implementing an abstract base class (interface) in the first place I would assume.</p>
<p>* #ifdef PROD is silly. #ifdef DEBUG is fine. Tests should be running which full debug code active IMO (even if optimizations are turned on) since this helps them do the tag-team of awesomeness with ASSERTs you are using for poor-mans DBC.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johan Haleby</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-330</link>
		<dc:creator>Johan Haleby</dc:creator>
		<pubDate>Sun, 09 Nov 2008 08:39:07 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-330</guid>
		<description>Many of the things mentioned here such as static methods, final classes and making your own dependencies etc can actually be unit tested in Java. It&#039;s just that the most common mock frameworks fail to do so. We have developed an open source framework to deal with many of these issues called PowerMock that is just an extension to EasyMock. What&#039;s important to understand is that good design and testable design is not always the same thing.</description>
		<content:encoded><![CDATA[<p>Many of the things mentioned here such as static methods, final classes and making your own dependencies etc can actually be unit tested in Java. It&#8217;s just that the most common mock frameworks fail to do so. We have developed an open source framework to deal with many of these issues called PowerMock that is just an extension to EasyMock. What&#8217;s important to understand is that good design and testable design is not always the same thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brendan Miller</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-156</link>
		<dc:creator>Brendan Miller</dc:creator>
		<pubDate>Wed, 10 Sep 2008 23:19:31 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-156</guid>
		<description>I agree with your points excep for:

1. Your aversion to final classes. Use composition not inheritence. Inheritance breaks incapsulation in many ways.

2. Your aversion to asserting non-null. Null breaks type safety and assertions are needed to validate that the function contract (supply me with arguments of a certain type) is actually being honored. A null string reference is not a reference to a string *at all*. Null references by default are a hold over from C and assembly pointers, they are a misfeature in a strongly typed language like Java or C++ (although this can be fixed with smart pointers in C++). Many strongly typed languages, like ML, use non nullable references by default and require specification of nullability in the type for this very reason.

3. Your aversion to utility functions is the only thing I think is truly rediculous. Utility functions *increase* testibility not decrease it. Separating functions from a class that aren&#039;t involved in the maintenance of the classes invariant also increases the encapsulation of the class. To test that the class cannot be transformed into an invalid state, you only need to test methods that actually have access to that state.

Also a separate function that does not have priveledged access to your Url class can be tested for functionality without having to double check that it violated a class invariant, because it *can&#039;t* violate one.

Big classes with lots of helper methods are common in java, but this is actually very bad. Classes should only have one responsibility, e.g. to manage some resource, or maintain an on a piece of data, etc. Adding lots of utility methods into the class viol

That said, a utility function (or static method in java) should not be stateful in any way, and by that I also mean it should not use external services that aren&#039;t passed into the function as arguments. However, this it is true of *any* object that it is bad to access global state.

Utility functions aren&#039;t really an OO idiom, but they are a *good* idiom, and they are not hostile to testing. OO is great, but it isn&#039;t the only thing in our bag of tricks.</description>
		<content:encoded><![CDATA[<p>I agree with your points excep for:</p>
<p>1. Your aversion to final classes. Use composition not inheritence. Inheritance breaks incapsulation in many ways.</p>
<p>2. Your aversion to asserting non-null. Null breaks type safety and assertions are needed to validate that the function contract (supply me with arguments of a certain type) is actually being honored. A null string reference is not a reference to a string *at all*. Null references by default are a hold over from C and assembly pointers, they are a misfeature in a strongly typed language like Java or C++ (although this can be fixed with smart pointers in C++). Many strongly typed languages, like ML, use non nullable references by default and require specification of nullability in the type for this very reason.</p>
<p>3. Your aversion to utility functions is the only thing I think is truly rediculous. Utility functions *increase* testibility not decrease it. Separating functions from a class that aren&#8217;t involved in the maintenance of the classes invariant also increases the encapsulation of the class. To test that the class cannot be transformed into an invalid state, you only need to test methods that actually have access to that state.</p>
<p>Also a separate function that does not have priveledged access to your Url class can be tested for functionality without having to double check that it violated a class invariant, because it *can&#8217;t* violate one.</p>
<p>Big classes with lots of helper methods are common in java, but this is actually very bad. Classes should only have one responsibility, e.g. to manage some resource, or maintain an on a piece of data, etc. Adding lots of utility methods into the class viol</p>
<p>That said, a utility function (or static method in java) should not be stateful in any way, and by that I also mean it should not use external services that aren&#8217;t passed into the function as arguments. However, this it is true of *any* object that it is bad to access global state.</p>
<p>Utility functions aren&#8217;t really an OO idiom, but they are a *good* idiom, and they are not hostile to testing. OO is great, but it isn&#8217;t the only thing in our bag of tricks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan Lohre</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-133</link>
		<dc:creator>Jan Lohre</dc:creator>
		<pubDate>Tue, 02 Sep 2008 18:45:19 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-133</guid>
		<description>nice article, although I disagree on mainly two points:

1. Be Defensive - They’re out to Get Your Code!
I usually am defensive on arguments of my api methods and I would tend not to sacrifice this defensiveness in my production code for tests unless I find myself in a situation unable to test. 
In the end I cannot change that deliberately lateron as it would be a breakage of runtime behaviour.

2. Final Methods
Here I disagree even stronger as my api would then allow overriding/subclassing without being designed for inheritance. (For internal classes I would be in the same situation as in 1, when I cannot write a test because of it, only then I would think about removing the final keyword)

I would even go so far to consider Java having the wrong default (an unfinal keyword would be the way to go)

Cheers,
Jan</description>
		<content:encoded><![CDATA[<p>nice article, although I disagree on mainly two points:</p>
<p>1. Be Defensive &#8211; They’re out to Get Your Code!<br />
I usually am defensive on arguments of my api methods and I would tend not to sacrifice this defensiveness in my production code for tests unless I find myself in a situation unable to test.<br />
In the end I cannot change that deliberately lateron as it would be a breakage of runtime behaviour.</p>
<p>2. Final Methods<br />
Here I disagree even stronger as my api would then allow overriding/subclassing without being designed for inheritance. (For internal classes I would be in the same situation as in 1, when I cannot write a test because of it, only then I would think about removing the final keyword)</p>
<p>I would even go so far to consider Java having the wrong default (an unfinal keyword would be the way to go)</p>
<p>Cheers,<br />
Jan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabio Nascimento</title>
		<link>http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/comment-page-1/#comment-50</link>
		<dc:creator>Fabio Nascimento</dc:creator>
		<pubDate>Thu, 07 Aug 2008 19:38:09 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=130#comment-50</guid>
		<description>I have several of these errors, but I am improving...

Thank&#039;s for the article.

Fabio Nascimento</description>
		<content:encoded><![CDATA[<p>I have several of these errors, but I am improving&#8230;</p>
<p>Thank&#8217;s for the article.</p>
<p>Fabio Nascimento</p>
]]></content:encoded>
	</item>
</channel>
</rss>
