DaedTech

Stories about Software

By

TDD and CodeRush

TDD as a Practice

The essence of Test Driven Development (TDD) can be summarized most succinctly as “red, green, refactor”. Following this practice will tend to make your code more reliable, cleaner, and better designed. It is no magic bullet, but you doing TDD is better at programming than you not doing it. However, a significant barrier to adoption of the practice is the (justifiable) perception that you will go slower when you start doing it. This is true in the same way that sloppy musicians get relatively proficient being sloppy, but if they really want to excel, they have to slow down, perfect their technique, and gradually speed things back up.

My point here is not to proselytize for TDD. I’m going to make the a priori assumption that testing is better than not testing and TDD is better than not TDD and leave it at that. My point here is that making TDD faster and easier would grease the skids for its broader adoption and continue practice.

My Process with MS Test

I’ve been unit testing for a long time, testing with TDD for a while, and also using CodeRush for a while. CodeRush automates all sorts of refactorings and generally makes development in Visual Studio quicker and more keyboard driven. As much as I love it and stump for it, though, I had never gotten around to using its test runner until recently.

I saw the little test tube icons next to my tests and figured, “why bother with that when I can just hit Ctrl-R, T to execute my MS Test tests in scope?” But a couple of weeks ago, I tried it on a lark, and I decided to give it a fair shake. I’m very glad that I did. The differences are subtle, but powerful, and I find myself a more efficient TDD practitioner for it.

Here is a screenshot of what I would do with the Visual Studio test runner:

I would write a unit test, and hit Ctrl-R, T, and the Visual Studio test results window would pop up at the bottom of my screen (I auto-hide almost everything because I like a lot of real estate for looking at code). For the first run, the screenshot there would be replaced with a single failing test. Then, I would note the failure, open the class and make changes, switch back to the unit test file, and run all of the tests in the class, producing the screenshot above. When I saw that this had worked, I would go back to the class and look either to refactor or for another failing test to add for fleshing out my class further.

So, to recap, write a test, Ctrl-R, T, pop-up window, dismiss window, switch to another file, make changes, switch back, Ctrl-R, T, pop-up window, dismiss pop-up window. I’m very good at this in a way that one tends to be good at things through large amounts of practice, so it didn’t seem inefficient.

If you look at the test results window too, it’s often awkward to find your tests if you run more than just a few. They appear in no particular order, so seeing particular ones involves sorting by one of the columns in the class. There tends to be a lot of noise this way.

Speeding It Up With CodeRush

Now, I have a new process, made possible by the way CodeRush shows test pass/fail:

Notice the little green checks on the left. In my new process, I write a test, hit Ctrl-T, R, and note the failure. I then switch to the other class and make it pass, at which time, I hit Ctrl-T, L (which I have bound to “repeat last test run”) before going back to the test class. As that runs, I switch to the test class and see that my test now passed (I can also easily run all tests in the file if I choose). Now, it’s time to write my next test.

So, to recap here, it’s write a test, Ctrl-T, R, switch to another file, make changes, Ctrl-T, L, switch back. I’ve completely eliminated dealing with a pop-up window and created a situation where my tests are running as I’m switching between windows (multi-tasking at its finest).

In terms of observing results, this has a distinct advantage as well. Instead of the results viewer, I can see green next to the actual code. That’s pretty powerful because it says “this code is good” rather than “this entry is good, and if you double click it, you can see which code is good”. And, if you want to see a broader view than the tests, you can see green next to the class and the namespace. Or, you can launch the CodeRush test runner and see a hierarchical view that you can drill into, rather than a list that you can awkwardly sort or filter.

Does This Really Matter?

Is shaving off a this small an amount of time worth it? I think so. I run tests dozens, if not hundreds of times per day. And, as any good programmer knows, shaving time off of your most commonly executed tasks is at the heart of optimizing a process. And, if we can shave time off of a process that people, for some reason, view as a luxury rather than a mandate, perhaps we can remove a barrier to adopting what should be considered a best practice — heck, a prerequisite for being considered a professional, as Uncle Bob would tell you.

By

“Classworthiness” and Poor Cohesion

Finding Seams in Code

The other day, someone asked me about how to “get at” certain things that he wanted to unit test in a class that he was writing. That is, he had some code that he wanted to extract from a public method and test, but his only solution was to expose the new helper method as public in order to do this. My response to this strategy is that it doesn’t make sense to alter the public interface of your class to accommodate testing. Your class has some ‘natural’ set of inputs and outputs, and those are what you want to test – elevating visibility, reflection, and other trickery is not the way to test the class, since that’s not how it will be used, eventually.

