<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://blog.aabech.no/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Lars-Erik's blog</title>
    <link>https://blog.aabech.no/</link>
    <description>Ramblings about Umbraco, .net and JavaScript development. With a sprinkle of other stuff.</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <item>
      <guid isPermaLink="false">1114</guid>
      <link>https://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco-just-got-simpler/</link>
      <title>The basics of unit testing Umbraco just got simpler</title>
      <description>&lt;h2&gt;Status Quo of Umbraco Testing 2017&lt;/h2&gt;
&lt;p&gt;It's been a year since giving my &lt;a href="https://vimeo.com/channels/codegarden16/183623111"&gt;presentation about Unit Testing Umbraco at Codegarden 2016&lt;/a&gt;. Testing Umbraco is ever more popular, and I'm happy to see there's more people talking about it on twitter, at festivals and at gatherings. Recently, &lt;a href="https://twitter.com/c_thillemann"&gt;Christian Thillemann&lt;/a&gt; decided he was tired of waiting for the request I created last year about having Umbraco.Tests on Nuget. He went ahead and set up an AppVeyor script and we now have automatic releases of &lt;a href="https://www.nuget.org/packages/Our.Umbraco.Community.Tests/"&gt;Our.Umbraco.Community.Tests&lt;/a&gt; on nuget whenever there's a new Umbraco version out. #H5YR, @kedde! :)&lt;/p&gt;
&lt;p&gt;Of course, having this requirement and having to lean that heavily on an external assembly just to get to annoying internals in our core product is just an intermediate challenge. Come Umbraco 9 on .net Core, we'll (hopefully) be able to just spin up a complete hosted Umbraco site from our tests and inject / swap out whatever we'd fancy in that startup. Java and other platforms has had that option for years, so it's about time the .net community gets there. But enough ranting about that, let's just keep contributing so we get closer to running on .net Core.&lt;/p&gt;
&lt;h2&gt;Meanwhile...&lt;/h2&gt;
&lt;p&gt;In the mean time, I've been exploring further practices with automated testing. I'll get back to those in future posts and presentations. But while doing this, I stubled into an alternative practice to the one I've been preaching. It's mostly the same, but it challenges the inherital structure of Umbraco.Tests.&lt;/p&gt;
&lt;p&gt;A really good principle when you write tests is letting your tests be interfaces to your code. Whenever you write a user interface, you take good care putting your logic in separate classes. The UI stuff is just UI stuff. Maybe our tests should be like that too? When you do that, you can even re-use your tests for different layers of your code! I'll not go into details about that in this post, but a natural result was to offload everything about Umbraco into its own class. I don't know why I didn't before, or why it took me so long to do it, but I suppose it's got something to do with closed mentality. When you see a pattern (inherited unit-tests), you automatically think it's a good one, and don't challenge it.&lt;/p&gt;
&lt;h2&gt;An epiphany&lt;/h2&gt;
&lt;p&gt;If you've gone through my post about &lt;a href="/archive/the-basics-of-unit-testing-umbraco/"&gt;the basics of testing Umbraco&lt;/a&gt;, you know you can inherit quite a few different base tests from Umbraco.Tests. They all have their uses, and I still recommend using the &lt;code&gt;BaseDatabaseFactoryTest&lt;/code&gt; if you're testing stuff integrated with a database. However, most of what I write are different &lt;code&gt;Surface-&lt;/code&gt;, &lt;code&gt;Render-&lt;/code&gt; and &lt;code&gt;*ApiControllers&lt;/code&gt; using fairly few stubbable things. All the front-end ones have the same basic initial state needs, and the backoffice ones just have fewer. So we end up inheriting &lt;code&gt;BaseRoutingTest&lt;/code&gt; and calling &lt;code&gt;GetRoutingContext(...)&lt;/code&gt; in all our setups. Being the smart devs you are, I'm sure a lot of you also ended up with some kind of &lt;code&gt;MyUmbracoTestBase&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But there's something completely wrong with that!&lt;/strong&gt; We're letting the dependency on &lt;em&gt;Umbraco&lt;/em&gt; get in the way of our own automation code. We can't create a hirarchy of tests that isn't dependent on Umbraco. For instance, if we had a base class initializating our own domain in our core domain tests, we couldn't re-use that for our MVC tests. To do that we'd have to inherit our core base test class from Umbraco tests, and then Umbraco would leak into our core domain. We don't want that.&lt;/p&gt;
&lt;h2&gt;The solution&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;SRP&lt;/a&gt; your tests of course! Excercise the same dicipline by applying nice layering to your test automation code as you do to your other code. I ended up refactoring most of my test classes' setups and teardowns into a &amp;quot;Support&amp;quot; class with only three public methods. Here's how you'd set up a test for a simple SurfaceController with this method:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[TestFixture]
public class Adding_Numbers
{
    private UmbracoSupport support = new UmbracoSupport();

