<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Tag refactoring</title>
    <link>http://blog.objectmentor.com/articles/tag/refactoring</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Tdd for the iPhone: Continued</title>
      <description>&lt;p&gt;After a week in Israel and the UK, I got back to this series. I&amp;#8217;ve switched to using XCode 4, but as it is not officially released, the videos I recorded using it were a bad idea to upload.&lt;/p&gt;


	&lt;p&gt;Back to XCode 3: &lt;a href="http://www.vimeo.com/album/1472322"&gt;http://www.vimeo.com/album/1472322&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 22 Nov 2010 00:45:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:4aab9cfc-47ab-484a-a981-990347659e29</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2010/11/22/tdd-for-the-iphone-continued</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>TDD</category>
      <category>iphone</category>
      <category>xcode</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>The 4-contact points of software development</title>
      <description>The three laws of &lt;span class="caps"&gt;TDD&lt;/span&gt; are:
	&lt;ul&gt;
	&lt;li&gt;Write no production code without a failing test&lt;/li&gt;
		&lt;li&gt;Write just enough of a test to fail&lt;/li&gt;
		&lt;li&gt;Write just enough production code to get the test to pass&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This list doesn&amp;#8217;t include refactoring, which is typically an assumed activity. In fact, some people refer to these rules as &amp;#8220;red, green, refactor&amp;#8221;. An even older version of this, from the Smalltalk community, is Red, Green, Blue. (Why Blue for refactor? I think someone was thinking &lt;span class="caps"&gt;RBG&lt;/span&gt; for a color space, luckily they didn&amp;#8217;t try to use &lt;span class="caps"&gt;CMYK&lt;/span&gt; or &lt;span class="caps"&gt;LAB&lt;/span&gt;!)&lt;/p&gt;


	&lt;p&gt;In this simple model, there two kinds of code: test &amp;#38; production. There are two kinds of activity: writing &amp;#38; refactoring. Interestingly, at one level it is all code. The thing that distinguishes both sets is intent.&lt;/p&gt;


	&lt;p&gt;The intent of a test is to demonstrate or maybe specify behavior. The intent of production code is to implement (hopefully) business-relevant functionality.&lt;/p&gt;


	&lt;p&gt;The intent of writing code is creation. The intent of refactoring code is to change (hopefully improve) its structure without changing its behavior (this is oversimplified but essentially correct).&lt;/p&gt;