When asked what I would do instead, I replied that for simple helper methods, I just let the tests of their callers stand. For more complex ones, I pull functionality out into a new class and give the old class a private reference to the new class. This is where I got an interesting response — he didn’t think that there was “enough” to justify a new class.

I asked what constituted “enough”, and prefaced the question by saying that his was a common and understandable sentiment. Over the course of time, I’ve grown used to people having certain heuristics for when code is “classworthy”. Perhaps if it’s somewhere between 3 methods and 20 methods. Or, maybe it’s a matter of lines of code — 50 lines is too little, but 1000 is too many. With enough of these heuristics in place, we could say that all methods should be 20 lines and all classes should be 10 methods and therefore 200 lines.

His response was what people’s response always seems to be (paraphrased): I can’t describe “classworthiness”, but I know it when I see it, and this isn’t it. I countered by offering a different kind of heuristic for “classworthiness”, but I’ll get to that later.

Cleaving Classes in Two

The next day, I found myself looking at the following code structure:

public class MyClass : IDoSomething, IDoSomethingUnrelated
{
    #region IDoSoemthingStuff
    //IDoSomething fields
    //IDoSomething properties
    //IDoSomething methods

    #endregion

    #region IDoSomethingUnrelated
    //IDoSomethingUnrelated fields
    //IDoSomethingUnrelated properties
    //IDoSomethingUnrelated methods

    #endregion
}

What we had here was two classes inexplicably crammed into a single class. But, that wasn’t what was concerning me (though I wasn’t thrilled about it). My task was to add the IDoSomethingUnrelated interface implementation to the following:

public class MySecondClass : IDoSomething
{
    #region IDoSoemthingStuff
    //IDoSomething fields
    //IDoSomething properties
    //IDoSomething methods

    #endregion
}

My first thought was the same thing that I hope yours is. Since there were two completely separate concerns in the first class, I would simply extract one of them to its own class and take it from there. Both classes still had to implement both interfaces (this was a non-negotiable constraint imposed on me), but at least I wouldn’t need to duplicate code, and I could make the internals of the class cleaner. The Boy Scout Rule for clean coding is an excellent one to follow.

However, there was a snag. Over the course of time, a handful of the fields had bled between the two classes masquerading as one. Not enough to give you the sense that it was anything but an accident and certainly not enough to make the concerns even remotely cohesive. It appeared to have happened by accident – expediency, one flag serving two purposes, laziness, that kind of thing.

As I was scowling at this and realizing that my exorcism of the possessed class was going to be more involved than I thought, a relationship between this task and my earlier conversation struck me.

A Root Cause of Poor Cohesion

So, how did this state of affairs come to be? Surely nobody looked at this code and thought, as they added the most recent handful of lines, “this is clean and awesome — go me!” Instead, somebody probably thought, “yuck, this is a pretty big class, but I’ll hold my nose and pile on just a touch” (the class was in the 1500 line neighborhood). Was I the first boy scout to get here and say “enough!” Apparently, I was, or it wouldn’t have looked like that.

But, surely, someone else must have considered it. My guess is that they considered extracting a class, but that they decided against it when they found, as I had, that the mess wasn’t quite as easy to detangle as it appeared. This class had become just cohesive enough that refactoring was a pain, but not cohesive enough to be considered anything other than “almost completely lacking cohesion”.

It wasn’t always like that. At some point, this thing was easily separable (if at no other time, at least when it was mashed together initially). But, between then and now, regioning was created, and fields/flags were carelessly and haphazardly re-appropriated depending on context. So, the real question is why someone didn’t see this as two separate classes right from the get-go. I traced the history of it back some and saw that when the two-classes-as-one paradigm was introduced, the class was relatively small in size compared to the rest of the code base. It has since grown steadily.

To think of it another way, when the class was first conceived, neither of its main responsibility was “classworthy” on its own. Not enough methods in the interfaces, or not enough lines of code, or something. Better to reduce the class footprint and combine them, and we can always separate them when the chimera gets too big. Of course, the best laid plans of mice and men… the person who thought that was probably not the same person that started introducing unnecessary cross-coupling, and neither of those people was me, looking at this class thinking that it should be two classes, and realizing that performing the separation surgery wasn’t going to be as easy as one might think.

Getting “Classworthy” Wrong

