Saturday, 29 January 2011

Is your code S.O.L.I.D?–DDD9 presentation

Earlier today I gave my presentation at Developer! Developer! Developer! 9 on S.O.L.I.D principles.

I considered creating a series of post on the principles but a quick search showed so many already on the internet that I decided against it, in fact searching for SOLID principles on Google resulted in Google telling me it had about 30,800,000 results.

Instead this post contains the links to the resources and code that I promised at the end of the talk.

So this is that list and I hope that everybody who saw the presentation enjoyed it and feel free to tweet or email me any questions you have.

Books

Agile Principles, Patterns & Practices in C# – the seminal work

Web Pages

Presentation slides

The slides for the presentation can be found here, I'd recommend downloading them as the Google doc viewer doesn’t display the slides correctly.

Presentation Code

The code that I used in the presentation is on GitHub which also has a wiki explaining the changes that were made between each project to meet the principle.

You can get to the code here.

Thursday, 13 January 2011

Pex & Moles

Last night at .Net Developer Network Steve Strong (@srstrong) gave us a presentation on Pex & Moles which has been created by Microsoft Research.
I first heard of Pex a while back (possibly 2 years ago now) and whilst I liked the look of it dependencies were a problem and so I didn’t pursue it any further, so I was especially interested to see Steve’s presentation and see how it had changed.

Pex

Pex is a Visual Studio add in for VS2008 and VS2010 which will create parameterised unit tests for your code, it aggressively (I’ll come back to that in a minute) goes through your code trying to create sufficient tests to attempt to get 100% code coverage.
Pex is fairly non-partisan about what unit test framework it uses and will just as happily use NUnit, MbUnit, etc as it will MSTest.
Pex will generate tests, including failing tests if the code doesn't work the way it thinks it should work.  These are created as normal tests that you can run individually just as you normally would.  If you have created any tests yourself then Pex will use those tests to improve the quality of the tests that it creates. 
Although Pex will do a fairly good job of test creation you should review the values it uses for the tests to ensure that you are happy about the values in case it has missed something or is using data which is just plain wrong; Pex does offer a host of functionality so that you can influence the data it creates restricting ranges, min or max values, etc.
Pex works by instrumenting the IL generated for your code so that it can track the various paths that are executed allowing it to then feed in various values it generates to exercise your code, these include some default values such as null, MinValue, 0, etc.
As I mentioned earlier Pex goes through your code aggressively, this means that it looks at your tests, your code, any libraries you use (including standard .Net assemblies) and attempts to follow as many branches through the code as possible.  On a practical note Pex may fail to be able to create tests to give 100% coverage if it cannot satisfy itself that it has exercised a branch in the code sufficiently, so the more complex the code the less likely Pex will manage to create the tests.  Because of this Pex is not suitable for system testing as the possible branches across a system will be just too many for it to create any meaningful tests.

Moles

Ok so you’ve got this great tool that can generate you tests but you need to mock dependencies such as a database so what do you do? You could try using Rhino Mocks or Moq but you run straight into the problem that Pex will attempt to go through the entire mocking library assembly and test that.
[Edit:] Another reason that Rhino & Moq are likely to cause problems is that because they generate new classes under the covers Steve was unsure exactly what Pex would do about those particular classes [Edit]
So the Pex team came up with Moles, which is not really a mocking library but a stubbing library, to allow you to stub your dependencies.  The way it does it is the usual sub typing method which does mean that you can’t stub the normal suspects (sealed classes, static classes, value type, etc) but Moles will go through an entire assembly and generate a mole for each type in that assembly that it can stub, included generic types as well, without you having to do it all yourself manually.
What you can then do is determine the behaviour of a mole by assigning delegates to the properties/methods of that stub and because the mole code is so simply pex is unlikely to ‘choke’ on it when observing your code.
Moles also come with behaviors (yes it is spelt correctly) and using these behaviors you can determine what the code should do for any properties/methods that you don’t explicitly provide an implementation for.  It would appear that moles work in a similar way to TypeMock and as such they are able to intercept calls in your code that normally would not be accessible to normal mocking libraries.

So are they any good?

What I’ve mentioned here only really scratches the surface of what Steve covered and you need to see Pex in action to appreciate what they can do and functionality that is available.
Steve did say that he believed you would end up using Pex to generate tests for your code and hand crafting specific tests to cover business logic that Pex just can’t get e.g. behavioural tests.
I will say that based on Steve’s presentation I will be taking another look at Pex & Moles for my code.

Many thanks to Steve for the presentation and Guy as always for organising the evening.

Tuesday, 4 January 2011

That was 2010, here comes 2011

Like so many others I thought I’d put a post together looking back at what happened in 2010 and what’s ahead for 2011.

2010 the year that was

So what did happen in 2010? Well looking at my blog posts I started the year going on about Asp.Net Dynamic data as I was using it extensively at the time and wanted to brain dump about it.

Then at the end of March I changed jobs and moved to a medium/large enterprise (dependent on your definition of size) and my focus then changed to Scrum with several posts (starting here) by myself on the state of Scrum in my new job and how I worked with the ScrumMaster to fix them.  I also got my Certified Scrum Master and have applied for Certified Scrum Professional (CSP).

After I made a ‘harsh’ comment about a presentation at DDD8 I was challenged to do better myself and that started me down the path to give presentations, starting with grok talks at .Net Developers Network on Selnium (slides available here) and Asp.Net Dynamic data. I then went on to give presentations at DDD South West and DDD Ireland 10 about Scrum.

The commute to my new job is on the train and I used the time to catch up on some reading and finally managed to read some ‘essential’ books such as Code Complete, Clean Code, Extreme Programming Explained, etc and I started to review books for Manning books my favourite so far by Jon Skeet’s C# in depth 2nd Edition.

The one thing I didn’t do a lot of in 2010 was green field development in work, it was code verification, refactoring and for the majority of my time getting the team to work well together and really understand Scrum, however, I did do some of my OSS project which I haven’t released into the wild yet.

2011 the year ahead

So what do I want to do this year?

Well I want to build on what I did last year with more user group presentations (anybody interested give me a yell) and have made a good start by having my session Is your code S.O.L.I.D? accepted for DDD9 on 29th January.

My reading will continue, currently reading Agile Principles, Patterns and Practices in C#, looking to read more essentials such as Refactoring: Improving the design of existing code and hoping to continue doing some more book reviews for Manning as I’ve really enjoyed that so far.

I’m hoping that my CSP application will be approved as that will be the last scrum certification I will look at doing unless I decide to go for Certified Scrum Coach, but that’s unlikely at the moment.

The one thing I’m hoping will change is the amount of development I get to do;  I'm looking to get my OSS project into the public domain and also an example of the Repository pattern that I’ve been working on (expect blog posts soon).  In the work place I’m looking at implementing continuous integration first for automated builds and then testing, not sure if I’ll get to automated deployment yet.  If I do manage to find the time, and I really hope I do, I want to look at F# and Ruby but it will probably depend on other commitments as to whether I get the time to do this.

The one thing that I would love, but wonder if its possible, is to earn a MVP but it may not be possible this year (or the next) I need to get more involved in community events, etc.

Right then, best get to it.