<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Miško Hevery &#187; OO</title>
	<atom:link href="http://misko.hevery.com/category/oo/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Fri, 30 Jul 2010 02:29:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Static Methods are Death to Testability</title>
		<link>http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/</link>
		<comments>http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 14:44:31 +0000</pubDate>
		<dc:creator>misko</dc:creator>
				<category><![CDATA[OO]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[Testability]]></category>

		<guid isPermaLink="false">http://misko.hevery.com/?p=352</guid>
		<description><![CDATA[by Miško Hevery
Recently many of you, after reading Guide to Testability, wrote to telling me there is nothing wrong with static methods. After all what can be easier to test than Math.abs()! And Math.abs() is static method! If abs() was on instance method, one would have to instantiate the object first, and that may prove to [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="http://misko.hevery.com/about/">Miško Hevery</a></p>
<p>Recently many of you, after reading <a href="http://misko.hevery.com/code-reviewers-guide/">Guide to Testability</a>, wrote to telling me there is nothing wrong with static methods. After all what can be easier to test than <span style="font-family: 'courier new', courier;">Math.abs()</span>! And <span style="font-family: 'courier new', courier;">Math.abs()</span> is static method! If <span style="font-family: 'courier new', courier;">abs()</span> was on instance method, one would have to instantiate the object first, and that may prove to be a problem. (See <a href="http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/">how to think about the new operator</a>, and <a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/">class does real work</a>)</p>
<p>The basic issue with static methods is they are procedural code. I have no idea how to unit-test procedural code. Unit-testing assumes that I can instantiate a piece of my application in isolation. During the instantiation I <strong>wire</strong> the dependencies with mocks/friendlies which replace the real dependencies. With procedural programing there is <strong>nothing to &#8220;wire&#8221;</strong> since there are no objects, the code and data are separate.</p>
<p>Here is another way of thinking about it. Unit-testing needs seams, seams is where we prevent the execution of normal code path and is how we achieve isolation of the class under test. seams work through polymorphism, we override/implement class/interface  and than wire the class under test differently in order to take control of the execution flow. With static methods there is nothing to override. Yes, static methods are easy to call, but if the static method calls another static method there is no way to overrider the called method dependency.</p>
<p>Lets do a mental exercise. Suppose your application has nothing but static methods. (Yes, code like that is possible to write, it is called procedural programming.) Now imagine the call graph of that application. If you try to execute a leaf method, you will have no issue setting up its state, and asserting all of the corner cases. The reason is that a leaf method makes no further calls. As you move further away from the leaves and closer to the root <span style="font-family: 'courier new', courier;">main()</span> method it will be harder and harder to set up the state in your test and harder to assert things. Many things will become impossible to assert. Your tests will get progressively larger. Once you reach the <span style="font-family: 'courier new', courier;">main()</span> method you  no longer have a unit-test (as your unit is the whole application) you now have a scenario test. Imagine that the application you are trying to test is a word processor. There is not much you can assert from the main method. </p>
<p>We have already covered that <a href="http://misko.hevery.com/2008/08/25/root-cause-of-singletons/">global state is bad</a> and how it makes your application <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/">hard to understand</a>. If your application has no global state than all of the input for your static method must come from its arguments. Chances are very good that you can move the method as an instance method to one of the method&#8217;s arguments. (As in <span style="font-family: 'courier new', courier;">method(a,b) </span>becomes <span style="font-family: 'courier new', courier;">a.method(b)</span>.) Once you move it you realized that that is where the method should have been to begin with. The use of static methods becomes even worse problem when the static methods start accessing the global state of the application. What about methods which take no arguments? Well, either methodX() returns a constant in which case there is nothing to test; it accesses global state, which is bad; or it is a factory.</p>
<p>Sometimes a static methods is a factory for other objects. This further exuberates the testing problem. In tests we rely on the fact that we can wire objects differently replacing important dependencies with mocks. Once a <span style="font-family: 'courier new', courier;">new</span> operator is called we can not override the method with a sub-class. A caller of such a static factory is permanently bound to the concrete classes which the static factory method produced. In other words the damage of the static method is far beyond the static method itself. Butting object graph wiring and construction code into static method is extra bad, since object graph wiring is how we isolate things for testing.</p>
<p>&#8220;So leaf methods are ok to be static but other methods should not be?&#8221; I like to go a step further and simply say, static methods are not OK. The issue is that a methods starts off being a leaf and over time more and more code is added to them and they lose their positions as a leafs. It is way to easy to turn a leaf method into none-leaf method, the other way around is not so easy. Therefore a static leaf method is a slippery slope which is waiting to grow and become a problem. Static methods are procedural! In OO language stick to OO. And as far as <span style="font-family: 'courier new', courier;">Math.abs(-5)</span> goes, I think Java got it wrong. I really want to write <span style="font-family: 'courier new', courier;">-5.abs()</span>. Ruby got that one right.</p>
]]></content:encoded>
			<wfw:commentRss>http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>Testability Explorer: Measuring Testability</title>
		<link>http://misko.hevery.com/2008/10/21/testability-explorer-measuring-testability/</link>
		<comments>http://misko.hevery.com/2008/10/21/testability-explorer-measuring-testability/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 15:41:20 +0000</pubDate>
		<dc:creator>misko</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Testability]]></category>
		<category><![CDATA[Testability Explorer]]></category>

		<guid isPermaLink="false">http://misko.hevery.com/?p=258</guid>
		<description><![CDATA[Testability Explorer: Using Byte-Code Analysis to Engineer Lasting Social Changes in an Organization’s Software Development Process. (Or How to Get Developers to Write Testable Code)
Presented at 2008 OOPSLA by Miško Hevery a Best Practices Coach @ Google
Abstract
Testability Explorer is an open-source tool that identifies hard-to-test Java code. Testability Explorer provides a repeatable objective metric of [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Testability Explorer: Using Byte-Code Analysis to Engineer Lasting Social Changes in an Organization’s Software Development Process. (Or How to Get Developers to Write Testable Code)</strong></p>
<p><em>Presented at 2008 OOPSLA by <a href="http://misko.hevery.com/about/">Miško Hevery</a> a Best Practices Coach @ Google</em></p>
<p><strong>Abstract</strong></p>
<p>Testability Explorer is an open-source tool that identifies hard-to-test Java code. Testability Explorer provides a repeatable objective metric of “testability.” This metric becomes a key component of engineering a social change within an organization of developers. The Testability Explorer report provides actionable information to developers which can be used as (1) measure of progress towards a goal and (2) a guide to refactoring towards a more testable code-base.<br />
<strong>Keywords:</strong> unit-testing; testability; refactoring; byte-code analysis; social engineering.</p>
<p><strong>1. Testability Explorer Overview</strong></p>
<p>In order to unit-test a class, it is important that the class can be instantiated in isolation as part of a unit-test. The most common pitfalls of testing are (1) mixing object-graph instantiation with application-logic and (2) relying on global state. The Testability Explorer can point out both of these pitfalls.</p>
<p><strong>1.1 Non-Mockable Cyclomatic Complexity</strong></p>
<p>Cyclomatic complexity is a count of all possible paths through code-base. For example: a main method will have a large cyclomatic complexity since it is a sum of all of the conditionals in the application. To limit the size of the cyclomatic complexity in a test, a common practice is to replace the collaborators of class-under-test with mocks, stubs, or other test doubles.</p>
<p>Let’s define “non-mockable cyclomatic complexity” as what is left when the class-under-test has all of its accessible collaborators replaced with mocks. A code-base where the responsibility of object-creation and application-logic is separated (using Dependency Injection) will have high degree of accessible collaborators; as a result most of its collaborators will easily be replaceable with mocks, leaving only the cyclomatic complexity of the class-under-test behind.</p>
<p>In applications, where the class-under-test is responsible for instantiating its own collaborators, these collaborators will not be accessible to the test and as a result will not be replaceable for mocks. (There is no place to inject test doubles.) In such classes the cyclomatic complexity will be the sum of the class-under-test and its non-mockable collaborators.</p>
<p>The higher the non-mockable cyclomatic complexity the harder it will be to write a unit-test. Each non-mockable conditional translates to a single unit of cost on the Testability Explorer report. The cost of static class initialization and class construction is automatically included for each method, since a class needs to be instantiated before it can be exercised in a test.</p>
<p><strong>1.2 Transitive Global-State</strong></p>
<p>Good unit-tests can be run in parallel and in any order. To achieve this, the tests need to be well isolated. This implies that only the stimulus from the test has an effect on the code execution, in other words, there is no global-state.</p>
<p>Global-state has a transitive property. If a global variable refers to a class than all of the references of that class (and all of its references) are globally accessible as well. Each globally accessible variable, that is not final, results in a cost of ten units on the Testability Explorer.</p>
<p><strong>2. Testability Explorer Report</strong></p>
<p>A chain is only as strong as its weakest link. Therefore the cost of testing a class is equal to the cost of the class’ costliest method. In the same spirit the application&#8217;s overall testability is de-fined in terms of a few un-testable classes rather than a large number of testable ones. For this reason when computing the overall score of a project the un-testable classes are weighted heavier than the testable ones.</p>
<p><a href="http://misko.hevery.com/wp-content/uploads/2008/10/testabilityexplorerreport.png"><img class="alignnone size-medium wp-image-259" title="testabilityexplorerreport" src="http://misko.hevery.com/wp-content/uploads/2008/10/testabilityexplorerreport.png" alt="" width="240" height="206" /></a></p>
<p><strong>3. How to Interpret the Report</strong></p>
<p>By default the classes are categorized into three categories: “Excellent” (green) for classes whose cost is below 50; “Good” (yellow) for classes whose cost is below 100; and “Needs work” (red) for all other classes. For convenience the data is presented as both a pie chart and histogram distribution and overall (weighted average) cost shown on a dial.</p>
<pre>[-]ClassRepository [ 323 ]
  [-]ClassInfo getClass(String) [ 323 ]
    line 51:
      ClassInfo parseClass(InputStream) [318]
      InputStream inputStreamForClass(String) [2]
  [-]ClassInfo parseClass(InputStream) [318]
    line 77: void accept(ClassVisitor, int) [302]
    line 75: ClassReader(InputStream) [15]</pre>
<p>Clicking on the class ClassRepository allows one to drill down into the classes to get more information. For example the above report shows that ClassRepository has a high cost of 318 due to the parseClass(InputStream) method. Looking in closer we see that the cost comes from line 77 and an invocation of the accept() method.</p>
<pre>73:ClassInfo parseClass(InputStream is) {
74:  try {
75:    ClassReader reader = new ClassReader(is);
76:    ClassBuilder v = new ClassBuilder (this);
77:    reader.accept(v, 0);
78:    return visitor.getClassInfo();
79:  } catch (IOException e) {
80:    throw new RuntimeException(e);
81:  }
82:}</pre>
<p>As you can see from the code the ClassReader can never be replaced for a mock and as a result the cyclomatic complexity of the accept method can not be avoided in a test &#8212; resulting in a high testability cost. (Injecting the ClassReader would solve this problem and make the class more test-able.)</p>
<p><strong>4. Social Engineering</strong></p>
<p>In order to produce a lasting change in the behavior of developers it helps to have a measurable number to answer where the project is and where it should be. Such information can provide in-sight into whether or not the project is getting closer or farther from its testability goal.</p>
<p>People respond to what is measured. Integrating the Testability Explorer with the project’s continuous build and publishing the report together with build artifacts communicate the importance of testability to the team. Publishing a graph of overall score over time allows the team to see changes on per check-in basis.</p>
<p>If Testability Explorer is used to identify the areas of code that need to be refactored, than compute the rate of improvement and project expected date of refactoring completion and create a sense of competition among the team members.</p>
<p>It is even possible to set up a unit test for Testability Explorer that will only allow the class whose testability cost is better than some predetermined cost.</p>
]]></content:encoded>
			<wfw:commentRss>http://misko.hevery.com/2008/10/21/testability-explorer-measuring-testability/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dependency Injection Myth: Reference Passing</title>
		<link>http://misko.hevery.com/2008/10/21/dependency-injection-myth-reference-passing/</link>
		<comments>http://misko.hevery.com/2008/10/21/dependency-injection-myth-reference-passing/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 15:01:06 +0000</pubDate>
		<dc:creator>misko</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Testability]]></category>

		<guid isPermaLink="false">http://misko.hevery.com/?p=252</guid>
		<description><![CDATA[by Miško Hevery
After reading the article on Singletons (the design anti-pattern) and how they are really global variables and dependency injection suggestion to simply pass in the reference to the singleton in a constructor (instead of looking them up in global state), many people incorrectly concluded that now they will have to pass the singleton [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="../about/">Miško Hevery</a></p>
<p>After reading the article on <a href="http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/">Singletons</a> (the design anti-pattern) and how they are really <a href="http://misko.hevery.com/2008/08/25/root-cause-of-singletons/">global variables</a> and dependency injection <a href="http://misko.hevery.com/2008/09/10/where-have-all-the-new-operators-gone/">suggestion</a> to simply pass in the reference to the singleton in a constructor (instead of looking them up in <a href="http://misko.hevery.com/2008/08/25/root-cause-of-singletons/">global state</a>), many people incorrectly concluded that now they will have to pass the singleton all over the place. Let me demonstrate the myth with the following example.</p>
<p>Let&#8217;s say that you have a LoginPage which uses the UserRepository for authentication. The UserRepository in turn uses Database Singleton to get a hold of the global reference to the database connection, like this:</p>
<pre>class UserRepository {
  private static final BY_USERNAME_SQL = "Select ...";

  User loadUser(String user) {
    <strong>Database db = Database.getInstance();</strong>
    return db.query(BY_USERNAME_SQL, user);
  }
}

class LoginPage {
  UserRepository repo = new UserRepository();

  login(String user, String pwd) {
    User user = repo.loadUser(user);
    if (user == null || user.checkPassword(pwd)) {
      throw new IllegalLoginException();
    }
  }
}</pre>
<p>The first thought is that if you follow the advice of dependency injection you will have to pass in the Database into the LoginPage just so you can pass it to the UserRepository. The argument goes that this kind of coding will make the code hard to maintain, and understand. Let&#8217;s see what it would look like after we get rid of the global variable Singleton Database look up.</p>
<p>First, lets have a look at the UserRepository.</p>
<pre>class UserRepository {
  private static final BY_USERNAME_SQL = "Select ...";
  private final Database db;

<strong>  UserRepository(Database db) {
    this.db = db;
  }
</strong>
  User loadUser(String user) {
    return db.query(BY_USERNAME_SQL, user);
  }
}</pre>
<p>Notice how the removal of Singleton global look up has cleaned up the code. This code is now easy to test since in a test we can instantiate a new UserRepository and pass in a fake database connection into the constructor. This improves testability. Before, we had no way to intercept the calls to the Database and hence could never test against a Database fake. Not only did we have no way of intercepting the calls to Database, we did not even know by looking at the API that Database is involved. (see <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/">Singletons are Pathological Lairs</a>) I hope everyone would agree that this change of explicitly passing in a Database reference greatly improves the code.</p>
<p>Now lets look what happens to the LoginPage&#8230;</p>
<pre>class LoginPage {
  UserRepository repo;

<strong>  LoginPage(Database db) {
    repo = new UserRepository(db);
  }
</strong>
  login(String user, String pwd) {
    User user = repo.loadUser(user);
    if (user == null || user.checkPassword(pwd)) {
      throw new IllegalLoginException();
    }
  }
}</pre>
<p>Since UserRepository can no longer do a global look-up to get a hold of the Database it musk ask for it in the constructor. Since LoginPage is doing the construction it now needs to ask for the Databse so that it can pass it to the constructor of the UserRepository. The myth we are describing here says that this makes code hard to understand and maintain. <strong>Guess what?! The myth is correct! The code as it stands now is hard to maintain and understand.</strong> Does that mean that dependency injection is wrong? NO! it means that you only did half of the work! In <a href="http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/">how to think about the new operator</a> we go into the details why it is important to separate your business logic from the new operators. Notice how the LoginPage violates this. It calls a new on User Repository. The issue here is that LoginPage is <a href="http://misko.hevery.com/2008/07/18/breaking-the-law-of-demeter-is-like-looking-for-a-needle-in-the-haystack/">breaking a the Law of Demeter</a>. LoginPage is asking for the Database even though it itself has no need for the Database (This greatly hinders testability as explained <a href="http://misko.hevery.com/2008/07/18/breaking-the-law-of-demeter-is-like-looking-for-a-needle-in-the-haystack/">here</a>). You can tell since LoginPage does not invoke any method on the Database. <strong>This code, like the myth suggest, is bad!</strong> So how do we fix that?</p>
<p>We fix it by doing <strong>more</strong> Dependency Injection.</p>
<pre>class LoginPage {
  UserRepository repo;

<strong>  LoginPage(UserRepository repo) {
    this.repo = repo;
  }
</strong>
  login(String user, String pwd) {
    User user = repo.loadUser(user);
    if (user == null || user.checkPassword(pwd)) {
      throw new IllegalLoginException();
    }
  }
}</pre>
<p>LoginPage needs UserRepository. So instead of trying to construct the UserRepository itself, it should simply ask for the UserRepository in the constructor. The fact that UserRepository needs a reference to Database is not a concern of the LoginPage. Neither is it a concern of LoginPage how to construct a UserRepository. Notice how this LoginPage is now cleaner and easier to test. To test we can simply instantiate a LoginPage and pass in a fake UserRepository with which we can emulate what happens on successful login as well as on unsuccessful login and or exceptions. It also nicely takes care of the concern of this myth. <strong>Notice that every object simply knows about the objects it directly interacts with. There is no passing of objects reference just to get them into the right location where they are needed.</strong> If you get yourself into this myth then all it means is that you have not fully applied dependency injection.</p>
<p>So here are the two rules of Dependency Injection:</p>
<ul>
<li>Always ask for a reference! (don&#8217;t create, or look-up a reference in global space aka Singleton design anti-pattern)</li>
<li>If you are asking for something which you are not directly using, than you are violating a <a href="http://misko.hevery.com/2008/07/18/breaking-the-law-of-demeter-is-like-looking-for-a-needle-in-the-haystack/">Law of Demeter</a>. Which really means that you are either trying to create the object yourself or the parameter should be passed in through the constructor instead of through a method call. (We can go more into this in another blog post)</li>
</ul>
<p>So <a href="http://misko.hevery.com/2008/09/10/where-have-all-the-new-operators-gone/">where have all the new operators gone</a> you ask? Well we have already answered that question <a href="http://misko.hevery.com/2008/09/10/where-have-all-the-new-operators-gone/">here</a>. And with that I hope we have put the myth to rest!</p>
<p>BTW, for those of you which are wondering why this is a common misconception the reason is that people incorrectly assume that the constructor dependency graph and the call graph are inherently identical (see <a href="http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/">this post</a>). If you construct your objects in-line (as most developers do, <a href="http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/">see thinking about the new operator</a>) then yes the two graphs are very similar. However, if you separate the object graph instantiation from the its execution, than the two graphs are independent. This independence is what allows us to inject the dependencies directly where they are needed without passing the reference through the intermediary collaborators.</p>
]]></content:encoded>
			<wfw:commentRss>http://misko.hevery.com/2008/10/21/dependency-injection-myth-reference-passing/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Where Have all the &#8220;new&#8221; Operators Gone?</title>
		<link>http://misko.hevery.com/2008/09/10/where-have-all-the-new-operators-gone/</link>
		<comments>http://misko.hevery.com/2008/09/10/where-have-all-the-new-operators-gone/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 17:10:24 +0000</pubDate>
		<dc:creator>misko</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Testability]]></category>

		<guid isPermaLink="false">http://misko.hevery.com/?p=215</guid>
		<description><![CDATA[(the other title: Your Application has a Wiring Problem)
In My main() Method Is Better Than Yours we looked into what a main() method should look like. There we introduced a clear separation between (1) the responsibility of constructing the object graph and (2) the responsibility of running the application. The reason that this separation is [...]]]></description>
			<content:encoded><![CDATA[<p><em>(the other title: Your Application has a Wiring Problem)</em></p>
<p>In <a href="http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/">My main() Method Is Better Than Yours</a> we looked into what a main() method should look like. There we introduced a clear separation between (1) the responsibility of constructing the object graph and (2) the responsibility of running the application. The reason that this separation is important was outlined in <a href="http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/">How to Think About the “new” Operator</a>. So let us look at where have all of the new operators gone&#8230;</p>
<p>Before we go further I want you to visualize your application in your mind. Think of the components of your application as physical boxes which need to be wired together to work. The wires are the references one component has to another. In an ideal application you can change the behavior of the application just by wiring the components differently. For example instead of instantiating LDAPAuthenticator you instantiate KerberosAuthenticator and you wire the KerberosAuthenticator to appropriate components which need to know about Authenticator. That is the basic idea. By <a href="http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/">removing the new operators from the application logic</a> you have separated the responsibility of wiring the components from the application logic, and this is highly desirable. So now the problem becomes, where have all the new operators gone?</p>
<p>First lets look at a manual wiring process. In the <a href="http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/">main() method</a> we asked the ServerFactory to build us a Server (in our case a <a href="http://www.mortbay.org/jetty/">Jetty</a> Web Server) Now, server needs to be wired together with servlets. The servlets, in turn, need to be wired with their services and so on. Notice that the factory bellow is full of &#8220;new&#8221; operators. We are new-ing the components and we are passing the references of one component to another to create the wiring. This is the instantiation and wiring activity I asked you to visualize above. (Full <a href="http://code.google.com/p/unit-test-teaching-examples/source/browse/trunk/src/di/webserver/ServerBuilder.java?r=6">source</a>):</p>
<pre>  public Server buildServer() {
    Server server = new Server();

    SocketConnector socketConnector
         = new SocketConnector();
    socketConnector.setPort(8080);
    server.addConnector(socketConnector);

    new ServletBuilder(server)
      .addServlet("/calc", new CalculatorServlet(
                           new Calculator()))
      .addServlet("/time", new TimeServlet(
                           new Provider() {
        public Date get() {
          return new Date();
        }
      }));

    return server;
  }</pre>
<p>When I first suggest to people that application logic should not instantiate its own dependencies, I get two common objections which are myths:</p>
<ol>
<li><em>&#8220;So now each class needs a factory, therefore I have twice as many classes!</em>&#8221; Heavens No! Notice how our ServerFactory acted as a factory for many different classes. Looking at it I counted 7 or so classes which we instantiated in order to wire up our application. So it is not true that we have one to one correspondence. In theory you only need one Factory per object lifetime. You need one factory for all long-lived objects (your singletons) and one for all request-lifetime objects and so on. Now in practice we further split those by related concepts. (But that is a discussion for a separate blog article.) The important thing to realize is that: yes, you will have few more classes, but it will be no where close to doubling your load.</li>
<li><em>&#8220;If each object asks for its dependencies, than I will have to pass those dependencies through all of the callers. This will make it really hard to add new dependencies to the classes.&#8221;</em> The myth here is that call-graph and instantiation-graph are one and the same. We looked into this myth in <a href="http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/">Where have all the Singletons Gone</a>. Notice that the Jetty server calls the TimeServlet which calls the Date. If the constructor of Date or TimeServlet all of a sudden needed a new argument it would not effect any of the callers. The only code which would have to change is factory class above. This is because we have isolated the instantiation/wiring problem into this factory class. So in reality this makes it easier to add dependencies not harder.</li>
</ol>
<p>Now there are few important things to remember. Factories should have no logic! Just instantiation/wiring (so you will probably not have any conditionals or loops). I should be able to call the factory to create a server in a unit test without any access to the file-system, threads or any other expensive CPU or I/O operations. Factory creates the server, but does not run it. The other thing you want to keep in mind is that the wiring process is often controlled by the command line arguments. This makes is so that your application can behave differently depending what you pass in on a command line. The difference in behavior is not conditionals sprinkled throughout your code-base but rather a different way of wiring your application up.</p>
<p>Finally, here are few thoughts on my love/hate of Singletons (mentioned <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/">here</a> and <a href="http://misko.hevery.com/2008/08/25/root-cause-of-singletons/">here</a>) First a little review of singletons. A singleton with a lower case &#8217;s&#8217; is a good singleton and simply means a single instance of some class. A Singleton with an upper case &#8216;S&#8217; is a design pattern which is a singleton (one instance of some class) with a global &#8220;instance&#8221; variable which makes it accessible from anywhere. It is the global instance variable which makes it globally accessible , which turns a singleton into a Singleton. So singleton is acceptable, and sometimes very helpful for a design, but Singleton relies on mutable global state, which inhibits testability and makes a brittle, hard to test design. Now notice that our factory created a whole bunch of singletons as in a single instance of something . Also notice how those singletons got explicitly passed into the services that needed them. So if you need a singleton you simply create a single instance of it in the factory and than pass that instance into all of the components which need them. There is no need for the global variable.</p>
<p>For example a common use of Singleton is for a DB connection pool. In our example you would simply instantiate a new DBConnectionPool class in the top-most factory (above) which is responsible for creating the long-lived objects. Now lets say that both CalculatorServlet and TimeServlet would need a connection pool. In that case we would simply pass the same instance of the DBConnectionPool into each of the places where it is needed. Notice we have a singleton (DBConnectionPool) but we don&#8217;t have any global variables associated with that singleton.</p>
<pre>  public Server buildServer() {
    Server server = new Server();

    SocketConnector socketConnector
         = new SocketConnector();
    socketConnector.setPort(8080);
    server.addConnector(socketConnector);

    <strong>DBConnectionPool pool = new DBConnectionPool();</strong>
    new ServletBuilder(server)
      .addServlet("/calc", new CalculatorServlet(
                           <strong>pool</strong>,
                           new Calculator()))
      .addServlet("/time", new TimeServlet(
                           <strong>pool</strong>,
                           new Provider() {
        public Date get() {
          return new Date();
        }
      }));

    return server;
  }</pre>
]]></content:encoded>
			<wfw:commentRss>http://misko.hevery.com/2008/09/10/where-have-all-the-new-operators-gone/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Where Have All the Singletons Gone?</title>
		<link>http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/</link>
		<comments>http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 18:48:41 +0000</pubDate>
		<dc:creator>misko</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[Testability]]></category>

		<guid isPermaLink="false">http://misko.hevery.com/?p=181</guid>
		<description><![CDATA[by Miško Hevery
In Singletons are Pathological Liars we discussed the problems of having singletons in your code. Let&#8217;s build on that and answer the question &#8220;If I don&#8217;t have singletons how do I ensure there is only one instance of X and how do I get X to all of the places it is needed?&#8221;
An [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="http://misko.hevery.com/about/">Miško Hevery</a></p>
<p>In <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/">Singletons are Pathological Liars</a> we discussed the problems of having singletons in your code. Let&#8217;s build on that and answer the question &#8220;If I don&#8217;t have singletons how do I ensure there is only one instance of X and how do I get X to all of the places it is needed?&#8221;</p>
<p>An OO application is a graph of objects. There are three different kinds of graphs I think of when I design an application</p>
<ol>
<li><strong>Collaborator Graph</strong>: T<span style="border-collapse: collapse; font-family: Arial;">his is the </span>graph of objects that would be emitted if you serialized your application. This shows which objects are aware of which others. (through object&#8217;s fields)</li>
<li><strong>Construction Graph</strong>: This graph shows which object created which other ones.</li>
<li><strong>Call Graph</strong>: This graph shows <span style="border-collapse: collapse;">which other methods each method calls</span>. A stack-trace would be a single slice through this graph.</li>
</ol>
<p>If the new operators are mixed with application logic (see: <a href="http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/">How to Think About the new Operator</a>) then the Constructor Graph and the Collaborator Graph tend to be one and the same. However, in an application which uses Dependency Injection the two graphs are completely independent. Lets have a closer look at our CreditCardProcessor example. Suppose this is our collaborator graph which we need to execute a request.</p>
<p><a href="http://misko.hevery.com/wp-content/uploads/2008/08/colaboratorgraph.png"><img class="aligncenter size-full wp-image-182" title="Colaborator Graph" src="http://misko.hevery.com/wp-content/uploads/2008/08/colaboratorgraph.png" alt="" width="457" height="330" /></a><br />
The above shows the application collaborator graph. The letter (S/R) in the corner designates object lifetime; either Singleton or Request scope. Now, just to be clear, there is nothing wrong with having a single instance of a class. The problem arises only when the singleton is available through a global &#8220;instance&#8221; variable as in Singleton.getInstance().</p>
<p>The HTTP request would come to AuthenticatorPage which would collaborate with Authenticator to make sure the user is valid and forward a valid request onto ChargePage which would then try to load the user from UserRepository and create the credit card transaction which would be processed by CrediCardProcessor. This in turn would collaborate with OfflineQueue to get the work done.</p>
<p>Now, in order to have a testable codebase we have to make sure that we don&#8217;t mix the object construction with application logic. So all of the above objects should rarely call the new operator (value objects are OK). Instead each of the objects above would declare its collaborators in the constructor. AuthenticatorPage would ask for ChargePage and Authenticator. ChargePage would ask for CreditCardProcessor and UserRepository. And so on. We have moved the problem of construction elsewhere.</p>
<p>In our tests it is now easy to instantiate the graph of objects and substitute test-doubles for our collaborators. For example if we would like to test the AuthenticatorPage, we would instantiate a real AuthenticatorPage with mock ChargePage and mock Authenticator. We would than assert that a request which comes in causes appropriate calls on Authenticator and ChargePage only if authentication is successful. If the AuthenticatorPage were to get a reference to Authenticator from global state or by constructing it, we would not be able to replace the Authenticator with a test-double. (This is why it is so important not to mix object construction with application logic. In the unit-test what you instantiate is a sub-set of the whole application. Hence the instantiation logic has to be separate from application logic! Otherwise, it&#8217;s a non-starter.)</p>
<p>So now the problem is, how do we construct the graph of objects?<br />
<img class="aligncenter size-full wp-image-183" title="Constructor Graph" src="http://misko.hevery.com/wp-content/uploads/2008/08/constructorgraph.png" alt="" width="488" height="482" /></p>
<p>In short we move all of the new operators to a factory. We group all of the objects of similar lifetime into a single factory. In our case all of the singletons end up in ApplicationFactory and all of the Pages end up in RequestFactory. The main method of our application instantiates an ApplicationFactory. When we call build() the ApplicationFactory in turn instantiates its list of objects (Database, OfflineQueue, Authenticator, UserRepository, CreditCardProcessor and RequestFactory). Because each of the objects declares its dependency, the ApplicationFactory is forced to instantiate the objects in the right order. In our case it must instantiate the Database first and than pass the reference to UserRepository and OfflineQueue. (The code will simply not compile any other way.)</p>
<p>Notice that when we create a RequestFactory we must pass in references to the Authenticator, UserRepository and CreditCardProcessor. This is because when we call build() on RequestFactory it will try to instantiate AuthenticatorPage which needs the Authenticator. So we need to pass the Authenticator into the constructor of RequestFactory and so on.</p>
<p>At run-time an HTTP request comes in. The servlet has a reference to RequestFactory and calls build(). The servlet now has a reference to the AuthenticatorPage and it can dispatch the request for processing.</p>
<p>Important things to notice:</p>
<ul>
<li>Every object only has references to what it needs directly! No passing around of objects which are not directly needed by the code. There is no global state at all. Dependencies are obvious since each object only asks for what it needs.</li>
<li>If an object needs a reference to a new dependency it simply declares it. This change only affects the corresponding factory, and as a result, it is very isolated.</li>
<li>All of the new operators end up in the factories; application logic is devoid of new operators.</li>
<li>You group all of the objects with the same lifetime into a single factory (If the factory gets too big you can break it up into more classes, but you can still think of it as a single factory)</li>
<li>The problem of &#8220;how do I ensure that I only have one of something&#8221; is nicely sidestepped. You instantiate only a single ApplicationFactory in your main, and as a result, you only instantiate a single instance of all of your singletons.</li>
</ul>
<p><span style="border-collapse: collapse;"><span style="font-family: Arial;">Now the factories become largely a series of object creations</span></span>. Totally boring stuff, so boring a computer could generate the code. Simply look at the constructor and recursively instantiate whatever the constructor wants. Wait, a computer can generate it! Its called <a href="http://www.picocontainer.org/web/scoping.html">PicoContainer</a> or <a href="http://code.google.com/p/google-guice/">GUICE</a>! So you don&#8217;t actually have to write the factories.</p>
]]></content:encoded>
			<wfw:commentRss>http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Singletons are Pathological Liars</title>
		<link>http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/</link>
		<comments>http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 17:43:03 +0000</pubDate>
		<dc:creator>misko</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[Testability]]></category>

		<guid isPermaLink="false">http://misko.hevery.com/?p=162</guid>
		<description><![CDATA[by Miško Hevery
So you join a new project, which has an extensive mature code base. Your new lead asks you to implement a new feature, and, as a good developer, you start by writing a test. But since you are new to the project, you do a lot of exploratory &#8220;What happens if I execute [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="../2008/08/14/about/">Miško Hevery</a></p>
<p>So you join a new project, which has an extensive mature code base. Your new lead asks you to implement a new feature, and, as a good developer, you start by writing a test. But since you are new to the project, you do a lot of exploratory &#8220;What happens if I execute this method&#8221; tests. You start by writing this:</p>
<pre>testCreditCardCharge() {
  CreditCard c =  new CreditCard(
    "1234 5678 9012 3456", 5, 2008);
  c.charge(100);
}</pre>
<p>This code:</p>
<ul>
<li>Only works when you run as part of the suite.</li>
<li>When run in isolation, throws NullPointerException.</li>
<li>When you get your credit card bill, you are out $100 for every time the test runs.</li>
</ul>
<p>Now, I want to focus on the last point. How in the world did the test cause an actual charge on my credit card? Charging a credit card is not easy. The test needs to talk to a third party credit card web-service. It needs to know the URL for the web-service. It needs to authenticate, pass the credentials, and identify who the merchant is. None of this information is present in the test. Worse yet, since I don&#8217;t even know where that information is present, how do I mock out the external dependencies so that every run does not result in $100 actually being charged? And as a new developer, how was I supposed to know that what I was about to do was going to result in me being $100 poorer? That is &#8220;Spooky action at a distance!&#8221;</p>
<p>But why do I get NullPointerException in isolation while the test works fine when run as part of the suite? And how do I fix it? Short of digging through lots of source code, you go and ask the more senior and wiser people on the project. After a lot of digging, you learn that you need to initialize the CreditCardProcessor.</p>
<pre>testCreditCardCharge() {
  CreditCardProcessor.init();
  CreditCard c =  new CreditCard(
    "1234 5678 9012 3456", 5, 2008);
  c.charge(100);
}</pre>
<p>You run the test again; still no success, and you get a different exception. Again, you chat with the senior and wiser members of the project. Someone tells you that the CreditCardProcessor needs an OfflineQueue to run.</p>
<pre>testCreditCardCharge() {
  OfflineQueue.init();
  CreditCardProcessor.init();
  CreditCard c =  new CreditCard(
    "1234 5678 9012 3456", 5, 2008);
  c.charge(100);
}</pre>
<p>Excited, you run the test again: nothing. Yet another exception. You go in search of answers and come back with the knowledge that the Database needs to be initialized in order for the Queue to store the data.</p>
<pre>testCreditCardCharge() {
  Database.init();
  OfflineQueue.init();
  CreditCardProcessor.init();
  CreditCard c =  new CreditCard(
    "1234 5678 9012 3456", 5, 2008);
  c.charge(100);
}</pre>
<p>Finally, the test passes in isolation, and again you are out $100. (Chances are that the test will now fail in the suite, so you will have to surround your initialization logic with &#8220;if not initialized then initialize&#8221; code.)</p>
<p>The problem is that the APIs are pathological liars. The credit card pretends that you can just instantiate it and call the charge method. But secretly, it collaborates with the CreditCardProcessor. The CreditCardProcessor API says that it can be initialized in isolation, but in reality, it needs the OfflineQueue. The OflineQueue needs the database. To the developers who wrote this code, it is obvious that the CreditCard needs the CreditCardProcessor. They wrote the code that way. But to anyone new on the project, this is a total mystery, and it hinders the learning curve.</p>
<p>But there is more! When I see the code above, as far as I can tell, the three init statements and the credit card instantiation are independent. They can happen in any order. So when I am re-factoring code, it is likely that I will move and rearrange the order as a side-effect of cleaning up something else. I could easily end up with something like this:</p>
<pre>testCreditCardCharge() {
  CreditCardProcessor.init();
  OfflineQueue.init();
  CreditCard c =  new CreditCard(
    "1234 5678 9012 3456", 5, 2008);
  c.charge(100);
  Database.init();
}</pre>
<p>The code just stopped working, but I had no way to knowing that ahead of time. Most developers would be able to guess that these statements are related in this simple example, but on a real project, the initialization code is usually spread over many classes, and you might very well initialize hundreds of objects. The exact order of initialization becomes a mystery.</p>
<p>How do we fix that? Easy! Have the API declare the dependency!</p>
<pre>testCreditCardCharge() {
  Database db = Database();
  OfflineQueue q = OfflineQueue(db);
  CreditCardProcessor ccp = new CreditCardProcessor(q);
  CreditCard c =  new CreditCard(
    "1234 5678 9012 3456", 5, 2008);
  c.charge(ccp, 100);
}</pre>
<p>Since the CreditCard charge method declares that it needs a CreditCardProcessor, I don&#8217;t have to  go ask anyone about that. The code will simply not compile without it. I have a clear hint that I need to instantiate a CreditCardProcessor. When I try to instantiate the CreditCardProcessor, I am faced with supplying an OfflineQueue. In turn, when trying to instantiate the OfflineQueue, I need to create a Database. The order of instantiation is clear! Not only is it clear, but it is impossible to place the statements in the wrong order, as the code will not compile. Finally, explicit reference passing makes all of the objects subject to garbage collection at the end of the test; therefore, this test can not cause any other test to fail when run in the suite.</p>
<p>The best benefit is that now, you have seams where you can mock out the collaborators so that you don&#8217;t keep getting charged $100 each time you run the test. You even have choices. You can mock out CreditCardProcessor, or you can use a real CreditCardProcessor and mock out OfflineQueue, and so on.</p>
<p>Singletons are nothing more than global state. Global state makes it so your objects can secretly get hold of things which are not declared in their APIs, and, as a result, Singletons make your APIs into pathological liars.</p>
<p>Think of it another way. You can live in a society where everyone (every class) declares who their friends (collaborators) are. If I know that Joe knows Mary but neither Mary nor Joe knows Tim, then it is safe for me to assume that if I give some information to Joe he may give it to Mary, but under no circumstances will Tim get hold of it. Now, imagine that everyone (every class) declares some of their friends (collaborators), but other friends (collaborators which are singletons) are kept secret. Now you are left wondering how in the world did Tim got hold of the information you gave to Joe.</p>
<p>Here is the interesting part. If you are the person who built the relationships (code) originally, you know the true dependencies, but anyone who comes after you is baffled, since the friends which are declared are not the sole friends of objects, and information flows in some secret paths which are not clear to you. You live in a society full of liars.</p>
<p>[ Follow up: <a href="http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/">Where Have All the Singletons Gone</a> ]</p>
<p>[ Follow up: <a href="http://misko.hevery.com/2008/08/25/root-cause-of-singletons/">Root Cause of Singletons</a> ]</p>
]]></content:encoded>
			<wfw:commentRss>http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
		<item>
		<title>Procedural Language Eliminated GOTOs; OO Eliminated IFs</title>
		<link>http://misko.hevery.com/2008/08/14/procedural-language-eliminated-gotos-oo-eliminated-ifs/</link>
		<comments>http://misko.hevery.com/2008/08/14/procedural-language-eliminated-gotos-oo-eliminated-ifs/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 01:59:35 +0000</pubDate>
		<dc:creator>misko</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Testability]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://misko.hevery.com/?p=159</guid>
		<description><![CDATA[by Miško Hevery
Procedural languages allowed us to remove GOTOs in our code. I would like to think that OO languages allow us to remove IFs (conditionals). I know you can&#8217;t remove of all the IFs, but it is interesting just how many IFs you can remove in an application.
You can&#8217;t remove these IFs:


Comparing relative sizes [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="../about/">Miško Hevery</a></p>
<p>Procedural languages allowed us to remove GOTOs in our code. I would like to think that OO languages allow us to remove IFs (conditionals). I know you can&#8217;t remove of all the IFs, but it is interesting just how many IFs you can remove in an application.</p>
<p>You can&#8217;t remove these IFs:</p>
<div class="Ih2E3d">
<ul>
<li>Comparing relative sizes (&gt;, &lt; operators)</li>
<li>Comparing primitives</li>
</ul>
</div>
<p>Recently I came across a very interesting and clever way to get rid of IFs when doing lazy initialization. Many thanks to my friend Pascal for pointing it out.</p>
<pre>private interface Getter {
  T get();
}

class abstract LazyInitializer implements Getter {
  private Getter getter = new Getter() {
    public T get() {
      final T value = initialize();
      getter = new Getter(){
        T get() { return value; }
      }
    }
  };

  protected abstract T initialize();

  public T get() {
    return getter.get();
  }
}

class Sum extends LazyInitializer {
  public Double initialize() {
    return 1+2+3+4+5+6+7+8+9;
  }
}

Getter sum = new Sum();
sum.get(); // Causes the initialization
sum.get(); // returns the previous value</pre>
<p>For extra credit you can do thread safe initializer as well</p>
<pre>class abstract ThreadSafeLazyInitializer
               implements Getter {
  private Getter getter = new Getter() {
    public synchronize T get() {
      return synchronizedGetter.get();
    }
  };

  private Getter synchornizedGetter = new Getter() {
    public T get() {
      final T value = initialize();
      gettter = new Getter(){
        T get() { return value; }
      }
      synchronizedGetter = gettter;
    }
  };

  protected abstract T initialize();

  public T get() {
    return getter.get();
  }
}</pre>
<p>The cool part about the thread-safe initializer: the cost of thread synchronization only gets paid the first time.</p>
<div class="Ih2E3d">From a testing point of view, this helps with the Separation of Concerns principle. You want your lazy initialization/thread synchronization code to be separate from the logic which computes the value. This way, you can test the laziness once and know that it works everywhere. It is also less likely that you will make a mistake in the initialization code, since you only have one.</div>
<p>Now, the above piece of code is very wordy, and you may wonder why you would write all this code when it can be written in three lines. Point well taken; but if Java was less wordy and supported closures, the above code would be significantly shorter. In languages such as JavaScript, where functions are first class citizens, the above can be written almost as succinctly as the IF clause:</p>
<pre>function myValue() {
  return ?; // Do your computation here.
}

function decorateLazy(initialize){
  var lazy =  function() { return lazy.getter(); };
  lazy.getter = function() {
    var value = initialize();
    lazy.getter = function() { return value; };
    return value;
  }
  return lazy;
}

myValue = decorateLazy(myValue);</pre>
]]></content:encoded>
			<wfw:commentRss>http://misko.hevery.com/2008/08/14/procedural-language-eliminated-gotos-oo-eliminated-ifs/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Circular Dependency in constructors and Dependency Injection</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/</link>
		<comments>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 18:06:30 +0000</pubDate>
		<dc:creator>misko</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Testability]]></category>

		<guid isPermaLink="false">http://misko.hevery.com/?p=146</guid>
		<description><![CDATA[by Miško Hevery
So you discovered dependency injection and GUICE and you are happily refactoring and writing new tests for you code until you come across this circular reference.
class A {
  final B b;
  A(B b){
    this.b = b;
  }
}

class B {
  final A a;
  B(){
   [...]]]></description>
			<content:encoded><![CDATA[<p>by <a href="../about/">Miško Hevery</a></p>
<p>So you discovered dependency injection and <a href="http://code.google.com/p/google-guice/">GUICE</a> and you are happily refactoring and writing new tests for you code until you come across this circular reference.</p>
<pre>class A {
  final B b;
  A(B b){
    this.b = b;
  }
}

class B {
  final A a;
  B(){
    this.a = new A(this);
  }
}

+---------+      +---------+
|    A    |&lt;-----|    B    |
|         |      |         |
|         |-----&gt;|         |
+---------+      +---------+</pre>
<p>Hm, dependency injection says that you need to ask for your dependencies and so the resulting code will be:</p>
<pre>class A {
  final B b;
  A(B b){
    this.b = b;
  }
}

class B {
  final A a;
  B(A a){
    this.a = a;
  }
}</pre>
<p>But now we have a problem, we can&#8217;t instantiate this (I know GUICE can through proxy, but it is not clean and it does not help us in tests). So the real problem in situation like this is mixing of concerns. One of the two objects is hiding another object C. Either A contains C or B contains C. <strong>To find out which one it is, list all of the methods in your class A used by class A, and all the methods in your class B used by class A. The shorter of the two lists is your hidden Class C.</strong></p>
<pre>+---------+      +---------+
|    A    |&lt;-----|  B      |
|         |      |  |  +-+ |
|         |      |  +-&gt;|C| |
|         |------+----&gt;| | |
|         |      |     +-+ |
+---------+      +---------+</pre>
<p>Suppose B has the shorter list. We now extract all of the methods in B which are accessing the state of hidden C methods into a new object C like this:</p>
<pre>                         +---------+
+---------+              |    B    |
|    A    |&lt;-------------|         |
|         |              |         |
|         |    +---+     |         |
|         |---&gt;| C |&lt;----|         |
|         |    +---+     +---------+
+---------+

class C {
  C(){
  }
}

class A {
  final C c;
  A(C c){
    this.c = c;
  }
}

class B {
  final A a;
  final C c;
  B(A a, C c){
    this.a = a;
    this.c = c;
  }
}</pre>
<p>When you go through this exercise you will realize that the C was always an object in its own right but you have never thought about it that way, so the new code is actually better OO. From testing point of view you can now test each class in isolation.</p>
]]></content:encoded>
			<wfw:commentRss>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