To return to my conversation about when to break out a new class, I responded that, to me, “classworthiness” had nothing to do with the number of methods, properties or fields, nothing to do with too many or too few lines of code, and nothing to do with how many classes exist in the solution at the moment. These are all red herrings as far as the decision as to what a class should be. A concept is “classworthy” to me if it encapsulates behavior and (usually) state, does one thing and does that thing well, and has only one reason to change (see Single Responsibility Princple).

If you read about the SRP, you’ll notice that there’s nothing in there about how many lines of code or how many methods are in the class. Don’t get me wrong — I’m very leery of classes that are 500+ lines of code and have 20+ methods, but this is a symptom rather than the illness. Those classes are (usually) bad not because of any number, but because something with that much code and that many methods generally isn’t focused and cohesive — it’s generally not doing one thing and doing it well. It’s a Swiss Army Knife Class.


(Image from Derick Bailey at Los Techies).

I’ve never really understood the reluctance to create a new class. It’s as if they were a limited resource like oil, and people want to conserve them. This isn’t limited to the person that I was talking to — it’s fairly pervasive, in my experience. I do understand that you can take my sentiment to an absurd extreme and create a class for every line of code or some such thing (though this would result in different metrics problems by raising coupling), but that isn’t what I’m talking about. I’m talking about reluctance to factor out a class because it will only have 70 lines of code or it will only have two methods.

If you find yourself thinking this way, pull back for a minute and ask yourself whether the code you’re considering pulling out is a separate behavior or not. If you have a class that receives data transfer objects from the GUI and validates them, the “and” tells you that you are, in fact, doing two separate things. Perhaps the validation logic just checks two fields to see if they’re empty or not. Is it really worth a separate class for that?

Yes! Why not decouple validation from staging? You’re not going to deplete the world’s class reserves with that one class creation. But, what you will get is a scheme where the validation of your object is decoupled from the staging of that object and where, if one of those things changes, the other will not be affected. And, to tie this all back together, you also get a nice, new seam for unit testing.

By

Setting Up Spring MVC 3.0

Why Spring MVC?

It’s been a while since I’ve done a lot with Java. I’ve been writing an Android app and see and interact with just enough Java not to forget what it looks like, but for the last couple of years, I’ve mainly worked in .NET with C#. Today, I started on actual development of my home automation server in earnest (will be added to github shortly). One of the main design goals of this home automation effort is to support affordable solutions and, toward that end, I am designing it to run on bare bones Linux machines, thus allowing old computers to be re-appropriated to run it.

This is the driving force in my choice of implementation tools. It needs to be runnable on Linux and Windows, and to have a small footprint. But, it also needs to support a true object oriented design paradigm and rich server side functionality. So, I will be dusting off my J2EE and using Spring MVC and Java for the server itself.

Setting up Spring MVC 3.0

I’ve been spoiled by developing principally in .NET over the last couple of years. In that world, any kind of project is usually a Visual Studio install and a plugin or NuGet package away. In the open source world of Spring and Java, it’s not quite as straightforward. My first step was, of course, a hello world app. I have plenty of Spring MVC/J2EE experience, but I was last developing with Spring when it was version 1.x, and we’re a few years removed and on 3.1, so I’m basically starting all over.

I already had Eclipse and Tomcat installed, and I set about finding an Eclipse plugin for creating a sample spring project or a tutorial on the same. I didn’t really find either. The most helpful thing I found, by far, was this blog post. If you take steps to satisfy the preconditions listed and follow the blog itself, you’ll be most of the way there.

I had to take two additional steps to get my new Spring “Hello World” project up and running. I had to get commons-logging.jar from the spring framework that I had downloaded and put it into my little app’s Web-INF\lib folder. I then had to do the same with jstl.jar from my Tomcat installation. Only after doing that was Hello World up and running.

Hopefully, this saves someone reading some time.

By

Command

Quick Information/Overview

Pattern Type Behavioral
Applicable Language/Framework Agnostic OOP
Pattern Source Gang of Four
Difficulty Easy – Moderate

Up Front Definitions

  1. Invoker: This object services clients by exposing a method that takes a command as a parameter and invoking the command’s execute
  2. Receiver: This is the object upon which commands are performed – its state is mutated by them

The Problem

Let’s say you get a request from management to write an internal tool. A lot of people throughout the organization deal with XML documents and nobody really likes dealing with them, so you’re tasked with writing an XML document builder. The user will be able to type in node names and pick where they go and whatnot. Let’s also assume (since this post is not about the mechanics of XML) that all XML documents consist of a root node called “Root” and only child nodes of root.