    [SetUp]
    public void Setup()
    {
        support.SetupUmbraco();
    }

    [TearDown]
    public void TearDown()
    {
        support.DisposeUmbraco();
    }

    [Test]
    public void Posting_AddModel_Calculates_Result()
    {
        const int expectedSum = 3;
        var controller = new SimpleSurfaceController();
        var result = (AdditionModel)controller.Add(1, 2);
        Assert.AreEqual(expectedSum, result.Model.Sum);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;I want to stress that this is not, and will not be a package. See below for reasons.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So it isn't that different from inheriting &lt;code&gt;BaseRoutingTest&lt;/code&gt;. The only difference is that we delegate the setup and teardown to another type instance instead of delegating it to the base class. The &lt;code&gt;UmbracoSupport&lt;/code&gt; class still derives from &lt;code&gt;BaseRoutingTest&lt;/code&gt;, but it stops there. It won't leak further.&lt;/p&gt;
&lt;p&gt;Note that NUnit creates an instance of your test fixture for each test, so you'll get a fresh UmbracoSupport instance for each of your tests.&lt;/p&gt;
&lt;p&gt;The third method you'd want to call is &lt;code&gt;PrepareController()&lt;/code&gt;. The moment you'd like to act upon the &lt;code&gt;CurrentPage&lt;/code&gt;, or just use &lt;code&gt;RedirectToCurrentUmbracoPage&lt;/code&gt;, you'll have to call &lt;code&gt;support.PrepareController(controller)&lt;/code&gt;. This wires up the Umbraco request context instance to your controller's state.&lt;/p&gt;
&lt;p&gt;When you've done that, you've got quite a few properties on the support class that let's you get to the juicy stuff you'll want to stub:&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Property&lt;/th&gt;&lt;th&gt;Purpose&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;UmbracoContext&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The current &lt;code&gt;UmbracoContext&lt;/code&gt;. Inject it to your controllers and whatnot&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;ServiceContext&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The complete &lt;code&gt;IServiceContext&lt;/code&gt; with clean stubs&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;CurrentPage&lt;/code&gt;&lt;/td&gt;&lt;td&gt;A mock of the currently served &lt;code&gt;IPublishedContent&lt;/code&gt;. Same as &lt;code&gt;UmbracoContext.Current.PublishedRequestContext.PublishedContent&lt;/code&gt;, &lt;code&gt;UmbracoHelper.AssignedContentItem&lt;/code&gt; and &lt;code&gt;SurfaceController.CurrentPage&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;RouteData&lt;/code&gt;&lt;/td&gt;&lt;td&gt;You might hit further usage of this internally to Umbraco. It's available for modification when you do&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;HttpContext&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The good ol' "testable" &lt;code&gt;HttpContextBase&lt;/code&gt; from MVC, created along with the &lt;code&gt;UmbracoContext&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;h2&gt;The implementation&lt;/h2&gt;
&lt;p&gt;I've already implemented this class in the &lt;a href="https://github.com/lars-erik/umbraco-unit-testing-samples"&gt;umbraco-unit-testing-samples repo&lt;/a&gt;. I'll post it here as well for you to copy uncritically. I won't go through the details. It's just a refactored version of &lt;a href="http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco/"&gt;what I've shown in previous posts&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;using System;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
using Umbraco.Core;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Umbraco.Web.Routing;

namespace Umb.Testing.Tests.Support
{
    public class UmbracoSupport : BaseRoutingTest
    {
        public UmbracoContext UmbracoContext =&amp;gt; umbracoContext;

        public new ServiceContext ServiceContext =&amp;gt; serviceContext;

        public IPublishedContent CurrentPage =&amp;gt; currentPage;

        public RouteData RouteData =&amp;gt; routeData;

        public UmbracoHelper UmbracoHelper =&amp;gt; umbracoHelper;

        public HttpContextBase HttpContext =&amp;gt; umbracoContext.HttpContext;

        public string ContentCacheXml { get; set; }

        /// &amp;lt;summary&amp;gt;
        /// Initializes a stubbed Umbraco request context. Generally called from [SetUp] methods.
        /// Remember to call UmbracoSupport.DisposeUmbraco from your [TearDown].
        /// &amp;lt;/summary&amp;gt;
        public void SetupUmbraco()
        {
            InitializeFixture();
            TryBaseInitialize();

            InitializeSettings();

            CreateCurrentPage();
            CreateRouteData();
            CreateContexts();
            CreateHelper();

            InitializePublishedContentRequest();
        }

        /// &amp;lt;summary&amp;gt;
        /// Cleans up the stubbed Umbraco request context. Generally called from [TearDown] methods.
        /// Must be called before another UmbracoSupport.SetupUmbraco.
        /// &amp;lt;/summary&amp;gt;
        public void DisposeUmbraco()
        {
            TearDown();
        }

        /// &amp;lt;summary&amp;gt;
        /// Attaches the stubbed UmbracoContext et. al. to the Controller.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name=&amp;quot;controller&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        public void PrepareController(Controller controller)
        {
            var controllerContext = new ControllerContext(HttpContext, RouteData, controller);
            controller.ControllerContext = controllerContext;

            routeData.Values.Add(&amp;quot;controller&amp;quot;, controller.GetType().Name.Replace(&amp;quot;Controller&amp;quot;, &amp;quot;&amp;quot;));
            routeData.Values.Add(&amp;quot;action&amp;quot;, &amp;quot;Dummy&amp;quot;);
        }

        protected override string GetXmlContent(int templateId)
        {
            if (ContentCacheXml != null)
                return ContentCacheXml;

            return base.GetXmlContent(templateId);
        }

        private UmbracoContext umbracoContext;
        private ServiceContext serviceContext;
        private IUmbracoSettingsSection settings;
        private RoutingContext routingContext;
        private IPublishedContent currentPage;
        private RouteData routeData;
        private UmbracoHelper umbracoHelper;
        private PublishedContentRequest publishedContentRequest;

        protected override ApplicationContext CreateApplicationContext()
        {
            // Overrides the base CreateApplicationContext to inject a completely stubbed servicecontext
            serviceContext = MockHelper.GetMockedServiceContext();
            var appContext = new ApplicationContext(
                new DatabaseContext(Mock.Of&amp;lt;IDatabaseFactory&amp;gt;(), Logger, SqlSyntax, GetDbProviderName()),
                serviceContext,
                CacheHelper,
                ProfilingLogger);
            return appContext;
        }

        private void TryBaseInitialize()
        {
            // Delegates to Umbraco.Tests initialization. Gives a nice hint about disposing the support class for each test.
            try
            {
                Initialize();
            }
            catch (InvalidOperationException ex)
            {
                if (ex.Message.StartsWith(&amp;quot;Resolution is frozen&amp;quot;))
                    throw new Exception(&amp;quot;Resolution is frozen. This is probably because UmbracoSupport.DisposeUmbraco wasn't called before another UmbracoSupport.SetupUmbraco call.&amp;quot;);
            }
        }

        private void InitializeSettings()
        {
            // Stub up all the settings in Umbraco so we don't need a big app.config file.
            settings = SettingsForTests.GenerateMockSettings();
            SettingsForTests.ConfigureSettings(settings);
        }

        private void CreateCurrentPage()
        {
            // Stubs up the content used as current page in all contexts
            currentPage = Mock.Of&amp;lt;IPublishedContent&amp;gt;();
        }

        private void CreateRouteData()
        {
            // Route data is used in many of the contexts, and might need more data throughout your tests.
            routeData = new RouteData();
        }

        private void CreateContexts()
        {
            // Surface- and RenderMvcControllers need a routing context to fint the current content.
            // Umbraco.Tests creates one and whips up the UmbracoContext in the process.
            routingContext = GetRoutingContext(&amp;quot;http://localhost&amp;quot;, -1, routeData, true, settings);
            umbracoContext = routingContext.UmbracoContext;
        }

        private void CreateHelper()
        {
            umbracoHelper = new UmbracoHelper(umbracoContext, currentPage);
        }

        private void InitializePublishedContentRequest()
        {
            // Some deep core methods fetch the published content request from routedata
            // others access it through the context
            // in any case, this is the one telling everyone which content is the current content.

            publishedContentRequest = new PublishedContentRequest(new Uri(&amp;quot;http://localhost&amp;quot;), routingContext, settings.WebRouting, s =&amp;gt; new string[0])
            {
                PublishedContent = currentPage,
                Culture = CultureInfo.CurrentCulture
            };

            umbracoContext.PublishedContentRequest = publishedContentRequest;

            var routeDefinition = new RouteDefinition
            {
                PublishedContentRequest = publishedContentRequest
            };

            routeData.DataTokens.Add(&amp;quot;umbraco-route-def&amp;quot;, routeDefinition);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Why no package?&lt;/h2&gt;
&lt;p&gt;You might be wondering why I didn't package this up into a nice reusable nuget package for y'all to install. It's because THIS ISN'T IT! It isn't a silver bullet. It's boilerplate. You're bound to want to add your own varying states and preconditions to this setup. If I'd packaged it, we'd need an &amp;quot;OnThis&amp;quot;, &amp;quot;OnThat&amp;quot; and &amp;quot;OnWhatever&amp;quot; for everything that happens so you could extend it. I've done several PRs to the core to have Umbraco.Tests usable as it is now, and I don't want to create another such layer.&lt;/p&gt;
&lt;p&gt;You might for instance have to fiddle a bit with &lt;code&gt;CreateApplicationContext()&lt;/code&gt;, and I'm quite sure that any sligtly advanced site will have to have tests setting up stuff in &lt;code&gt;FreezeResolution()&lt;/code&gt;. That'd be solvable with inheritance, but here we go again... ;)&lt;/p&gt;
&lt;h2&gt;Next up&lt;/h2&gt;
&lt;p&gt;There's a few doors that open when you extract everything out of your tests. I'm dying to show you more, but I'll have to leave a lame &amp;quot;to-be-continued&amp;quot; and hope I don't mess it up. Happy refactoring, and do go get that nuget package from @kedde!&lt;/p&gt;
</description>
      <pubDate>Sat, 18 Mar 2017 22:21:23 Z</pubDate>
      <a10:updated>2017-03-18T22:21:23Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1094</guid>
      <link>https://blog.aabech.no/archive/kill-switch-weve-got-action/</link>
      <title>Kill switch, we've got Action!</title>
      <description>&lt;p&gt;&lt;img src="https://blog.aabech.no/media/1013/killswitch.png" alt="Kill switch already" /&gt;&lt;/p&gt;
&lt;p&gt;Have you ever struggled just a tiny bit following the conditional logic in a switch statement? Ever made a bug with them? How 'bout that neat extra break statement you need to put in to prevent it from falling through. In the old days, we found falling through useful. Nowadays, we aren't even allowed by the compiler. Yet we persist in adding case and break as block delimiters in those long long cascades of code.&lt;/p&gt;
&lt;p&gt;I've been reading a few new blogs today, and I stumbled over a few fairly ugly switches. So I decided to tell y'all what I think about them. I'll also show you a couple of alternatives I personally find easier on the eyes. Incidentally, they'll also lend themselves to more maintainable code, including opening doors to some nice architectural patterns.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;These techniques are far from new, switch is a well recognized &amp;quot;code-smell&amp;quot;. &lt;a href="#bottom"&gt;There's references at the bottom of the article&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let's just dive into an example. Say we have some input and we want to vary the output based on what it is. It can be based on the state of any object or value we have, not just strings. However, I'll make it simple for the sake of the example.&lt;/p&gt;
&lt;p&gt;We have some input, and we'll decide whether to kill switch:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void KillOrNot(string input)
{
    var output = &amp;quot;&amp;quot;;
    var i = 0;

    switch(input)
    {
        case &amp;quot;kill-switch-now&amp;quot;:
            output = &amp;quot;aaargh&amp;quot;;
            break;
        case &amp;quot;long-live-switch&amp;quot;:
            output = &amp;quot;noooooo&amp;quot;;
            i = -1;
            break;
    }

    // do something with this information
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice the individual logic with &lt;code&gt;i&lt;/code&gt; in there. I don't quite get what &lt;code&gt;i&lt;/code&gt; does, but I'm sure it's in there for a purpose. With &lt;code&gt;i&lt;/code&gt; in there however, this code is as bad as it gets. Imagine it with yet another variable and 15 more cases. If you really, really like such switches and want to stop reading, I recommend you at least put those blocks of logic into each of their own functions. That way each block will tell you it's purpose. We'd put the kill part in a &lt;code&gt;Kill()&lt;/code&gt; method, and the live part in a &lt;code&gt;LiveAndDecrease()&lt;/code&gt; method. At least it would cut down the size, somewhat, and we'd know what it does. We'd have one line of code per case. (Variables would be promoted to members.)&lt;/p&gt;
&lt;p&gt;But let's put the &lt;code&gt;i&lt;/code&gt; away for a moment. It probably violates the &lt;a href="https://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;single responsibility principle&lt;/a&gt; anyway. I'll get to the really good part later. Now, to swap one string for another, there's a neat class we can use for pairs of strings instead. It'll be shorter and more concise. It's called a &lt;code&gt;Dictionary&lt;/code&gt;. (Or it's sometimes faster siblings.) Here's the string lookup refactored to use a dictionary:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void Now(string input)
{
    var outputs = new Dictionary&amp;lt;string, string&amp;gt;
    {
        { &amp;quot;kill-switch-now&amp;quot;, &amp;quot;aaargh&amp;quot; }
        { &amp;quot;long-live-switch&amp;quot;, &amp;quot;nooooo&amp;quot; }
    };

    var output = outputs[input];

    // do something with this information
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice how the actual result gets as close to the predicate as possible. It's a lot easier to follow. Imagine this with 20 cases instead of 2. &lt;/p&gt;
&lt;p&gt;With simple strings out of the way, how how about that &lt;code&gt;i&lt;/code&gt;. I'll cross my fingers and hope you agree it's easier to work with if each case has it's own method. So we've promoted &lt;code&gt;output&lt;/code&gt; and &lt;code&gt;i&lt;/code&gt;, and we've created these two &lt;code&gt;Kill&lt;/code&gt; and &lt;code&gt;LiveAndDecrease&lt;/code&gt;  methods:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;string output;
int i;

void Kill()
{
    output = &amp;quot;aaaargh&amp;quot;;
}

void LiveAndDecrease()
{
    output = &amp;quot;nooooo&amp;quot;;
    i = -1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Did you guess what's next? It's another dictionary. But we'll swap out the of-type part. We'll use &lt;code&gt;Action&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Dictionary&amp;lt;string, string&amp;gt; handlers = new Dictionary&amp;lt;string, Action&amp;gt;
{
    { &amp;quot;kill-switch-now&amp;quot;, Kill }
    { &amp;quot;long-live-switch&amp;quot;, LiveAndDecrease }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And to invoke it, we just call the dictionary item:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void Now(string input)
{
    handlers[input]();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;How's that for readability? The only thing I have an issue with here is the invocation itself. However, it's one line, and we should probably name &lt;code&gt;Now&lt;/code&gt; something meaningful for our context. Once you know this trick it stops being so arcane too.&lt;/p&gt;
&lt;p&gt;If you have ReSharper installed, maintenance becomes a breeze. Whenever you write a new item in the dictionary, you'll get a small lamp on the nonexistent method name. Press &lt;code&gt;Alt+Enter&lt;/code&gt;, and hey presto - there's a new method with the right signature!&lt;/p&gt;
&lt;p&gt;If we need parameters for each block, we could either set some members, or we could add them as parameters to the action. A string parameter would be &lt;code&gt;Action&amp;lt;string&amp;gt;&lt;/code&gt;, and the signature would be &lt;code&gt;void Name(string parameter)&lt;/code&gt;. We can of course also return stuff. So we'd go &lt;code&gt;Func&amp;lt;string, string&amp;gt;&lt;/code&gt; and &lt;code&gt;handlers[input](parameter)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;All this is actually just poor man's &lt;a href="https://en.wikipedia.org/wiki/Strategy_pattern"&gt;strategy pattern&lt;/a&gt;. The bigger your blocks in the switch statement, the bigger the chance you actually have polymorphic behavior that belongs in several subclasses of a common type. For simple logic, there's nothing wrong with the poor man's edition. The fact that we've got methods for each block means we can easily extract each method to it's own class when the time comes to go big.&lt;/p&gt;
&lt;p&gt;&lt;a id="bottom"&gt;&lt;/a&gt;
If you got this far and find these ideas interesting, I highly recommend you check out these links:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Martin Fowler's page on replacing &lt;a href="http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html"&gt;conditionals with polymorphism&lt;/a&gt;&lt;br /&gt;
Basically, just buy the Refactoring book linked on the same page. ;)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://sourcemaking.com/refactoring/smells/switch-statements"&gt;The &amp;quot;pirate&amp;quot; version&lt;/a&gt; of the same refactorings&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There's a lot more out there. Just &lt;a href="https://www.google.com/search?q=clean+code"&gt;google Clean Code&lt;/a&gt;.&lt;br /&gt;
Happy refactoring! :)&lt;/p&gt;
</description>
      <pubDate>Wed, 05 Oct 2016 20:07:57 Z</pubDate>
      <a10:updated>2016-10-05T20:07:57Z</a10:updated>
    </item>
  </channel>
</rss>