If you mix those combinations you have the&lt;i&gt; &lt;b&gt;4-limbs of development&lt;/b&gt;&lt;/i&gt;:
	&lt;ul&gt;
	&lt;li&gt;Writing a test&lt;/li&gt;
		&lt;li&gt;Writing production code&lt;/li&gt;
		&lt;li&gt;Refactoring a test&lt;/li&gt;
		&lt;li&gt;Refactoring production code&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;An important behavior to practice is doing only one of these at a time. That is, when you are writing tests, don&amp;#8217;t also write production code. Sure, you might use tools to stub out missing methods and classes, but the heart of what you are doing is writing a test. Finish that train of thought before focusing on writing production code.&lt;/p&gt;


	&lt;p&gt;On the other hand, if you are refactoring production code, do just that. Don&amp;#8217;t change tests at the same time, try to only do one refactoring at a time, etc.&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;WHY&lt;/span&gt;?&lt;/p&gt;


	&lt;p&gt;First an analogy that almost always misses since most developers don&amp;#8217;t additionally rock climb.&lt;/p&gt;


	&lt;p&gt;When rock climbing, a good general bit of advice is to only move one contact point at a time. For this discussion, consider your two hands and two feet as your four contact points. Sure, you can use your face or knee, but neither are much fun. So just considering two hands and two feet, that suggests that if, for example, you move your right hand, then leave your left hand and both feet in place.&lt;/p&gt;


	&lt;p&gt;This gives you stability, a chance to easily recover by simply moving the most recent appendage back in place and, when the inevitable happens, another appendage slips, you have a better chance of not eating rock face. If you move more than one thing at a time, you are in more danger because you&amp;#8217;ve taken a risky action and reduced the number of points of contact, or stability.&lt;/p&gt;


	&lt;p&gt;Will you sometimes move multiple appendages? Sure. But not as a habit. Sometimes you need to take risks. The rock face may not always offer up movement patterns that make applying this recommendation possible. Since you know the environment will occasionally work against you, you need to maintain some slack for the inevitable.&lt;/p&gt;


	&lt;p&gt;Practicing Test Driven Development is similar. If you change production code and tests at the same time, what happens if a test fails? What is wrong? The production code, the test, both, neither? An even more subtle problem is that tests pass but the test is fragile or heavily implementation-dependent. While not necessarily an immediate threat, it represents design debt that will eventually cause problems. (This also happens frequently when tests are written after the production code as it&amp;#8217;s seductively easy to write tests that exercise code, expressing the production&amp;#8217;s code implementation but fundamentally hiding the intent.)&lt;/p&gt;


	&lt;p&gt;Notice, if you had only refactored code, then you know the problem is in one place. When you change both, the problem space actually goes from 1 to 3 (4 if you allow for neither). Furthermore, if you are changing both production and test code at the same time and you get to a point where you&amp;#8217;ve entered a bottomless pit, you&amp;#8217;ll end up throwing away more work if you choose to restore from the repository.&lt;/p&gt;


	&lt;p&gt;Are there going to be times when you change both? Sure. Sometimes you may not see a clear path that gives you the option to do only one thing at a given time. Sometimes the tests and code will work against you. Often, you&amp;#8217;ll be working in a legacy code base where there are no tests. Given that the environment will occasionally (or frequently) work against you, you need to maintain some slack.&lt;/p&gt;


	&lt;p&gt;Essentially, be focused on a single goal at any given time: write a test. then get it to pass. clean up production code &amp;#38; keep the tests first unchanging and then passing.&lt;/p&gt;


	&lt;p&gt;I find that this is a hard thing both to learn and to apply. I frequently jump ahead of myself. Unfortunately I&amp;#8217;m &amp;#8220;lucky&amp;#8221; enough when I do jump ahead that when I fail, I thoroughly fall flat on my face.&lt;/p&gt;


	&lt;p&gt;This approach is contextual (aren&amp;#8217;t they all?). Every time you start working on code, you&amp;#8217;ll be faced with these four possibilities. Each time you are, you need to figure out what is the most important thing in the moment, and do that one thing. Once you&amp;#8217;ve taken care of the most important thing, you may have just promoted the second most important thing to first place. Even so, reassess. What is the most important thing now? Do that.&lt;/p&gt;


	&lt;p&gt;Good luck trying to apply this idea to your development work. I&amp;#8217;m interesting in hearing about it.&lt;/p&gt;</description>
      <pubDate>Thu, 19 Aug 2010 22:59:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:a43fda5f-eaf6-4d2d-bb76-34f395bebda2</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2010/08/19/the-4-contact-points-of-software-development</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>TDD</category>
      <category>refactoring</category>
      <category>4</category>
      <category>contact</category>
      <category>points</category>
    </item>
    <item>
      <title>A Few C plus plus TDD videos</title>
      <description>&lt;p&gt;Using CppUTest, gcc 4.4 and the Eclipse &lt;span class="caps"&gt;CDT&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Rough, as usual.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.vimeo.com/album/254486"&gt;The Video Album&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Might redo first one with increased font size. Considering redoing whole series at 800&amp;#215;600.&lt;/p&gt;</description>
      <pubDate>Mon, 12 Jul 2010 06:30:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:a05f9c5f-4b90-46ab-9eaf-72750d3ae303</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2010/07/12/a-few-c-plus-plus-tdd-videos</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>TDD</category>
      <category>c</category>
      <category>plus</category>
      <category>std</category>
      <category>tr1</category>
      <category>accumulate</category>
      <category>for_each</category>
      <category>bind</category>
      <category>_1</category>
      <category>refactoring</category>
      <category>array</category>
      <category>vector</category>
      <category>random</category>
    </item>
    <item>
      <title>Parts 5 and 6 of the 4-part series</title>
      <description>&lt;p&gt;The title says it all. I was bothered by a few things in the Shunting Yard Algorithm (actually many more things), but I felt compelled to fix two of those things.&lt;/p&gt;


So if you have a &lt;a href="http://vimeo.com/album/210446"&gt;look at the album&lt;/a&gt;, you&amp;#8217;ll notice 2 more videos:
	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://vimeo.com/10979675"&gt;Video 5 of 4&lt;/a&gt;: Remove the need for spaces between tokens.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://vimeo.com/10994492"&gt;Video 6 of 4&lt;/a&gt;: Remove duplication of operators in algorithm and tokenizer.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Hope these are interesting or at least entertaining.&lt;/p&gt;</description>
      <pubDate>Fri, 16 Apr 2010 20:57:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e2b52b26-ffe1-4953-8749-9f6987cb224a</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2010/04/16/parts-5-and-6-of-the-4-part-series</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>c</category>
      <category>TDD</category>
      <category>visual</category>
      <category>studio</category>
      <category>resharper</category>
      <category>shunting</category>
      <category>yard</category>
      <category>algorithm</category>
      <category>dry</category>
      <category>SRP</category>
      <category>don</category>
      <category>t</category>
      <category>repeat</category>
      <category>yourself</category>
      <category>single</category>
      <category>responsibility</category>
      <category>Principle</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>Injecting Those Dependencies</title>
      <description>&lt;p&gt;Last week I was teaching a class with a good group of C++ developers in Chicago. They were up on C++, current standardization efforts, virtual machines, performance analysis and tuning. At one point I mentioned a metric for virtual method dispatch on the &lt;span class="caps"&gt;JVM&lt;/span&gt; and one of the students used my numbers to work backwards to determine the number of instructions on different processors.&lt;/p&gt;


	&lt;p&gt;I was teaching our Working Effectively with Legacy Code class. Michael Feathers usually teaches this class, but he was busy (probably working with legacy code somewhere, which, ironically, is why he wrote the book &amp;#8211; to solve the problem once and for all and to stop working with legacy code).&lt;/p&gt;


In an early project we have some problem code (the class was in Java, but we discussed C++ quite a bit):
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    public void addEvent(Event event) {
        event.added();
        events.add(event);
        mailService.sendMail(&amp;quot;jacques@spg1.com&amp;quot;, &amp;quot;Event Notification&amp;quot;, event
                .toString());
        display.showEvent(event);
    }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

The problem with this code, however, comes from what happens in the constructor:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    public Scheduler(String owner, SchedulerDisplay display) {
        this.owner = owner;

        mailService = MailService.getInstance();
        this.display = new SchedulerDisplay();
    }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The display object is associated with a real device (OK, not in our simulation, but you get the point). We need to fix that dependency.&lt;/p&gt;


So we traditionally talk about a few ways to address that:
	&lt;ol&gt;
	&lt;li&gt;Create an interface and then have a concrete and test-double implementation&lt;/li&gt;
		&lt;li&gt;Create a test subclass&lt;/li&gt;
	&lt;/ol&gt;


Those are a few typical ways to handle this problem, but given that the students also worked in C++ and were very concerned with performance (and dynamic binding), so we discussed more alternatives:
1. Link seam &amp;#8211; build the system using the original SchedulerDispaly header file but link in a different version of the class. 2. Template parameter (compiler seam) &amp;#8211; introduce a template parameter and compile in the dependency rather than link it in.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;#pragma once

template&amp;lt;class D&amp;gt; class Scheduler
{
public:
  Scheduler(D &amp;amp;display);
  ~Scheduler();
  void handleEvent();

private:
  D &amp;amp;display;
};&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

So here are 4 ways to remove the dependency (working bottom up in a sense):
	&lt;ol&gt;
	&lt;li&gt;Link seam: link in a different version of SchedulerDisplay&lt;/li&gt;
		&lt;li&gt;Compiler seam: use a template parameter&lt;/li&gt;
		&lt;li&gt;Dynamic seam: create a test subclass that removes external dependency&lt;/li&gt;
		&lt;li&gt;Dynamic seam: extract an interface from existing concrete class, and make Scheduler depend on the new interface.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;For C++, the first option might be a good option if it allows a very quick build/test cycle. Imagine using a class that brings in several unwanted dependencies. By making a test version replacement and linking to it, you might be able to get up and running quickly. On the other hand, you have to have a custom build target, which is really outside of the language.&lt;/p&gt;


The second option is good for C++ and maybe C#, but not Java. Java&amp;#8217;s implementation of generics is so bad, that it requires an interface for this particular case, so you end up with the final option anyway. Using templates puts the seam into the language rather than the build system. It will affect clients because either:
	&lt;ol&gt;
	&lt;li&gt;They will have to provide a template parameter&lt;/li&gt;
		&lt;li&gt;Or, by providing a default value for the template parameter, clients will be aware of a class they were not previously aware of. Still, a viable option, but it will increase build time at least a little.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;The third option may not work in any language without changing the original problem code. Imagine a no argument constructor that performs some static initialization. Without changing the constructor, you cannot guarantee that this approach will work. In any case, the problem as presented requires a change since the constructor was doing the initialization of the display; it did not allow for dependency injection.&lt;/p&gt;


	&lt;p&gt;The final option requires a bit more change, but is pretty flexible if you can afford the virtual method overhead. In Java this isn&amp;#8217;t much of an issue (for a number of reasons, mostly related to effective &lt;span class="caps"&gt;JIT&lt;/span&gt; optimization). The cost is a bit more of a problem in C++ because introducing a first virtual method is a big deal. Personally, I don&amp;#8217;t work on embedded systems, so I can generally ignore that problem. But this is not something to simply ignore, context does matter.&lt;/p&gt;


	&lt;p&gt;So which option is the best?&lt;/p&gt;


	&lt;p&gt;None of the above.&lt;/p&gt;


	&lt;p&gt;As a developer, you should probably be aware of all of these options (or maybe not the template option of you&amp;#8217;re strictly a Java developer). Then, when you are faced with a legacy coding problem, you&amp;#8217;ll have more tools from which to choose.&lt;/p&gt;</description>
      <pubDate>Mon, 22 Mar 2010 16:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:079869bc-e7ef-4708-9a69-7b24541bd190</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>welc</category>
      <category>refactoring</category>
      <category>dependency</category>
      <category>injection</category>
    </item>
    <item>
      <title>Name that refactoring: 2 - Version 2</title>
      <description>&lt;p&gt;A few updates applied to the second name that refactoring. Note that I&amp;#8217;m using a star to represent a problem dependency. It is the &amp;#8220;star&amp;#8221; of the refactoring. I&amp;#8217;m looking for a better image. I could go with a database icon, but the principle is more general than that. The cloud was confusing. So if you have an idea, please let me know what it is!&lt;/p&gt;


	&lt;p&gt;Again, thanks for taking a look and giving me the feedback.&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 0: Problematic Dependency&lt;/i&gt;&lt;/b&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_2-0.png/109818239/NameThatRefactoring_2-0.png"/&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 1: Capture dependency in a single class&lt;/i&gt;&lt;/b&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_2-1.png/109818237/NameThatRefactoring_2-1.png"/&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 2: Raise the abstraction to a domain-level interface&lt;/i&gt;&lt;/b&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_2-2.png/109818235/NameThatRefactoring_2-2.png" /&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 3: Introduce test doubles as needed&lt;/i&gt;&lt;/b&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_2-3.png/109818233/NameThatRefactoring_2-3.png" /&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 16 Dec 2009 02:14:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:8a78598f-7598-49e9-85c3-415b4afb89f3</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/12/16/name-that-refactoring-2-version-2</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>refactoring</category>
      <category>TDD</category>
      <category>picture</category>
      <category>writing</category>
    </item>
    <item>
      <title>Name that refactoring: 1 - Version 2</title>
      <description>&lt;p&gt;Here is an update to the first name that refactoring based on feedback. How about this?&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 0: Method with embedded dependency&lt;/b&gt;&lt;/i&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_1-1.png/109816073/NameThatRefactoring_1-1.png"/&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 1:Extract method to move dependency to another method&lt;/b&gt;&lt;/i&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_1-2.png/109816629/NameThatRefactoring_1-2.png"/&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 2: Subclass and override method with new code that does not have original dependency&lt;/b&gt;&lt;/i&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_1-3.png/109816627/NameThatRefactoring_1-3.png"/&gt;&lt;/p&gt;


	&lt;p&gt;So what do you think? Better or worse?&lt;/p&gt;


	&lt;p&gt;What about using the star in other image? In general, the star could represent the dependency we&amp;#8217;re trying to get rid of.&lt;/p&gt;</description>
      <pubDate>Wed, 16 Dec 2009 01:43:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f54168c7-20d6-4c62-ba6f-7de62ec2295d</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/12/16/name-that-refactoring-1-version-2</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>refactoring</category>
      <category>TDD</category>
      <category>picture</category>
      <category>writing</category>
    </item>
    <item>
      <title>Name that refactoring: 2</title>
      <description>&lt;p&gt;Ok, so I&amp;#8217;m getting good feedback on the first picture. Now for a series of pictures.&lt;/p&gt;


	&lt;p&gt;How do you interpret this series?&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 0&lt;/b&gt;&lt;/i&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_2-1.png/109310773/NameThatRefactoring_2-1.png"/&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 1&lt;/b&gt;&lt;/i&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_2-2.png/109310771/NameThatRefactoring_2-2.png"/&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 2&lt;/b&gt;&lt;/i&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_2-3.png/109310769/NameThatRefactoring_2-3.png"/&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;&lt;i&gt;Step 3&lt;/b&gt;&lt;/i&gt;&lt;p/&gt;
&lt;img src="http://schuchert.wikispaces.com/file/view/NameThatRefactoring_2-4.png/109310767/NameThatRefactoring_2-4.png"/&gt;&lt;/p&gt;</description>
      <pubDate>Sun, 13 Dec 2009 22:51:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f612878a-68b6-427b-bca5-78783f70c884</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/12/13/name-that-refactoring-2</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>refactoring</category>
      <category>TDD</category>
      <category>picture</category>
      <category>writing</category>
    </item>
    <item>
      <title>Name that refactoring: 1</title>
      <description>&lt;p&gt;I&amp;#8217;m working on writing up a few handouts I can use in a &lt;span class="caps"&gt;TDD&lt;/span&gt; class (and a few other places). There are a few drawings I keep doing and I&amp;#8217;m trying to replicate them so I can refer to something.&lt;/p&gt;


Problem is, with many such drawings, the observation of the creation is as important as the end product. I&amp;#8217;d like your feedback/recommendations (or links to better pictures). I&amp;#8217;m curious about at least two things.
	&lt;ul&gt;
	&lt;li&gt;What does this picture suggest to you or what can you draw from it, if anything?&lt;/li&gt;
		&lt;li&gt;Do you think this would be better served as a series of pictures showing the build-up?&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;&lt;img src=http://schuchert.wikispaces.com/file/view/NameThatRefactoring_1.png/109280805/NameThatRefactoring_1.png"&gt;&lt;/p&gt;


	&lt;p&gt;Thanks for your feedback.&lt;/p&gt;</description>
      <pubDate>Sun, 13 Dec 2009 19:35:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:55886ea2-c35a-4ab2-a166-7e2dd425404b</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/12/13/name-that-refactoring-1</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>refactoring</category>
      <category>TDD</category>
      <category>picture</category>
      <category>writing</category>
    </item>
    <item>
      <title>Improving Testability of GUI Code, an Eample</title>
      <description>&lt;p&gt;Just finished first draft. More to come, questions/comments appreciated.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://schuchert.wikispaces.com/tdd.Refactoring.UiExample"&gt;http://schuchert.wikispaces.com/tdd.Refactoring.UiExample&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 10 Sep 2009 23:08:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:02584823-cab7-4003-8a06-87e6767455fb</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/09/10/improving-testability-of-gui-code-an-eample</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>test</category>
      <category>gui</category>
      <category>SRP</category>
      <category>welc</category>
      <category>refactoring</category>
      <category>legacy</category>
    </item>
    <item>
      <title>Another Refactoring Exercise: Design Patterns Recommended!-)</title>
      <description>&lt;p&gt;Well the previous exercise was fun so here&amp;#8217;s another one. The following code is taken from the same problem, an &lt;span class="caps"&gt;RPN&lt;/span&gt; calculator. Originally, the interface of the calculator was &amp;#8220;wide&amp;#8221;; there was a method for each operator. E.g., plus(), minus(), factorial(). In an effort to fix this, a new method, perform(String operatorName) was added and ultimately the interface was fixed gradually to remove those methods.&lt;/p&gt;


	&lt;p&gt;Changing he calculator &lt;span class="caps"&gt;API&lt;/span&gt; in this way is an example of applying the open/closed principle. However, the resulting code is just a touch ugly (I made it a little extra ugly just for the hack [sic] of it). This code as written does pass all of my unit tests.&lt;/p&gt;


Before the code, however, let me give you a little additional information:
	&lt;ul&gt;
	&lt;li&gt;I changed the calculator to use BigDecimal instead of int&lt;/li&gt;
		&lt;li&gt;Right now the calculator has three operators, +, &amp;#8211; !&lt;/li&gt;
		&lt;li&gt;Eventually, there will be many operators (50 ish)&lt;/li&gt;
		&lt;li&gt;Right now there are only binary and unary operators, however there will be other kinds: ternary, quaternary, and others such as sum the stack and replace just the sum on the stack or calculate the prime factors of the top of the stack so take one value but push many values&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So have a look at the following code and then either suggest changes or provide something better. There&amp;#8217;s a lot that can be done to this code to make it clearer and make the system easier to extend.&lt;/p&gt;


&lt;h3&gt;The Perform Method&lt;/h3&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_Java "&gt;   public void perform(String operatorName) {
      BigDecimal op1 = stack.pop();

      if (&amp;quot;+&amp;quot;.equals(operatorName)) {
         BigDecimal op2 = stack.pop();
         stack.push(op1.add(op2));
         currentMode = Mode.inserting;
      } else if (&amp;quot;-&amp;quot;.equals(operatorName)) {
         BigDecimal op2 = stack.pop();
         stack.push(op2.subtract(op1));
         currentMode = Mode.inserting;
      } else if (&amp;quot;!&amp;quot;.equals(operatorName)) {
         op1 = op1.round(MathContext.UNLIMITED);
         BigDecimal result = BigDecimal.ONE;
         while (op1.compareTo(BigDecimal.ONE) &amp;gt; 0) {
            result = result.multiply(op1);
            op1 = op1.subtract(BigDecimal.ONE);
         }
         stack.push(result);
      } else {
         throw new MathOperatorNotFoundException();
      }
   }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Unlike the last example, I&amp;#8217;ll provide the entire class. Feel free to make changes to this class as well. However, for now focus on the&lt;i&gt; &lt;b&gt;perform(...)&lt;/b&gt;&lt;/i&gt; method.&lt;/p&gt;


	&lt;p&gt;One note, Philip Schwarz recommended a change to what I proposed to avoid the command/query separation violation. I applied his recommendation before posting this updated version.&lt;/p&gt;


&lt;h3&gt;The Whole Class&lt;/h3&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_Java "&gt;package com.scrippsnetworks.calculator;

import java.math.BigDecimal;
import java.math.MathContext;

public class RpnCalculator {
   private OperandStack stack = new OperandStack();
   private Mode currentMode = Mode.accumulating;

   enum Mode {
      accumulating, replacing, inserting
   };

   public RpnCalculator() {
   }

   public void take(BigDecimal value) {
      if (currentMode == Mode.accumulating)
         value = determineNewTop(stack.pop(), value);

      if (currentMode == Mode.replacing)
         stack.pop();

      stack.push(value);
      currentMode = Mode.accumulating;
   }

   private BigDecimal determineNewTop(BigDecimal currentTop, BigDecimal value) {
      BigDecimal newTopValue = currentTop;
      String digits = value.toString();
      while (digits.length() &amp;gt; 0) {
         newTopValue = newTopValue.multiply(BigDecimal.TEN);
         newTopValue = newTopValue.add(new BigDecimal(Integer.parseInt(digits
               .substring(0, 1))));
         digits = digits.substring(1);
      }

      return newTopValue;
   }

   public void enter() {
      stack.dup();
      currentMode = Mode.replacing;
   }

   public void perform(String operatorName) {
      BigDecimal op1 = stack.pop();

      if (&amp;quot;+&amp;quot;.equals(operatorName)) {
         BigDecimal op2 = stack.pop();
         stack.push(op1.add(op2));
         currentMode = Mode.inserting;
      } else if (&amp;quot;-&amp;quot;.equals(operatorName)) {
         BigDecimal op2 = stack.pop();
         stack.push(op2.subtract(op1));
         currentMode = Mode.inserting;
      } else if (&amp;quot;!&amp;quot;.equals(operatorName)) {
         op1 = op1.round(MathContext.UNLIMITED);

         BigDecimal result = BigDecimal.ONE;
         while (op1.compareTo(BigDecimal.ONE) &amp;gt; 0) {
            result = result.multiply(op1);
            op1 = op1.subtract(BigDecimal.ONE);
         }
         stack.push(result);
      } else {
         throw new MathOperatorNotFoundException();
      }
   }

   public BigDecimal getX() {
      return stack.x();
   }

   public BigDecimal getY() {
      return stack.y();
   }

   public BigDecimal getZ() {
      return stack.z();
   }

   public BigDecimal getT() {
      return stack.t();
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Tue, 09 Jun 2009 20:57:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:9a050400-2d6e-4f85-a4a5-f4081b39f1e5</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/06/09/another-refactoring-exercise-design-patterns-recommended</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>refactoring</category>
      <category>exercise</category>
      <category>design</category>
      <category>patterns</category>
      <category>abstract</category>
      <category>factory</category>
      <category>strategy</category>
      <category>template</category>
      <category>method</category>
    </item>
    <item>
      <title>Refactoring Exercise</title>
      <description>&lt;p&gt;So I&amp;#8217;ve written a somewhat ugly method with the intent of having people clean it up. Want to give it a try? Post your results and then I&amp;#8217;ll show what I ended up doing in a few days (or after the activity dies down).&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s in Java, but feel free to post in other languages. I&amp;#8217;d be interested in seeing something in whatever language Michael Feathers happens to be deeply studying right now!-)&lt;/p&gt;


	&lt;p&gt;Oh, and if you feel like going &amp;#8220;overboard&amp;#8221; and using a few design patterns just because you can, I&amp;#8217;d like to see that as well.&lt;/p&gt;


	&lt;p&gt;Here it is:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_java "&gt;  public void take(int v) {
    if (currentMode == Mode.accumulating) {
      int digits = (int)Math.pow(10, (int)Math.log10(v) + 1);
      int x = stack.pop();
      x *= digits;
      x += v;
      stack.push(x);
    }

    if (currentMode == Mode.replacing) {
      stack.pop();
      stack.push(v);
      currentMode = Mode.accumulating;
    }

    if (currentMode == Mode.inserting) {
      int top = stack.peek();
      stack.push(top);
      stack.push(v);
      currentMode = Mode.accumulating;
    }
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 04 Jun 2009 21:23:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ce8dfe6e-cd31-4b42-acee-f98327c0bd2a</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>refactoring</category>
      <category>exercise</category>
    </item>
    <item>
      <title>Strict Mocks and Characterization Tests</title>
      <description>&lt;p&gt;This week I worked with a great group in Canada. This group of people had me using Moq for the first time and I found it to be a fine mocking tool. In fact, it reminded me of why I think the Java language is now far outclassed by C# and only getting more behind (luckily the &lt;span class="caps"&gt;JVM&lt;/span&gt; has many languages to offer).&lt;/p&gt;


One issue this group is struggling with is a legacy base with several services written with static &lt;span class="caps"&gt;API&lt;/span&gt;&amp;#8217;s. These classes are somewhat large, unwieldy and would be much improved with some of the following refactorings:
	&lt;ul&gt;
	&lt;li&gt;Replace switch with polymorphism&lt;/li&gt;
		&lt;li&gt;Replace type code with strategy/state&lt;/li&gt;
		&lt;li&gt;Introduce Instance Delegator&lt;/li&gt;
		&lt;li&gt;Use a combination of template method pattern + strategy and also strategy + composite&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This is actually pretty standard stuff and this group understands the way forward. But what of their existing technical debt?&lt;/p&gt;


	&lt;p&gt;Today we picked one method in particular and attempted to work our way through it. This method was a classic legacy method (no unit tests). It also had a switch on type and then it also did one or more things based on a set of options. All of this was in one method.&lt;/p&gt;


	&lt;p&gt;If you read &lt;a href="http://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Technology/dp/0201485672/ref=sr_1_1?ie=UTF8&amp;#38;s=books&amp;#38;qid=1243043314&amp;#38;sr=8-1"&gt;Fowler&amp;#8217;s Refactoring Book&lt;/a&gt;, it mentions a combination of encapsulating the type code followed by replacing switch with polymorphism for the first problem in this method (the switch). We were able to skip encapsulating the type code since we wanted to keep the external &lt;span class="caps"&gt;API&lt;/span&gt; unchanged (legacy code).&lt;/p&gt;


	&lt;p&gt;So we first created a base strategy for the switch and then several empty derived classes, one for each of the enums. This is a safe legacy refactoring because it only involved adding new code.&lt;/p&gt;


	&lt;p&gt;Next, we created a factory to create the correct strategy based on the type code and added that to the existing method (we also added a few virtual methods). Again, a safe refactoring since it only involved adding effectively unused code (we did create the factory using nearly strict &lt;span class="caps"&gt;TDD&lt;/span&gt;). Finally, we delegated from the original method to the strategy returned from the factory. Safe again, since we had tested the factory.&lt;/p&gt;


	&lt;p&gt;So far, so good. But next, we wanted to push the method down to each of the subclasses and remove the parts of the logic that did not apply to each given type. We did a spike refactoring to see what that&amp;#8217;d be like and it was at least a little dicey. We finally decided to get the original method under test so that as we refactored, we had the safety net necessary to refactor with confidence.&lt;/p&gt;


	&lt;p&gt;We started by simply calling the method with null&amp;#8217;s and 0 values. We worked our way through the method, adding hand-rolled test doubles until we came across our first static class.&lt;/p&gt;


Their current system has &lt;span class="caps"&gt;DAO&lt;/span&gt;&amp;#8217;s with fully static interfaces. This is something that is tough to fake (well we were not using and &lt;span class="caps"&gt;AOP&lt;/span&gt; framework, so &amp;#8230;). Anyway, this is where we introduced the instance delegator. We:
	&lt;ul&gt;
	&lt;li&gt;Added an instance of the class as a static member (essentially creating a singleton).&lt;/li&gt;
		&lt;li&gt;Added a property setter and getter (making it an overridable singleton).&lt;/li&gt;
		&lt;li&gt;We then copied the body of the static method into an instance method, which we made virtual.&lt;/li&gt;
		&lt;li&gt;We then delegated the static method to the virtual method.&lt;/li&gt;
		&lt;li&gt;Then, in the unit test, we set the singleton to a hand-coded test double in the setup and reset the singleton in the tear down method.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;We had to do this several times and on the third time (I think it was the third time), the hand-rolled test double would have had to implement several (17ish) methods and it became clear that we were ready to use a mocking framework. They are using Moq so we started using Moq to accomplish the remainder of the mocking.&lt;/p&gt;


	&lt;p&gt;After some time, we managed to get a test that essentially sent a tracer bullet through one path of the method we wanted to get under test. When the test turned green there was much rejoicing.&lt;/p&gt;


However, we had to ask the question: &amp;#8220;So what are we testing?&amp;#8221; After some discussion, we came up with a few things:
	&lt;ul&gt;
	&lt;li&gt;This method currently makes calls to the service layers and those calls depend on both an enumeration (replaced with a shallow and wide hierarchy of strategies) and options (to be replaced with a composition of strategies).&lt;/li&gt;
		&lt;li&gt;It also changes some values in an underling domain object.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So that&amp;#8217;s what we needed to &lt;a href="http://en.wikipedia.org/wiki/Characterization_Test"&gt;characterize&lt;/a&gt;.&lt;/p&gt;


We had a discussion on this and as a group. We wanted a way to report on the actual method calls so we could then assert (or in Moq parlance Verify). We looked at using Moq&amp;#8217;s callbacks, but it appears that those are registered on a per-method basis. We briefly toyed with the idea of using an &lt;span class="caps"&gt;AOP&lt;/span&gt; tool to introduce tracing, but that&amp;#8217;s for another time (I&amp;#8217;m thinking of looking into it out of curiosity) but we decided that we could instead do the following:
	&lt;ul&gt;
	&lt;li&gt;Begin as we already had, get through the method with a tracer.&lt;/li&gt;
		&lt;li&gt;Determine the paths we want to get under test.&lt;/li&gt;
		&lt;li&gt;For each path:
	&lt;ul&gt;
	&lt;li&gt;Create a test using strict mocks (which fail as soon as an unexpected method is called)&lt;/li&gt;
		&lt;li&gt;Use a Setup to document this call as expected &amp;#8211; this is essentially one of the assertions for the characterization test.&lt;/li&gt;
		&lt;li&gt;Continue until we have all the Setups required to get through the test.&lt;/li&gt;
		&lt;li&gt;Add any final assertions based on state-based checks and call VerifyAll on the Moq-based mock object.&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This would be a way we could work through the method and characterize it before we start refactoring it in earnest.&lt;/p&gt;


	&lt;p&gt;This might sound like a lot of work and it certainly is no cake walk, but all of this work was done by one of the attendees and as a group they certainly have the expertise to do this work. And in reality, it did not take too long. As they practice and get some of the preliminary work finished, this will be much easier.&lt;/p&gt;


Overall, it was a fun week. We:
	&lt;ul&gt;
	&lt;li&gt;Spent time on one project practicing &lt;span class="caps"&gt;TDD&lt;/span&gt; and refactoring to patterns (they implemented 5 of the GoF patterns).&lt;/li&gt;
		&lt;li&gt;Spent time practicing some of Fowler&amp;#8217;s refactorings and Feather&amp;#8217;s legacy refactorings.&lt;/li&gt;
		&lt;li&gt;Spent a day practicing &lt;span class="caps"&gt;TDD&lt;/span&gt; using mocks for&lt;i&gt; &lt;b&gt;everything&lt;/b&gt;&lt;/i&gt; but the unit under test. At the end they had a test class, one production class and several interfaces.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;In retrospect, the work they did in the first three days was nearly exactly what they needed to practice for the final day of effort. When we started tackling their legacy code, they had already practiced everything they used in getting the method under test.&lt;/p&gt;


	&lt;p&gt;So overall great week with a fun group of guys in Canada.&lt;/p&gt;</description>
      <pubDate>Fri, 22 May 2009 20:34:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:cbf32d08-3237-4a89-85ed-807948fd45e0</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/05/22/strict-mocks-and-characterization-tests</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>characterization</category>
      <category>moq</category>
      <category>mock</category>
      <category>c</category>
      <category>design</category>
      <category>patterns</category>
      <category>legacy</category>
      <category>code</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>Refactoring Finds Dead Code</title>
      <description>&lt;p&gt;One of the many things that I just love about my job as a consultant/mentor is when I actually get to sit down with programmers and pair program with them. This doesn&amp;#8217;t seem to happen nearly as often as I would like, so when two developers at a recent client site asked me if I could look at some legacy code to see if I could figure out how to get some tests around it, I jumped at the opportunity. We acquired a room equipped with a projector and a whiteboard. A laptop was connected to the projector and we were all able to comfortably view the code.&lt;/p&gt;


	&lt;p&gt;I visit a lot of different companies and see a lot of absolutely ghastly code. What I was looking at here wasn&amp;#8217;t all that bad. Variable names were not chosen to limit keystrokes and method names appeared to be descriptive. This was good news, as I needed to understand how this was put together before I could offer help with a testing strategy.&lt;/p&gt;


	&lt;p&gt;As we walked through the code, I noticed that there were classes in the project directory ending in &amp;#8216;Test&amp;#8217;. This took me by surprise. Usually when I&amp;#8217;m helping people with legacy code issues, there aren&amp;#8217;t any tests. Here, there were tests in place and they actually passed. Very cool, but now my mission wasn&amp;#8217;t clear to me as I thought my help was needed getting tests in place around legacy code.&lt;/p&gt;


	&lt;p&gt;The developers clarified that they wanted help in testing private methods. Ah ha, the plot thickens.&lt;/p&gt;


	&lt;p&gt;The question of testing private methods comes up frequently whether I&amp;#8217;m teaching a class or consulting on a project. My first response is a question. &amp;#8220;Is the private method being tested through the public interface to the class?&amp;#8221; If that&amp;#8217;s the case, then there&amp;#8217;s nothing to worry about and I can steer the conversation away from testing private methods to testing behaviors of a class instead of trying to test individual methods. Note that a private method being tested through its public interface would be guaranteed if the class was developed &lt;span class="caps"&gt;TDD&lt;/span&gt; style where the test is written first, followed by one or more public methods to make the test pass, followed by one or more extract method refactorings, which would be the birth of the private methods. This is almost never the case. My client didn&amp;#8217;t know how the code was developed, but by inspection they concluded that the parameters of the test were adequately exercising the private method.&lt;/p&gt;


	&lt;p&gt;It looked like my work was done here. But not so fast.&lt;/p&gt;


	&lt;p&gt;I have a policy that says that whenever I have code up in an editor, I have to try to leave it just a little bit better than when I found it. Since we had put the testing matter to rest and we still had some time left in the conference room before another meeting started, I suggested that we see if we could make some small improvements to the code we were examining.&lt;/p&gt;


	&lt;p&gt;As I said earlier, the code wasn&amp;#8217;t horrible. The code smell going past my nostrils was Long Method and the cure was Extract Method.&lt;/p&gt;


	&lt;p&gt;The overall structure of the method we were examining was&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
    if( conditional_1 )
    {
        // do lots of complicated stuff
    }
    else if( conditional_2 )
    {
        // do even more complicated stuff
    }
    else
    {
        // do stuff so complicated nobody understood it
    }
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;where conditional_1 was some horribly convoluted expression involving lots of &amp;#38;&amp;#38;&amp;#8217;s, ||&amp;#8217;s, and parentheses. Same for condition_2, which also had a few ^&amp;#8217;s thrown in for good luck. To understand what the method did, one would have to first understand the details of how the method did it.&lt;/p&gt;


	&lt;p&gt;I asked the developers if they could come up with a nice, descriptive method name that described what I&amp;#8217;m calling condition_1 so that we could do an extract method refactoring and the code would look like:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
    if( descriptive_name() )
    {
        // do lots of complicated stuff
    }
    // etc
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now there were less details to understand when trying to determine what this method did. If we were to stop here and check in the code, we could leave the session feeling good as the code is better than when we started. But we still had time before we had to abandon our conference room so I pressed on.&lt;/p&gt;


	&lt;p&gt;&amp;#8220;Can you summarize what this code does as a descriptive method name,&amp;#8221; I asked. The developers pondered a few moments and came up with what they felt was a good name. Excellent. We did the same procedure for the &amp;#8220;else if&amp;#8221; clause. When we finished that, one of the developers said something along the lines of, &amp;#8220;That was the easy part, I have no idea what this [else] remaining code does.&amp;#8221; I was going to pat everybody on the back and call it a day because the code had improved tremendously from when we started, but the developers seemed to have a &amp;#8220;we can&amp;#8217;t stop now&amp;#8221; attitude. They studied the code, pondered, cursed, discussed some more, and then one of them said, &amp;#8220;This code can never execute!&amp;#8221;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;d like to use the expression, &amp;#8220;You could have heard a pin drop,&amp;#8221; to describe the silence in the room, but since there were only three of us, the phrase looses its power. As it turns out, now that the if() and else if() conditionals were given descriptive names and people could grok them at a glance, it became obvious that the business rules didn&amp;#8217;t permit the final else &amp;#8211; the first two conditions were all that could exist and the most complicated code of all was never being called. This was downright comical!&lt;/p&gt;


	&lt;p&gt;I asked if the tests would still pass if we deleted the code and after a short examination of the tests, the developers weren&amp;#8217;t as confident that the test parameters actually hit that area of code. There was a log() statement in that code and one of the developers was going to examine the production logs to see if the code ever executed.&lt;/p&gt;


	&lt;p&gt;So there you have it, refactor your code and the bad parts just go away!&lt;/p&gt;</description>
      <pubDate>Wed, 31 Dec 2008 21:01:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:00a7eddd-61a2-4564-88f1-666dc29cc3f3</guid>
      <author>Bob Koss</author>
      <link>http://blog.objectmentor.com/articles/2008/12/31/refactoring-finds-dead-code</link>
      <category>Young Bob's Rants</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>It's all in how you approach it</title>
      <description>&lt;p&gt;I was painting a bedroom over the last week. Unfortunately, it was well populated with furniture, a wall-mounted TV that needed lowering, clutter, the usual stuff. Given the time I had available, I didn&amp;#8217;t think I&amp;#8217;d be able to finish the whole bedroom before having to travel again.&lt;/p&gt;


	&lt;p&gt;I decided to tackle the wall with the wall-mounted TV first, so I moved the furniture to make enough room, taped just that wall (but not the ceiling since I was planning on painting it) and then proceeded to apply two coats of primer and two coats of the real paint. I subsequently moved around to an alcove and another wall and the part of the ceiling I could reach without having to rent scaffolding.&lt;/p&gt;


	&lt;p&gt;I managed to get two walls done and everything moved back into place before I left for another business trip. My wife is happy because the bedroom looks better. I did no damage and made noticeable progress. I still have some &lt;b&gt;P&lt;/b&gt;ainting to do (the capital P is to indicate it will be a Pain). I eventually have to move the bed, rent scaffolding, and so on. That&amp;#8217;s probably more in the future than I&amp;#8217;d prefer, but I&amp;#8217;ll do it when I know I have the time and space to do it.&lt;/p&gt;


	&lt;p&gt;Contrast this to when we bough the house back in March. I entered an empty house. I managed to get two bedrooms painted (ceilings included) and the &amp;#8220;grand&amp;#8221; room with 14&amp;#8217; vaulted ceilings. I only nearly killed myself once &amp;#8211; don&amp;#8217;t lean a ladder against a wall but put the legs on plastic &amp;#8211; and it was much easier to move around. I had a clean slate.&lt;/p&gt;


	&lt;p&gt;Sometimes you&amp;#8217;ve got to get done what you can get done to make some progress. When I was younger, my desire to finish the entire bedroom might have stopped me from making any progress. Sure, the bedroom is now half old paint and half new paint, but according to my wife it looks better &amp;#8211; and she&amp;#8217;s the product owner! I can probably do one more wall without having to do major lifting and when I&amp;#8217;m finally ready to rent the scaffolding, I won&amp;#8217;t have as much to do. I can break down the bed, rent the scaffolding and then in one day I might be able to finish the remainder of the work. (Well probably 2 days because I&amp;#8217;ll end up wanting to apply 2 coats to the ceiling and I&amp;#8217;ll need to wait 8 hours).&lt;/p&gt;


	&lt;p&gt;Painting is just like software development.&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jul 2008 13:15:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:54fa00c2-ce8f-489f-9937-74ee4e508156</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2008/07/21/its-all-in-how-you-approach-it</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>refactoring</category>
      <category>greenvield</category>
      <category>versus</category>
      <category>legacy</category>
    </item>
    <item>
      <title>Top Refactorings</title>
      <description>&lt;p&gt;This has been done before by another mentor, and it was fun.  I&amp;#8217;ve been watching my use of refactoring tools, and here is my top five list of seven most-used refactorings:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;Rename&lt;/li&gt;
&lt;li&gt;Introduce Variable&lt;/li&gt;
&lt;li&gt;Extract Method&lt;/li&gt;
&lt;li&gt;Inline variable&lt;/li&gt;
&lt;li&gt;Inline method&lt;/li&gt;
&lt;li&gt;Move Method&lt;/li&gt;
&lt;li&gt;Change method signature&lt;/li&gt;
&lt;/ol&gt;

	&lt;p&gt;You might guess that I&amp;#8217;m doing a fair amount of refactoring on legacy code.&lt;/p&gt;


	&lt;p&gt;I was suprised how much I&amp;#8217;m using introduce/inline variable, but quite often I&amp;#8217;m doing that to make a block of code ready for method extraction.  Having broken out small methods, I sometimes find a larger method to extract, and then I inline the use of smaller methods. It&amp;#8217;s sometimes hard to find the right level of generality.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;d have trouble imagining that the first three weren&amp;#8217;t everyone&amp;#8217;s favorite refactorings.&lt;/p&gt;</description>
      <pubDate>Thu, 26 Jun 2008 01:10:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1b5f9a03-bd7d-49be-a66d-89622e7f60ed</guid>
      <author>tottinger</author>
      <link>http://blog.objectmentor.com/articles/2008/06/26/top-refactorings</link>
      <category>Tim's Tepid Torrent</category>
      <category>refactoring</category>
      <category>tool</category>
      <category>rename</category>
      <category>inline</category>
      <category>move</category>
      <category>list</category>
      <category>choices</category>
    </item>
    <item>
      <title>Discipline often directed at the symptom, not the cause</title>
      <description>Have you ever heard something like?
&lt;blockquote&gt;
If the developer just had a little discipline and did it the right way, we would not have this problem.
&lt;/blockquote&gt;

	&lt;p&gt;That&amp;#8217;s often a sign that the way something is getting done is hard to do, not supported well or just plain works against against you.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s an example I recently came across&amp;#8230;&lt;/p&gt;


In a particular organization, part of their build system produces &amp;#8220;project&amp;#8221; information to allow for development on multiple platforms and with different C++ compilers. That&amp;#8217;s excellent, it sounds like a great tool. For a developer to get started, he/she:
	&lt;ul&gt;
	&lt;li&gt;Checks out the source tree&lt;/li&gt;
		&lt;li&gt;Runs the script&lt;/li&gt;
		&lt;li&gt;Starts working&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So far, everything is great and this part of their build system is essential to their environment &amp;#8211; and good in general.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s the next part&amp;#8230;&lt;/p&gt;


To add a file to the system you have to:
	&lt;ul&gt;
	&lt;li&gt;Create the file&lt;/li&gt;
		&lt;li&gt;Update project information&lt;/li&gt;
		&lt;li&gt;Rerun the script to regenerate project information.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;When I asked how long it takes to add a class, I was told about 5 minutes. So if I want to add a header file and source file, it takes 5 minutes. That&amp;#8217;s a big problem. Why?&lt;/p&gt;


	&lt;p&gt;After this discussion, I heard one of the senior people lamenting that a developer had put another class in an existing file rather than creating a second file. He said something like &amp;#8220;If the developer just had discipline, he&amp;#8217;d to the right thing.&amp;#8221; Those darn developers.&lt;/p&gt;


	&lt;p&gt;It takes 5 minutes to add a few files to a build. That does not include build time. That&amp;#8217;s just the time to configure the build information.&lt;/p&gt;


	&lt;p&gt;Does 5 minutes seem like very much time?&lt;/p&gt;


Here are a few more thins I noticed(before I knew about the build system):
	&lt;ul&gt;
	&lt;li&gt;Some header files defined multiple classes&lt;/li&gt;
		&lt;li&gt;Some source files had the methods for those multiple classes&lt;/li&gt;
		&lt;li&gt;Some of the header files had names that did not match any of the classes defined in that header file&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So is this a problem?&lt;/p&gt;


Here&amp;#8217;s an important rule from Jerry Weinberg:
&lt;blockquote&gt;
Nothing + Nothing + Nothing eventually equals something
&lt;/blockquote&gt;

	&lt;p&gt;5 minutes may not seem like a lot of time, and if it were isolated, then it&amp;#8217;s probably not a problem. On the other hand, when you multiply it by a team and time, you end up with big problems.&lt;/p&gt;


	&lt;p&gt;Imagine, you need to use class X. Its header file is actually named Q.h and by the way, classes &lt;span class="caps"&gt;T U&lt;/span&gt; and L are defined in that file &amp;#8211; none of which you want to know about.&lt;/p&gt;


	&lt;p&gt;So your class now has phantom dependencies on T, U and L. Also, how did you find the right header file? A quick search (time wasted). Someone changed U, so you end up having to recompile even though you don&amp;#8217;t care about nor use (wasted time). I&amp;#8217;m sure you can come up with a few things on your own.&lt;/p&gt;


	&lt;p&gt;So what do you do about it?&lt;/p&gt;


OK, first, do not throw the baby out with the bathwater. The original tool solved an important problem. But the first rule of problem solving, again from Jerry Weinberg:
&lt;blockquote&gt;
Every solution introduces problems
&lt;/blockquote&gt;

The problems include (but are not limited to):
	&lt;ul&gt;
	&lt;li&gt;Time wasting adding files&lt;/li&gt;
		&lt;li&gt;It requires discipline to add new files, so it doesn&amp;#8217;t always happen&lt;/li&gt;
		&lt;li&gt;A name is wrong, but it&amp;#8217;s a pain to update the build configuration, so it doesn&amp;#8217;t happen &amp;#8211; not all the time, just every so often&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Little by little, things get a bit more chaotic.&lt;/p&gt;


	&lt;p&gt;So now that we&amp;#8217;ve observed a problem &amp;#8211; some waste, we need to find a way to remove the need to update the build information and regenerate to even work.&lt;/p&gt;


I don&amp;#8217;t know what&amp;#8217;s going to happen with this group. They are hoping to perform some refactorings. Their system has quite a bit of coupling. One thing we can do to reduce coupling is:
	&lt;ul&gt;
	&lt;li&gt;Introduce interfaces&lt;/li&gt;
		&lt;li&gt;Inversion of Control&lt;/li&gt;
		&lt;li&gt;Identify seams and use some of Feather&amp;#8217;s stuff to introduce them&lt;/li&gt;
		&lt;li&gt;Etc., the usual stuff to introduce seams and break dependencies.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;But many of the refactorings they&amp;#8217;ll want to use will involve creating new classes. Since that takes a little bit longer, it will slow everything down &amp;#8211; or seem so daunting, it might not happen at all.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s a personal example. A few years ago, I built the security system for one applications and then a suite of applications with single sign on. When I initially introduced the security system, many people wrote tests that would forget to log out, causing problems in both the real system and the simulator.&lt;/p&gt;


	&lt;p&gt;I kept grumbling. I though, &amp;#8220;if people would just do it right, there wouldn&amp;#8217;t be a problem.&amp;#8221; If they just had a little discipline.&lt;/p&gt;


Independent of whose fault it was, it ended up being my problem &amp;#8211; and, quite frankly, it was my fault as well. The solution was actually pretty easy:
	&lt;ul&gt;
	&lt;li&gt;Create an abstract test base&lt;/li&gt;
		&lt;li&gt;Change tests to use it&lt;/li&gt;
		&lt;li&gt;In the setup, the base logged in&lt;/li&gt;
		&lt;li&gt;In the teardown, the base logged out&lt;/li&gt;
	&lt;/ul&gt;


You might think, &amp;#8220;Duh!&amp;#8221; and in retrospect, so did I. But that few minutes of effort:
	&lt;ul&gt;
	&lt;li&gt;Reduced code duplication&lt;/li&gt;
		&lt;li&gt;Increased the stability of the tests&lt;/li&gt;
		&lt;li&gt;Made it hard for people to mess up (so long as they used the correct test base &amp;#8211; and I updated all of the tests that needed it, so going forward, people had correct examples upon which to base their work).&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Ultimately, this removed a lot of my wasted time&lt;/p&gt;


	&lt;p&gt;Detecting waste is the first thing. Until you know it is a problem, you cannot do anything about it.&lt;/p&gt;


So, the next time you think something like:
	&lt;ul&gt;
	&lt;li&gt;If that person was only following &amp;#8220;the rules&amp;#8221; &lt;/li&gt;
		&lt;li&gt;If he/she just had a little more discipline&lt;/li&gt;
		&lt;li&gt;Stupid user, they should not have done that&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Ask yourself if it&amp;#8217;s possible that those statements are directed at the symptom, not the problem.&lt;/p&gt;</description>
      <pubDate>Sat, 01 Mar 2008 16:18:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:83bf0f26-2d34-4f54-bc32-b4487c1224ac</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2008/03/01/discipline-often-directed-at-the-symptom-not-the-cause</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>lean</category>
      <category>waste</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>On Being Stupid</title>
      <description>&lt;blockquote&gt;
This was posted originally to a mailing list, but is reproduced here essentially unchanged by request of a friend. 
&lt;/blockquote&gt;

	&lt;p&gt;I frequently see code (written by others) that is completely
double-spaced, heavily commented, loaded with many abbreviated or meaningless variable names, and hundreds of lines long.
In order to read the function and understand what it&amp;#8217;s doing, poor Tim
must wear out a mouse, skip comments, and track the variables on
paper.  A &amp;#8220;smarter&amp;#8221; programmer could just load it into his head, I
suppose, but not the simpleton who writes this email.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m not smart enough to just read it from top to bottom and
understand it. Sadly, when I read through and understand what in the
heck the thxIniFvt variable is doing, I will forget it by the time I
figure out the purpose(es) of pszVbt.  I can spend all day, or even a
few days to figure out a method, and that&amp;#8217;s an admission of
feeble-mindedness to be sure.  I guess I&amp;#8217;m not up to the level of some
of the rapid hackers. That&amp;#8217;s a limitation I face most days.&lt;/p&gt;


	&lt;p&gt;I find that I can sometimes understand a method like that only  if I just delete all the blank lines and comments first, then reformat to break lines, then inline all methods with seven or more parameters, and then start renaming variables, extracting explanatory variables, and
extracting explanatory methods. I may have to break the method into
classes even.   I guess I&amp;#8217;m not one of the smart kids.&lt;/p&gt;


	&lt;p&gt;I used to be one of the smart kids.  I once built a module so complex and fragile that nobody but me could figure out what to do with it.  It was all tables and indicators, and stunningly clever.  I am so ashamed that I wrote it. It was such a mistake that they eventually disabled it rather than field it in such a state. That was years ago, but so memorable to me. Other programmers said it was like the inside of a swiss watch, all
delicate and perfectly balanced, and scary to mess with unless you first knew exactly what each part was doing, and why.&lt;/p&gt;


	&lt;p&gt;I would like to be faster than I am both mentally and in the sense
of quickly producing code. I&amp;#8217;d like to be a little less intimidated at
the start of a project. .But I would not want those things if it meant
building crap that people who are not appreciably more talented than
myself would trip over every day.  Instead, I sometimes wish I could
teach the really fast, smart kids how to dumb down the code for the
rest of us morons to read.&lt;/p&gt;


	&lt;p&gt;The funny thing is that dumbing code to my level doesn&amp;#8217;t make it
harder for the smart kids to use it, and sometimes allows a compiler
to do a better job with it.  I guess stupid isn&amp;#8217;t so stupid after all.&lt;/p&gt;</description>
      <pubDate>Mon, 10 Sep 2007 10:16:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:0b06c5f0-9426-40f9-add4-95c9b20a56ac</guid>
      <author>tottinger</author>
      <link>http://blog.objectmentor.com/articles/2007/09/10/on-being-stupid</link>
      <category>Tim's Tepid Torrent</category>
      <category>naming</category>
      <category>dumb</category>
      <category>moron</category>
      <category>clever</category>
      <category>refactoring</category>
      <category>comments</category>
      <category>coding</category>
      <category>Fast</category>
      <category>whitespace</category>
      <category>dumbing</category>
      <category>down</category>
      <category>code</category>
    </item>
  </channel>
</rss>

