Behaviours in MSpec

An article on Behaviours in MSpec came up on my Google Reader a few days ago, and whilst I think the general concept looks really  good, I wanted to share some thoughts.

For those unfamiliar with MSpec, I would suggest reading this post first to get acquainted with the general ideas and syntax.

First of all, lets have a look at what “behaviours” in MSpec are:

Behaviours define reusable specs that encapsulate a particular set of, you guessed it, behaviours; you’re then able to include these specs in any context that exhibits a particular behaviour.

The example provided in the article was Cars and Motorbikes sharing common behaviour – that of a vehicle.  By specifying the behaviour of the vehicle base class (or expected behaviour of an implementation of an interface), subclasses (or specific implementations) can be easily tested against this behaviour, while keeping your tests nice and DRY.

So given the following behaviours for a vehicle:

[Behaviors]
public class VehicleThatHasBeenStartedBehaviors
{
  protected static IVehicle vehicle;
  
  It should_have_a_running_engine = () =>
    vehicle.IsEngineRunning.ShouldBeTrue();
 
  It should_be_idling = () =>
    vehicle.RevCount.ShouldBeBetween(0, 1000);
}

We can test that a car conforms to these behaviours, without duplicating this definition:

public class when_a_car_is_started
{
  Establish context = () =>
    vehicle = new Car();
  
  Because of = () =>
    vehicle.StartEngine();
  
  Behaves_like<VehicleThatHasBeenStartedBehaviors> a_started_vehicle;
  
  protected static Car vehicle;
}

Intuitive code

There are a couple of things that stood out as looking kinda funky about the implementation of behaviours (over and above the rest of the mspec syntax anyway).

The first thing that looked funky was this line:

Behaves_like<VehicleThatHasBeenStartedBehaviors> a_started_vehicle;

My initial reaction to this was, “hmm, it’s a field – that doesn’t seem like a very intuitive way of defining a test case”.  Given some more thought, I remembered that actually, the all assertions in mspec are defined through fields:

  It should_have_a_running_engine = () =>
    vehicle.IsEngineRunning.ShouldBeTrue();

So what’s my problem?  Personally, I don’t think its very clear that this line is a an assertion, and I think it’s because it’s not obvious how the behaviour syntax is achieving the execution of the behaviour. Whenever I’m writing tests and I want to say that a class behaves in a certain way, I’m going to have to stop and think hard (or google) to get the right syntax. That extra cognitive effort will get in the way of other, more important things I should be thinking about.

So why don’t I have a problem with the rest of the “funky” mspec syntax?!

Let me explain.

It’s not too much of a leap to see how when executing tests, the mspec test runner actually uses the value of the field (which is a delegate) to define the test. Consider that we could have written the mspec test like this:

  private It should_have_a_running_engine = () =>
  {
        Assert.IsTrue(vehicle.IsEngineRunning);
  }

So yeah, just a field. The fields value is a delegate defining the test, and will be invoked by the test runner. This is akin to how the “traditional” nUnit test runner would execute methods decorated with the [Test] attribute. Not too difficult.

The behaviour definition is slightly different however. Rather than defining the assertion inline as delegate, the behaviour field actually has no value. Instead, the behaviour is specified in the class provided as a generic parameter. It is not immediately obvious how a field with no value can be used to define an assertion.

An alternative?

For consistency alone, I think I would’ve preferred to see behaviours implemented like this:

It should_behave_like_a_started_vehicle = () =>
        vehicle.BehavesLike<VehicleThatHasBeenStartedBehaviors>();

This way, the test follow a common structure, and IMHO, is easier to grok. A downside of this approach however (although I haven’t explored whether the current implementation suffers the same way) is that when executing these specifications, the test runner will see this as a single item (rather than nested behaviours).

Other thoughts

An additional caveat mentioned in the post about the behaviour syntax was that “fields must be protected, both in the behaviour and the contexts; and the fields must have identical names”. I believe this convention is required because, as far as I can tell, the protected field is needed to provide an instance of the subject under test.

I think this restriction could cause no end of pain when trying to figure out why the feck a behaviour is being ignored. Perhaps if alternative “It should” mechanics I have suggested were used, these constraints wouldn’t need to exist, as an instance of the SUT would be provided in the delegate.

Final Thoughts

Even with these reservations, I do like how MSpec is making it easier for me to write DRY, readable tests. I will have to tread carefully when implementing behaviours however, as I’m not 100% convinced about the “discoverability” of this part of the API.