The first request that you get in is the aforementioned adding. So, knowing that you’ll be getting more requests, your first design decision is to create a DocumentBuilder class and have the adding implemented there.

/// This class is responsible for doing document build operations
public class DocumentBuilder
{
    /// This is the document that we're doing to be modifying
    private readonly XDocument _document;

    /// Initializes a new instance of the DocumentBuilder class.
    /// 
    public DocumentBuilder(XDocument document = null)
    {
        _document = document ?? new XDocument(new XElement("Root"));
    }

    /// Add a node to the document
    /// 
    public void AddNode(string elementName)
    {
        _document.Root.Add(new XElement(elementName));
    }
}

//Client code:
var myInvoker = new DocumentBuilder();
myInvoker.AddNode("Hoowa!");

So far, so good. Now, a request comes in that you need to be able to do undo and redo on your add operation. Well, that takes a little doing, but after 10 minutes or so, you’ve cranked out the following:

public class DocumentBuilder
{
    /// This is the document that we're doing to be modifying
    private readonly XDocument _document;

    /// Store things here for undo
    private readonly Stack _undoItems = new Stack();

    /// Store things here for redo
    private readonly Stack _redoItems = new Stack();

    /// Initializes a new instance of the DocumentBuilder class.
    /// 
    public DocumentBuilder(XDocument document = null)
    {
        _document = document ?? new XDocument(new XElement("Root"));
    }

    /// Add a node to the document
    /// 
    public void AddNode(string elementName)
    {
        _document.Root.Add(new XElement(elementName));
        _undoItems.Push(elementName);
        _redoItems.Clear();
    }

    /// Undo the previous steps operations
    public void Undo(int steps)
    {
        for (int index = 0; index < steps; index++)
        {
            var myName = _undoItems.Pop();
            _document.Root.Elements(myName).Remove();
            _redoItems.Push(myName);
        }
    }

    /// Redo the number of operations given by steps
    public void Redo(int steps)
    {
        for (int index = 0; index < steps; index++)
        {
            var myName = _redoItems.Pop();
            _document.Root.Add(new XElement(myName));
            _undoItems.Push(myName);
        }
    }
}

Not too shabby - things get popped from each stack and added to the other as you undo/redo, and the redo stack gets cleared when you start a new "branch". So, you're pretty proud of this implementation and you're all geared up for the next set of requests. And, here it comes. Now, the builder must be able to print the current document to the console. Hmm... that gets weird, since printing to the console is not really representable by a string in the stacks. The first thing you think of doing is making string.empty represent a print operation, but that doesn't seem very robust, so you tinker and modify until you have the following:

public class DocumentBuilder
{
    /// This is the document that we're doing to be modifying
    private readonly XDocument _document;

    /// This defines what type of operation that we're doing
    private enum OperationType
    {
        Add,
        Print
    }

    /// Store things here for undo
    private readonly Stack> _undoItems = new Stack>();

    /// Store things here for redo
    private readonly Stack> _redoItems = new Stack>();

    /// Initializes a new instance of the DocumentBuilder class.
    /// 
    public DocumentBuilder(XDocument document = null)
    {
        _document = document ?? new XDocument(new XElement("Root"));
    }

    /// Add a node to the document
    /// 
    public void AddNode(string elementName)
    {
        _document.Root.Add(new XElement(elementName));
        _undoItems.Push(new Tuple(OperationType.Add, elementName));
        _redoItems.Clear();
    }

    /// Print out the document
    public void PrintDocument()
    {
        Print();

        _redoItems.Clear();
        _undoItems.Push(new Tuple(OperationType.Print, string.Empty));
    }

    /// Undo the previous steps operations
    public void Undo(int steps)
    {
        for (int index = 0; index < steps; index++)
        {
            var myOperation = _undoItems.Pop();
            switch (myOperation.Item1)
            {
                case OperationType.Add:
                    _document.Root.Elements(myOperation.Item2).Remove();
                    _redoItems.Push(myOperation);
                    break;
                case OperationType.Print:
                    Console.Out.WriteLine("Sorry, but I really can't undo a print to screen.");
                    _redoItems.Push(myOperation);
                    break;
            }
        }
    }

    /// Redo the number of operations given by steps
    public void Redo(int steps)
    {
        for (int index = 0; index < steps; index++)
        {
            var myOperation = _redoItems.Pop();
            switch (myOperation.Item1)
            {
                case OperationType.Add:
                    _document.Root.Elements(myOperation.Item2).Remove();
                    _undoItems.Push(myOperation);
                    break;
                case OperationType.Print:
                    Print();
                    _undoItems.Push(myOperation);
                    break;
            }
        }
    }

