Jan 16, 2009

Decoupling Your Code, By Example

Let’s just say, for arguments sake, that you’ve run across a piece of code that is tightly coupled, i.e. there are lots of hard links between classes, it’s hard to test, changes are difficult, the code breaks when it shouldn’t, etc.  Let’s also say, for arguments sake, that you want to improve the design of that code and make it somewhat more maintainable so that you don’t spend the rest of your natural life trying to keep the code from completely falling apart.

How would you do that?

In this screen cast I’ll walk through a sample application, show where the tight coupling occurs and how to remove it.  In the process I’ll also be cover a bit of unit testing vs integration testing, mock frameworks, and inversion of control containers (Unity in this case).

Hopefully you get some value from it.

Download the WMV (93Mb, 44mins)

Double click for full screen


For those interested in the Visual Studio add-ons they saw, I am using:

Finally, the video is hosted at http://silverlight.live.com.


  1. Great screencast Richard. Look forward to some more goodness.

  2. I'd like to see more about when to decouple and when not to.

    I think that is the most confusing aspect of dependency injection and I have never seen it covered well.

    Otherwise, well done.

  3. Nice demo Richard

    I'm sure you put it there to catch who was watching to the end, but your blog URL on the last slide was blogpost.com :)

  4. Great sample, could you share the code ?

  5. @DamianM: LOL. Umm... yeah. Deliberate typos are my onpurposeness... Gaahh!! Now I have to re-record the whole thing :-)

    @acarum: I'll try and get the code up somewhere over the next few days (the starting point that is). It's not brilliant code - just enough for the purposes of the demo. Hope that's OK.

  6. @liam: When to decouple is an interesting question. I might knock up a blog post on it soon because this is a somewhat simplistic response: for existing code, decouple it when you are making changes (and when you have the time to do it properly). If it's new code, try and keep it loosely coupled from the start.