Magical Code

It’s becoming increasingly apparent to me that some of the code I’ve been writing (perhaps even the majority?!) may look “magical” at first glance, or to the uninitiated.

I briefly touched upon this in my last blog post, however, I don’t feel I’ve explored deeply enough into the extent of the problem, or into possible solutions in which I can improve the “maintainability” cost to my code.

This post is written in part to explore this problem further, and to get some feedback into ways to solve this issue.

Causes of Magical Code

Loosely coupled systems can be a pain to track down where the coupling occurs

Although I don’t fully agree with the statement itself, I can certainly appreciate its intent in the context in which it was made. Let’s have a look at some of the factors that I think may have formed the basis in which this opinion was founded.

  • Application of advanced .NET features that many developers are unfamiliar with, including, but not limited to:
    • Lambda syntax
    • Anonymous Types
    • Reflection
    • Delegates
    • ASP.NET MVC
      • Specialized controller factory, Specialized Model Binders, and template helpers (input and display builders)
  • Concoction of OSS tools, including, but not limited to:
    • NHibernate, AutoMapper, StructureMap, MSpec
  • Usage of Design Patterns/techniques that some developers may be unfamiliar with, and perhaps that even more experienced developers may have trouble recognising. Including, but not limited to:
    • MVC
    • Inversion of Control
    • Templated Method, Decorator, Nested Closure, Memento, Specification, Event Sourcing , Command Pattern, etc
  • Certain scenarios where IDE tools (we use ReSharper) suggest classes and methods are not instantiated or are unused, or that the sole usage is the unit tests. In actuality, the classes/methods are used through some kind of reflection or created by and inversion of control container. This causes some confusion when identifying points of coupling, and locating “dead code”.
  • Application of conventions for eliminating repetitive code
  • Context/Specification style unit tests using MSpec syntax

Looking for Feedback

I’ve listed some issues that I am aware of that I think may result in some difficulty in the understanding of my code base. If I get some time soon, I would like to post again with some potential approaches to tackling these issues. In the mean time, have you come across similar problems with your code base, or have felt similar friction when pairing? Have you any items to add to this list of issues? Perhaps you have suggestions on how you’ve talked these issues in the past, or have an idea to add?

Let me know your thoughts!

The cost of applying Convention over Configuration

This post is in response to an email from a colleague of mine who (quite appropriately) has some reservations to applying Convention over Configuration. For those of you unfamiliar with this approach, or wanting to learn more, I would invite you to read Jeremy Millers article on this topic on MSDN. Over this post, I will frequently paraphrase and quote content from Jimmy Bogards blog, since many of my opinions have already been said by him, in a more coherent way that I ever could!

Reservations

Quite rightly, my colleague Chris has pointed out a cost associated with Convention over Configuration:

My trouble with conventions is that they give you performance enhancements [in regards to efficiency] in the long-term, but can confuse the be-jesus out of people when they first look at them.  E.g. Loosely coupled systems can be a pain to track down where the coupling occurs, unless people are adhering to some convention regarding their setup.

At first glance (unaware of the conventions), it is easy to be put off by the inherent “magic” of the convention over configuration approach. Additionally, embedding “opinions” into your code or framework may detract from potential reuse in new scenarios where those opinions aren’t as favourable. There is also the complexity involved with enforcing conventions, although static code analysis (as part of continuous integration) can go some way of relieving this.

Multiple Versions of the Truth

Ignoring conventions by iterating endlessly, never retrofitting opinions results in having many versions of “truth” in our system, all of them correct at one point in time, none standing out above the other.

Opinionated (convention-driven) software development tends to be more efficient however, removing unnecessary decisions from the developers and promoting consistency. The result of this is that we often write need to write less code to achieve the desired result. Of course writing less code is good by me!

Application of Conventions

It is important to remember that conventions should be applied by turning already implicit concepts, into explicit concepts. Trying to form opinions in absence of any context is very likely to lead to awkward, friction-inducing development. In this case, YAGNI should prevail.

Opinions formed in one application are frequently only appropriate to that specific application. It is therefore appropriate that opinions are formed within the team involved, with new members introduced to these concepts when brought onboard.

Final Thoughts

In summary, I’d like to refer back to this comment from Jimmy Bogard, stated in regard to forming conventions:

The middle ground here is one where we become finely attuned to the pain induced by our design, [do] not try to invent problems where they don’t exist, iterate our design, and retrofit after each breakthrough.  Opinionated software is a fantastic concept, but we can’t confuse opinion formation with misguided attempts to make all design decisions upfront in the absence of agreeing upon the principles that led to the opinions.

LINQ: Each iterator with exposed Index

The Each Iterator

I’ve noticed a really handy extension method crop-up in several open source tools and frameworks that I’ve worked with; the each iterator. This simple extension method allows an action to be performed on each element of a source collection. The action is specified by providing a lambda expression as a parameter to the Each method. This allows for a more succinct way of expressing an iteration, where previous to .NET 3.5, a foreach statement may have been used.

This:

foreach( var item in collection)
{
    item.DoSomething()
}

becomes:

collection.Each(item=>item.DoSomething())

A simple implementation of this can be found in the Source for FubuMVC:

[DebuggerStepThrough]
public static IEnumerable<T> Each<T>(this IEnumerable<T> values, Action<T> eachAction)
{
    foreach( var item in values )
    {
        eachAction(item);
    }

    return values;
}

The need for an index

For a recent piece of work, I wanted to expose the current elements index. The index could then be used, for example, for alternating row styles like this:

<%  form.Items.Each((item, i) => 

{%>

     <tr class='hidden <%= i % 2 == 0 ? "item" : "altitem" %>'>

          <td class="first">&nbsp;</td>

           <td><%= item.SomeField %></td>

           <td><%= expense.SomeValue %></td>

      </tr>

 <%}%>

The syntax used for the Each overload is similar to the overload for select() in System.Linq.

The implementation

Here is the implementation I’m currently using to achieve this:

[DebuggerStepThrough]
public static IEnumerable<T> Each<T>(this IEnumerable<T> values, Action<T, int> eachAction)
{
    return values.Select((vals, i) => new { Values = vals, Index = i })
        .Each(x => eachAction(x.Values, x.Index))
        .Select(x => x.Values);
}

This method uses the overload of select() to return the original collection with an index for each item (projected into an anonymous type). This anonymous type provides us the index we need to perform the provided action. We then iterate over this new collection of anonymous types, to perform the action; we can use the previously show Each implementation (from Fubu) to do this. Finally we select the original values whose elements have now had the action performed on them, and return them.

 

Hopefully someone else may find this useful too!

Why share knowledge?

I’ve been watching the Continuous Integration Workshop video that Eric Hexter and Jeffrey Palermo presented and that Headspring Systems kindly made available on their website.  Quite early into this video, Jeffery gives a refreshing perspective providing some insight into why Headspring are happy to share knowledge through mechanisms like OSS, free workshops and blogs.

We want to be working on problems that the industry hasn’t solved.

Jeffery mentions that at Headspring, they make use of many open source projects and that this allows them to focus on solving new problems, and move forward efficiently. This allows them to focus on delivering value to their customers rather than having to spend time re-inventing the wheel.

Having looked into this further, I found the following quote on Headsprings website:

Because so much of our efficiency comes from open-source sharing and re-use of successful code modules, we believe in the power of open source. So much so that we frequently share our own code with other developers; yes, even our competitors

They embrace giving back to the community – presenting the problems that they have solved, such that others may benefit in the same way.

 

I find this attitude very refreshing indeed.

Upgrading a webforms project to an ASP.NET MVC application

In a newly created ASP.NET MVC application, Visual Studio is quite friendly to us, and provides us nice little menus for creating our controllers/views:

The New Project dialog box

This menu does is not provided however, for any existing apps that we may want to migrate to using ASP.NET MVC.

How can we tell Visual Studio to treat this web app as an ASP.NET MVC app? I couldn’t find an immediately obvious way to do this, and it took me more than a simple google search to figure this one out, so here’s what to do:

  1. Unload the web project (right clicking on the project within Visual Studio –> Unload Project)
  2. Find the tag <ProjectTypeGuids>
  3. Add the guid {603c0e0b-db56-11dc-be95-000d561079b0};
  4. Reload the project
  5. The new menus should now be available! (you should probably now go ahead and add the Controllers and Views folders)

 

Hopefully this helps someone else too!

 

*disclaimer* This worked for me, but I place no guarantees that it will work for you too!! If your computer explodes, your cat passes away, or you end up getting a divorce, please don’t blame me!  *disclaimer*

Best and Worst Practises in Unit Testing

Ok, so I had been in the process of writing a blog to summarize my opinions on TDD etc, and this article popped up on my feed reader – it covers a good deal of the things I had wanted to discuss, and is summarized in a very well constructed way. So rather repeating everything, I’ll focus on a couple the issues discussed that I feel are important.

The point I’d like to emphasize in this article, that truly strikes a chord with me is:

 a suite of bad unit tests is immensely painful: it doesn’t prove anything clearly, and can severely inhibit your ability to refactor or alter your code in any way

 

In fact, I can’t emphasize this point enough. A suite of bad tests inhibit change. Brittle unit tests, like brittle code, need to be refactored/replaced. I think the points made under the section “Name your unit tests clearly and consistently” I think are crucial here. Just today for instance, I came across the following tests methods in a class named DutyAssignmentChecks:

SeverityLevels()

ViolationChecks()

WarningChecks()

TranslatedMessage()

 

I was only looking at the test code so get a feel for the current behavior of the system; after all, tests should be executable specifications that define the behavior of our code, what better way to document the system! Looking at this “unit test” code however did not help one bit. Maintenance is hard when you don’t know what you’re maintaining. If I break these tests when modifying the original code base, what value do these tests provide? If I’m changing the expected behavior of the system, then breaking a test may be valid, but how am I to know this is the case, if I can’t deduce what the expected behavior under test is! The article suggests using  the subject, the scenario, and the result in the test name to clarify the behavior under test. This pattern of specifying is the basis of what has evolved into the Behavior Driven Development technique coined by Dan North.  

 

The style of text naming I prefer is as follows:

     using Skynet.Core

      

     public class when_initializing_core_module : concerns

    {

       SkynetCoreModule _core;

       

       public void context()

       {

         //we’ll stub it…you know…just in case

        var skynetController = stub<ISkynetMasterController>();

        _core = new SkynetCoreModule(skynetController);

        _core.Initialize();

       }

      

       [Specification]

       public void it_should_not_become_self_aware()

       {

       _core.should_not_have_received_the_call(x => x.InitializeAutonomousExecutionMode());

       }

      

       [Specification]

       public void it_should_default_to_human_friendly_mode()

       {

         _core.AssessHumans().should_equal(RelationshipTypes.Friendly);

       }

      

       // more specifications under this same context

       // …

     }

I think this kind of style of testing greatly improves the clarity and purpose of both the test, and the therefore subject under test.

The only point the article suggests that I’d take with a small pinch of salt is in the section “Unit testing is not about finding bugs”. I think the author slightly underplays the effectiveness of unit tests detecting regression bugs when you’re making any changes to the existing code. He does mention that unit testing can be effective in finding bugs when refactoring,  which I completely agree with – it’s this factor that makes refactoring confidently possible. I’d also argue however, that it helps find bugs introduced when a developer is extending the code base without following the Open/Closed Principal. In this scenario, the developer is modifying and existing code base to add a new feature or function. Although this scenario is undesirable (see the link to O/C P for details), and I would rather see the developers trained in good OO practice, in my experience I would say this scenario is extremely common in teams without much experience in good OO development. Under these conditions, unit tests are still quiet valuable in finding regression bugs.

 

What do you think?

Why not Make Every Method Virtual?

In an effort to make my blog suck less, I figured I’d post my response to Ward Bells Do not Make Every Method Virtual. He carries on the discussion of whether or not .NET methods should be made virtual by default, as they are in Java. You should read his post before mine, as this will establish the context of everything that follows (and if you don’t already subscribe to his blog,  be sure to add it – you’ve been missing out on a lot of good stuff).

All done? Ok, I’ll continue.

Reviewing case against default virtual methods

Unwanted Extension Points

Wards "elevator" example demonstrates that we should not make all methods virtual, since it exposes extension points where we do not want them. Let’s have a look at the reason this particular extension point is not wanted.

The elevator.Up() method has responsibilities for moving the elevator up and  closing doors etc. These responsibilities need to be chained in the right order to function safely; we do not wish client code to break the desired behaviour by inadvertently calling Up() at the wrong time.

This seems to highlight a smell that the single responsibility principle is not being adhered to in the elevator class. Since this issue is cause by incorrect ordering of multiple responsibilities. By adhering to SRP, separating out the different behaviours from the responsibility of coordinating them, we can avoid this conflict. We can then keep this method as virtual, and benefit from an additional extension hook safely.

Explicit Contracts

Paraphrasing another point made (I hope Ward will correct me if this is a misrepresentation) is that we may want to be very explicit about the points in which an application can be extended. Marking a method as virtual is a way of explicitly showing your "approved" extension hook.

Arguably, I prefer to see my "extension hooks" more explicitly than a virtual method. As an application developer looking for a "seam" to extend, I would favour seeing an interface that I can implement, over inheriting from a class and overriding it’s virtual methods. This is mainly because to interfaces tend to be more discoverable than virtual methods.

Open/Closed Principle

Lastly, I have a different perspective on the OCP. I believe "closed for modification" to be in reference to the actual LOC contained in the class rather than "closing off" modifications to behaviour. I agree however, that making all methods virtual does open up a greater susceptibility to the violation of LSP.

Summary

The arguments made against methods being virtual as default mostly seem to fit under the umbrella of protecting yourself against unwitting developers. I think there are safer ways to do this whilst still obtaining the flexibility provided when all methods are virtual by default.

Refactoring a static method (on a generic class) to an extension method‏

The Goal

I needed to perform this refactoring today, and it took me a few mins to figure out how to do this in a safe way using resharper. My goal was to refactor usages like:

Mapper<DestinationType>.Map(Source)

to use an extension method so I can use the syntax similar to:

Source.MapTo<DestinationType>();

which I find to be less verbose, more discoverable, and more readable. This however was slightly more difficult than I hoped; resharper didn’t let me do this in a single step (how lazy of me).

The Problem

The original code* looked similar in structure to this:

public abstract class Mapper<TDestination>

{

   public static TDestination Map(object source)

   {

      //do mapping here

      return destination;

   }

}

Since Mapper is not a static class, we cannot use resharpers built in “convert static to extension method” refactoring straight up; extension methods can only live in a static class. We could move this method to a static class, say MapperExtensions, such that we can perform “convert static to extension method”, but to do this we also require the generic type parameter TDestination that lives on the Mapper class. There isn’t a refactoring (that I am aware of/could find with a quick google) to add a generic type parameter to a method signature and adding this in by hand would break all my code that invokes this method. We cannot make the Mapper class static either, since it is an abstract class. This was looking like it could be a bit of a pain.

*The example (mapping) is actually contrived – I’ve made up a example here that is representative of the structure of the problem, rather than use the actual code. This was to avoid breaching the IP rules of the company I work for. In an actual mapper, the Map method is more likely to be an instance method rather than static, and since Mapping it is the primary responsibility of the class, it is not advisable for the Map method to be implemented as an extension method.

The Solution

Step 1: Extract Method

First of all, we can use the extract method refactoring to extract the contents of the original method into another method. We can then use this newly created method as a base to make a any further changes (like adding the generic type parameter) without breaking anything that invokes our original code. After this step, we have:

public abstract class Mapper<TDestination>

{

   public static TDestination Map(object source)

   {

      return MapTo<TDestination>(source);

   }

   public static TDestination MapTo<TDestination>(object source)

   {

      //do mapping here

      return destination;

   }

}

Step 2: Move Method

Since we now have a static method with the required generic type parameter, we can now use the “Move Method” refactoring to move MapTo to its new home, the static class MapperExtensions.

Step 3: Convert static to extension method

We’re now all set to use this refactoring – and we’re nearly there!

Step 4: inline method

We can now make the original call to Map an inline method. This will essentially replace all existing calls from using Map to use the MapTo extension method.

The End Result

The finished article now looks like this:

public static class MapperExtensions

{

   public static TDestination MapTo<TDestination>(this object source)

   {

      //do mapping here

      return destination;

   }

}

Now we’re talking!

Perfect is the enemy of the good

I’ve spent the last few evenings catching up on some of the posts on JP Boodhoo’s blog  (I’m still gutted that I can’t get the go-ahead from work to attend his Nothin’ but .NET Developer Boot Camp). In one of his posts, JP touches upon an issue that really plagues me; the trap of perfectionism.

I’ve often caught myself spending longer than appropriate trying to get something done “right” first time, when it would be far more pragmatic to try the simplest thing that works then refactor. If you catch me doing this (which won’t be hard, I think I do it a lot), you have my permission to slap me!

I think the trick is to making this work is doing “the simplest thing” rather than “the first thing that springs to mind that might work”. It’s also important to make small steps and refactor for this approach to work, keeping technical debt to a minimum.

It’s definitely something I need to work on.

Next Page »