    private void Print()
    {
        var myBuilder = new StringBuilder();
        Console.Out.WriteLine("\nDocument contents:\n");
        using (var myWriter = XmlWriter.Create(myBuilder, new XmlWriterSettings() { Indent = true, IndentChars = "\t" }))
        {
            _document.WriteTo(myWriter);
        }
        Console.WriteLine(myBuilder.ToString());
    }
}

Yikes, that's starting to smell a little. But, hey, you extracted a method for the print, and you're keeping things clean. Besides, you're fairly proud of your little tuple scheme for recording what kind of operation it was in addition to the node name. And, there's really no time for 20/20 hindsight because management loves it. You need to implement something that lets you update a node's name ASAP.

Oh, and by the way, they also want to be able to print the output to a file instead of the console. Oh, and by the by the way, you know what would be just terrific? If you could put something in to switch the position of two nodes in the file. They know it's a lot to ask right now, but you're a rock star and they know you can handle it.

So, you buy some Mountain Dew and pull an all nighter. You watch as the undo and redo case statements grow vertically and as your tuple grows horizontally. The tuple now has an op code and an element name like before, but it has a third argument that means the new name for update, and when the op-code is swap, the second and third arguments are the two nodes to swap. It's ugly (so ugly I'm not even going to code it for the example), but it works.

And, it's a success! Now, the feature requests really start piling up, and not only are stakeholders using your app, but other programmers have started using your API. There's really no time to reflect on the design now - you have a ton of new functionality to implement. And, as you do it, the number of methods in your builder will grow as each new feature is added, the size of the case statements in undo and redo will grow with each new feature is added, and the logic for parsing your swiss-army knife tuple is going to get more and more convoluted.

By the time this thing is feature complete, it's going to take a 45 page developer document to figure out what on Earth is going on. Time to start putting out resumes and jump off this sinking ship.

So, What to Do?

Before discussing what to do, let's first consider what went wrong. There are two main issues here that have contributed to the code rot. The first and most obvious is the decision to "wing it" with the Tuple solution that is, in effect, a poor man's type. Instead of a poor man's type, why not an actual type? The second issue is a bit more subtle, but equally important -- violation of the open/closed principle.

To elaborate, consider the original builder that simply added nodes to the XDocument and the subsequent change to implement undo and redo of this operation. By itself, this was fine and cohesive. But, when the requirements started to come in about more operations, this was the time to go in a different design direction. This may not be immediately obvious, but a good question to ask during development is "what happens if I get more requests like this?" When the class had "AddNode", "Undo" and "Redo", and the request for "PrintDocument" came in, it was worth noting that you were cobbling onto an existing class. It also would have been reasonable to ask, "what if I'm asked to add more operations?"

Asking this question would have resulted in the up-front realization that each new operation would require another method to be added to the class, and another case statement to be added to two existing methods. This is not a good design -- especially if you know more such requests are coming. Having an implementation where new the procedure for accommodating new functionality is "tack another method onto class X" and/or "open method X and add more code" is a recipe for code rot.

So, let's consider what we could have done when the request for document print functionality. Instead of this tuple thing, let's create another implementation. What we're going to do is forget about creating Tuples and forget about the stacks of string, and think in terms of a command object. Now, at the moment, we only have one command object, but we know that we've got a requirement that's going to call for a second one, so let's make it polymorphic. I'm going to introduce the following interface:

public interface IDocumentCommand
{
    /// Document (receiver) upon which to operate
    XDocument Document { get; set; }

    /// Execute the command
    void Execute();
        
    /// Revert the execution of the command
    void UndoExecute();

}

This is what will become the command in the command pattern. Notice that the interface defines two conceptual methods - execution and negation of the execution (which should look a lot like "do" and "undo"), and it's also going to be given the document upon which to do its dirty work.

Now, let's take a look at the add implementer:

public class AddNodeCommand : IDocumentCommand
{
    private readonly string _nodeName;

    private XDocument _document = new XDocument();
    public XDocument Document { get { return _document; } set { _document = value ?? _document; } }

