In Refactoring Legacy Code Michael Feathers describes the Sprout Method as a way for adding new functionality to a system in a way that is easy to test.

For example say we want to get the host name and port number of the machine from which our Silverlight application was served so we can connect back to it using our Restful API (something I needed to do yesterday).

We could just drop that logic do do that into the method where it’s needed:

        private void PostComplexClick(object sender, RoutedEventArgs e)
        {
            ClearTextBoxes();

            string url = Application.Current.Host.Source.AbsoluteUri + "/InvoiceService.svc" + "/" + "invoices";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/xml;charset=UTF-8"
            request.BeginGetRequestStream(PostComplexRequestReady, request);

        }

But that would be de-testable (pun intended). We can’t get our hands on the new code we want to test.

Instead, we can add what Michael calls a Sprout method and using the powers of OO put it into an abstraction we can test.

        [TestMethod]
        public void Should_be_able_to_get_machine_name_port_number_locally()
        {
            Assert.AreEqual(@"http://localhost:49657", Url.ServerRootParser());
        }

With implementation like this:

    public class Url
    {
        public static string ServerRoot()
        {
            return ServerRootParser(Application.Current.Host.Source.AbsoluteUri);
        }
     }

Now this is fine. Until we realize that our unit tests could be serviced from a different project than our application. In other words that hard coding of the port numbers would be very fragile.

Instead we can modify our tests so that they use some dummy data, and have the actual implementation do the work deeper in the object where it will work for all cases.

So tests and implementation then look something like this:

        [TestMethod]
        public void Should_be_able_to_get_machine_name_port_number_locally()
        {
            string documentUrl = @"http://localhost:49657/ClientBin/SilverlightRestExample.xap";
            Assert.AreEqual(@"http://localhost:49657", Url.ServerRootParser(documentUrl));
        }
    public class Url
    {
    
        public static string ServerRoot()
        {
            return ServerRootParser(Application.Current.Host.Source.AbsoluteUri);
        }

        public static class InvoiceService
        {
            public static readonly string Base = ServerRoot() + "/InvoiceService.svc";
            public const string Qualifier = "invoices";

            public static readonly string Get = Base + "/" + Qualifier + "/";
            public static readonly string Post = Base + "/" + Qualifier;
        }

        public static string ServerRootParser(string documentUrl)
        {
            int ptr = documentUrl.IndexOf("ClientBin");
            return documentUrl.Substring(0, ptr -1); // http://<machine>:<port>
        }
    }

Now we can test our ServerRootParser with whatever data we want, and no have to worry about the actual runtime result of

Application.Current.Host.Source.AbsoluteUri

I feel clumsy describing this (Michael does a much better job and I highly recommend his book). But just understanding that if you have some code that is hard test, that you can always create a new method (Sprout Method) or a new class (Sprout Class) is the point of this post.