Using events to improve testability and reduce temporal coupling.

25. October 2008

Temporal coupling in code is usually not very obvious but can cause maintainability nightmares. Code is coupled not by direct dependencies but by depending on the state that left behind by another piece of code. Temporal coupling is not something that should be avoided but it should be expressed explicitly. Someone editing your code should be able to see clearly where dependencies lie.

Functional languages solve this problem by not having side effects. Ordering of functions is very clear if you follow the returned values. In OO languages like C# you don't always have this luxury. Side effects can make things difficult for us. In this post I want to show a different approach to solving this problem

In higher level components, for example the presenter in an MVP UI architecture you often find code like this (warning, contrived example with non-production pseudo code ahead):

   1: public void LoadFile( string path )
   2: {
   3:     fileLoadService.FillModelFromFile( path, model );
   4:     if( model.Verify() )
   5:     {
   6:         view.ShowFileNameInTitle( inputFile.Name );
   7:         view.ShowLines( inputFile.Lines );
   8:     }
   9: }

Effectively you’re just sequencing calls here. The only reason for the existence of LoadFile is to make sure that all the functions that actually do anything are called in the right order. When I started writing tests for code like this I started feeling a bit uneasy:

   1: [Fact]
   2: public void ShouldCallModelVerifyAfterFillModel() { //blah.. }
   3:  
   4: [Fact]
   5: public void ShouldShowFileNameViewWhenVerified() { //blah.. }
   6:  
   7: [Fact] 
   8: public void ShouldShowLinesInViewWhenVerified() { //blah.. }

I won’t bore you with the details of the tests. Let’s just say that there’s a lot of mockery and some rhino’s. If you’re used to NUnit think [Test] instead of [Fact]. The point I want to bring across here is that I am forced to test the order in which the functions are called. This means I’m testing implementation details. This isn’t optimal. I don’t know if this is an official test-smell but it should be.

But as is ofthen the case it’s not the tests that should be fixed, it’s the code. Tests smells often point out code smells. Actually I don’t care if model.Verify() is called after fileLoadService.FillModelFromFile(). I only care that the model is verified when it changes to a possible unverified state. I don’t care if the filename and lines are shown in the view when it’s verified, I only care that they are updated every time the model changes to a verified state. So the dependencies I care about are kept hidden while I’m testing them indirectly by testing the order of the calls.

Eventually this will get more of a mess. If the presenter grows more of these implicit dependencies will show up and start conflicting with each other. Sequences like this will show up in more functions. They will be similar but different and hard to maintain.

Events to the rescue!

What we want here is to be notified by the model when it needs to be verified and when it changes so that the display needs to be refreshed. I’d write more code for you but I think this is best illustrated with a sequence diagram.

Here is the original situation where the presenter does all the work:

image

 

 

 

 

And here is how we can make this better, the model fires events and the presenter reacts. Coupling between expectations on the state of the model and the reaction by the presenter are made explicit here.

image

This will cause me to write tests like these:

   1: [Fact]
   2: public void ShouldVerifyOnModelVerifyRequired() { //blah... }
   3:  
   4: [Fact]
   5: public void ShouldDisplayTitleWhenModelChangesVerified() { //blah... }

 

I don’t know about you but I find this a lot clearer. The tests reflect requirements instead of implementation. The logic in the presenter is also partitioned better and is easier to reuse.

Comments

10/27/2008 2:54:40 AM

trackback

Trackback from DotNetKicks.com

Using events to improve testability and reduce temporal coupling.

DotNetKicks.com

10/27/2008 12:28:13 PM

Jon Limjap

What unit testing framework uses Fact instead of Test as the attribute name?

Jon Limjap Republic of the Philippines

10/27/2008 1:39:44 PM

Khalid Abuhakmeh

It's a cool idea, but doesn't it go against the TDD philosophy of "You Ain't Gonna Need It?" You are adding code to your presenter which, in theory, will only be used by your testing framework. Also instead of events, you could try an Aspect Oriented approach. Create an attribute that sits on the class you want to test, then the attribute watches your methods and looks to see that a method has fired. No events and a small footprint on your code that is easily removed on the production build (if you want to remove it).

Khalid Abuhakmeh United States

10/27/2008 1:42:07 PM

Mendelt

Hey Jon,

Thanks for commenting!

I use XUnit.Net(http://www.codeplex.com/xunit) It's got all kinds of small improvements over the standard NUnit. I like the [Fact] stuff, the assert syntax is a tiny bit better (Assert.Equal instead of Assert.AreEqual) No need to use [TestFixture] anything with [Fact]'s is a testfixture. No more setup and teardown. Now that you mention it I'll have to write a blogpost on it Smile

Mendelt

10/27/2008 1:50:43 PM

Mendelt

Hi Khalid,

Thank you for reading my blog and commenting!

I'm not really adding code to the presenter. Just partitioning it differently. Some of the code in the LoadFile() method is moved into eventhandlers that react to the model. Any code in the presenter and the model will be used in production.

I might be able to use AOP. I don't know if handling events with AOP works but maybe I could use it to make the model fire the events when it changes. When I get the time to finally play with postsharp or the Castle project aop stuff I'll try it out!

Mendelt

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme adapted from BlogEngine.NET standard theme by Mads Kristensen

Mendelt Siebenga

Mendelt Siebenga with coffeeMendelt Siebenga works as a C# programmer. In his spare time he's been known to pick up Python, Lisp and even a soldering iron from time to time.

You can also find me here