    /// Initializes a new instance of the AddNodeCommand class.
    /// Note the extra parameter here -- this is important.  This class essentially conceptually
    /// an action, so you're more used to seeing things in method form like this.  We pass in the "method" parameters
    /// to the constructor because we're encapsulating an action as an object with state
    public AddNodeCommand(string nodeName)
    {
        _nodeName = nodeName ?? string.Empty;
    }
    public void Execute()
    {
        Document.Root.Add(new XElement(_nodeName));
    }

    public void UndoExecute()
    {
        Document.Root.Elements(_nodeName).Remove();
    }
}

Pretty straightforward (in fact a little too straightforward - in a real implementation, there should be some error checking about the state of the document). When created, this object is seeded with the name of the node that it's supposed to create. The document is a setter dependency, and the two operations mutate the XDocument, which is our "receiver" in the command pattern, according to the pattern's specification.

Let's have a look at what our new Builder implementation now looks like before adding print document:

public class DocumentBuilder
{
    /// This is the document that we're doing to be modifying
    private readonly XDocument _document;

    /// Store things here for undo
    private readonly Stack _undoItems = new Stack();

    /// Store things here for redo
    private readonly Stack _redoItems = new Stack();

    /// Initializes a new instance of the DocumentBuilder class.
    /// 
    public DocumentBuilder(XDocument document = null)
    {
        _document = document ?? new XDocument(new XElement("Root"));
    }

    /// Add a node to the document
    /// 
    public void AddNode(string elementName)
    {
        var myCommand = new AddNodeCommand(elementName)  { Document = _document };
        myCommand.Execute();
        _undoItems.Push(myCommand);
        _redoItems.Clear();
    }

    /// Undo the previous steps operations
    public void Undo(int steps)
    {
        for (int index = 0; index < steps; index++)
        {
            var myCommand = _undoItems.Pop();
            myCommand.UndoExecute();
            _redoItems.Push(myCommand);
        }
    }

    /// Redo the number of operations given by steps
    public void Redo(int steps)
    {
        for (int index = 0; index < steps; index++)
        {
            var myCommand = _redoItems.Pop();
            myCommand.Execute();
            _undoItems.Push(myCommand);
        }
    }
}

Notice that the changes to this class are subtle but interesting. We now have stacks of commands rather than strings (or, later, tuples). Notice that undo and redo now delegate the business of executing the command to the command object, rather than figuring out what kind of operation it is and doing it themselves. This is critical to conforming to the open/closed principle, as we'll see shortly.

Now that we've performed our refactoring, let's add the print document functionality. This is now going to be accomplished by a new implementation of IDocumentCommand:

public class PrintCommand : IDocumentCommand
{
    private XDocument _document = new XDocument();
    public XDocument Document { get { return _document; } set { _document = value ?? _document; } }

    /// Execute the print command
    public void Execute()
    {
        var myBuilder = new StringBuilder();
        Console.Out.WriteLine("\nDocument contents:\n");
        using (var myWriter = XmlWriter.Create(myBuilder, new XmlWriterSettings() { Indent = true, IndentChars = "\t" }))
        {
            Document.WriteTo(myWriter);
        }
        Console.WriteLine(myBuilder.ToString());
    }

    /// Undo the print command (which, you can't)
    public void UndoExecute()
    {
        Console.WriteLine("\nDude, you can't un-ring that bell.\n");
    }
}

Also pretty simple. Let's now take a look at how we implement this in our "invoker", the DocumentBuilder:

public class DocumentBuilder
{
    /// This is the document that we're doing to be modifying
    private readonly XDocument _document;

    /// Store things here for undo
    private readonly Stack _undoItems = new Stack();

    /// Store things here for redo
    private readonly Stack _redoItems = new Stack();

    /// Initializes a new instance of the DocumentBuilder class.
    /// 
    public DocumentBuilder(XDocument document = null)
    {
        _document = document ?? new XDocument(new XElement("Root"));
    }

    /// Add a node to the document
    /// 
    public void AddNode(string elementName)
    {
        var myCommand = new AddNodeCommand(elementName) { Document = _document };
        myCommand.Execute();
        _undoItems.Push(myCommand);
        _redoItems.Clear();
    }

    /// Print the document
    public void PrintDocument()
    {
        var myCommand = new PrintCommand() { Document = _document};
        myCommand.Execute();
        _undoItems.Push(myCommand);
        _redoItems.Clear();
    }

    /// Undo the previous steps operations
    public void Undo(int steps)
    {
        for (int index = 0; index < steps; index++)
        {
            var myCommand = _undoItems.Pop();
            myCommand.UndoExecute();
            _redoItems.Push(myCommand);
        }
    }

    /// Redo the number of operations given by steps
    public void Redo(int steps)
    {
        for (int index = 0; index < steps; index++)
        {
            var myCommand = _redoItems.Pop();
            myCommand.Execute();
            _undoItems.Push(myCommand);
        }
    }
}

Lookin' good! Observe that undo and redo do not change at all. Our invoker now creates a command for each operation, and delegate its work to the receiver on behalf of the client code. As we continue to add more commands, we do not ever have to modify undo and redo.

But, we still don't have it quite right. The fact that we need to add a new class and a new method each time a new command is added is still a violation of the open/closed principle, even though we're better off than before. The whole point of what we're doing here is separating the logic of command execution (and undo/redo and, perhaps later, indicating whether a command can currently be executed or not) from the particulars of the commands themselves. We're mostly there, but not quite - the invoker, DocumentBuilder is still responsible for enumerating the different commands as methods and creating the actual command objects. The invoker is still too tightly coupled to the mechanics of the commands.

This is not hard to fix - pass the buck! Let's look at an implementation where the invoker, instead of creating commands in named methods, just demands the commands:

public class DocumentBuilder
{
    /// This is the document that the user will be dealing with
    private readonly XDocument _document;

    /// This houses commands for undo
    private readonly Stack _undoCommands = new Stack();

    /// This houses commands for redo
    private readonly Stack _redoCommands = new Stack();

    /// User can give us an xdocument or we can create our own
    public DocumentBuilder(XDocument document = null)
    {
        _document = document ?? new XDocument(new XElement("Root"));
    }

    /// Executes the given command
    /// 
    public void Execute(IDocumentCommand command)
    {
        if (command == null) throw new ArgumentNullException("command", "nope");
        command.Document = _document;
        command.Execute();
        _redoCommands.Clear();
        _undoCommands.Push(command);
    }

    /// Perform the number of undos given by iterations
    public void Undo(int iterations)
    {
        for (int index = 0; index < iterations; index++)
        {
            if (_undoCommands.Count > 0)
            {
                var myCommand = _undoCommands.Pop();
                myCommand.UndoExecute();
                _redoCommands.Push(myCommand);
            }
        }
    }

    /// Perform the number of redos given by iterations
    public void Redo(int iterations)
    {
        for (int index = 0; index < iterations; index++)
        {
            if (_redoCommands.Count > 0)
            {
                var myCommand = _redoCommands.Pop();
                myCommand.UndoExecute();
                _undoCommands.Push(myCommand);
            }
        }
    }
}

And, there we go. Observe that now, when new commands are to be added, all a maintenance programmer has to do is author a new class. That's a much better paradigm. Any bugs related to the mechanics of do/undo/redo are completely separate from the commands themselves.

Some might argue that the new invoker/DocumentBuilder lacks expressiveness in its API (having Execute(IDocumentCommand) instead of AddNode(string) and PrintDocument()), but I disagree:

var myInvoker = new DocumentBuilder();
myInvoker.Execute(new AddNodeCommand("Hoowa"));
myInvoker.Execute(new PrintCommand());
myInvoker.Undo(2);
myInvoker.Execute(new PrintCommand());

Execute(AddCommand(nodeName)) seems just as expressive to me as AddNode(nodeName), if slightly more verbose. But even if it's not, the tradeoff is worth it, in my book. You now have the ability to plug new commands in anytime by implementing the interface, and DocumentBuilder conforms to the open/closed principle -- it's only going to change if there is a bug in the way the do/undo/redo logic is found and not when you add new functionality (incidentally, having only one reason to change also makes it conform to the single responsibility principle).

A More Official Explanation

dofactory defines the command pattern this way:

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

The central, defining point of this pattern is the idea that a request or action should be an object. This is an important and not necessarily intuitive realization. The natural tendency would be to implement the kind of ad-hoc logic from the initial implementation, since we tend to think of objects as things like "Car" and "House" rather than concepts like "Add a node to a document".

But, this different thinking leads to the other part of the description - the ability to parameterize clients with different requests. What this means is that since the commands are stored as objects with state, they can encapsulate their own undo and redo, rather than forcing the invoker to do it. The parameterizing is the idea that the invoker operates on passed in command objects rather than doing specific things in response to named methods.

What is gained here is then the ability to put commands into a stack, queue, list, etc, and operate on them without specifically knowing what it is they do. That is a powerful ability since separating and decoupling responsibilities is often hard to accomplish when designing software.

Other Quick Examples

Here are some other places that the command pattern is used:

  1. The ICommand interface in C#/WPF for button click and other GUI events.
  2. Undo/redo operations in GUI applications (i.e. Ctrl-Z, Ctrl-Y).
  3. Implementing transactional logic for persistence (thus providing atomicity for rolling back)

A Good Fit – When to Use

Look to use the command pattern when there is a common set of "meta-operations" surrounding commands. That is, if you find yourself with requirements along the lines of "revert the last action, whatever that action may have been." This is an indicator that there are going to be operations on the commands themselves beyond simple execution. In scenarios like this, it makes sense to have polymorphic command objects that have some notion of state.

Square Peg, Round Hole – When Not to Use

As always, YAGNI applies. For example, if our document builder were only ever going to be responsible for adding nodes, this pattern would have been overkill. Don't go implementing the command pattern on any and all actions that your program may take -- the pattern incurs complexity overhead in the form of multiple objects and a group of polymorphs.

So What? Why is this Better?

As we've seen above, this makes code a lot cleaner in situations where it's relevant and it makes it conform to best practices (SOLID principles). I believe that you'll also find that, if you get comfortable with this pattern, you'll be more inclined to offer richer functionality to clients on actions that they may take.

That is, implementing undo/redo or atomicity may be something you'd resist or avoid as it would entail a great deal of complexity, but once you see that this need not be the case, you might be more willing or even proactive about it.

In, using this pattern where appropriate is better because it provides for cleaner code, fewer maintenance headaches, and more clarity.

By

Ubuntu and Belkin Dongles Revisited

Previously, I posted about odyssey to get belkin wireless dongles working with Ubuntu. Actually, the previous post was tame compared to what I’ve hacked together with these things over the years, including getting them to work on Damn Small Linux where I had to ferret out the text for the entire wpa_supplicant configuration using kernel messages from demsg. But, I digress.

I’m in the middle of creating an ad-hoc “music throughout the house” setup for my home automation, and this involves a client computer in most rooms in the house. Over the years, I’ve accepted donations of computers that range in manufacture date from 1995 to 2008, and these are perfect for my task. Reappropriated and freed from their Windows whatever, they run ably if not spectacularly with XUbunutu (and, in some cases DSL or Slackware when that’s too much for a machine that maxes out at 64 meg of RAM).

So, I have this setup in most rooms, and I just remodeled my basement, which was the last room to get the setup. I had one of these things working with the dongle and everything, but the sound card was this HP Pavilion special that was integrated with a fax card or something, and the sound just wasn’t happening. So, after sort of borking it while trying to configure, I scrapped the effort and reappropriated an old Dell.

Each time I do this, I grab the latest and greatest Ubuntu, and this time was no different. Each time, I check to see if maybe, just maybe, I won’t have to pull the Belkin drivers off of the CD and use ndiswrapper, and lo and behold, this was the breaking point – I finally didn’t.

I wish I could say it worked out of the box, but alas, not quite. I plugged in the dongle and the network manager popped up, and sure enough it was detecting wireless networks, but when I put in all of my credentials, it just kept prompting me for a password. I remembered that Network manager had difficulty with these cards and WPA-PSK security protocol, so I tried another network manager: wicd. Bam! Up and running.

So, for those keeping score at home, if you have Ubuntu 11.10 (Ocelot) and a belkin dongle, all you need to do is:

sudo apt-get install --reinstall wicd
sudo service network-manager stop
sudo apt-get remove --purge network-manager network-manager-gnome
sudo service wicd restart

And, that’s it. You should be twittering and facebooking and whatever else in no time.

Edit:

Since making this post, I set up another machine in this fashion, and realized that I made an important omission. The wicd wireless setup did not just work out of the box with WPA2. I had to modify my /etc/network/interfaces file to look like this:

auto lo
iface lo inet loopback

auto wlan0
iface wlan0 inet static
address {my local IP}
gateway 192.168.2.1
dns-namesevers 192.168.2.1
netmask 255.255.255.0
wpa-driver wext
wpa-ssid {my network SSID}
wpa-ap-scan 2
wpa-proto WPA RSN
wpa-pairwise TKIP CCMP
wpa-group TKIP CCMP
wpa-key-mgmt WPA-PSK
wpa-psk {my encrypted key}

For my network, I use static IPs and this setup was necessary to get that going as well as the encryption protocol. Without this step, the setup I mentioned above does not work out of the box — wicd continuously fails with a “bad password” message. Adding this in fixed it.

Cheers