Wednesday, 10 July 2013

Curse of the Base class

Please do not get me wrong, Inheritance has its place.

In this post, I will share a very badly designed base class implementation. The goal is not to criticise someones work, but to learn from it.

Some code

The Platform (developed by a 3ed party) we use had a base-class implementation that was similar to the following.

The class itself looks innocent but it has massive problems. The following is the example provided in the documentation.

So how can we actually unit test this code. Nightmare!

Refactor (ugly)


We need to organise the class so that is it suitable for unit testing.

The following is my first attempt. (Remarks: I am using bastard injection in this example.)

We have removed the call to the base class "Add" method in the derived class "Add". The most odd looking code is in the public constructor. It is calling the internal constructor with "IDatabase" and null. If you remember the constructor resolution order, you might remember that the public constructor is resolved last (in the above example). This means that the "_addAction" delegate is initialised twice. Once with null then by the actual value from the public constructor.


Unit test

Sorry, ideally you should be writing a test first. (TDD!).

The unit test should focus on the code we wrote, not what is provided by the Platform.

Its all here.

Sunday, 7 July 2013

Udacity Web Development course

I finally got around to completing the "Web Development" course on Udacity. I have to say the instructor Steve Hoffman has very good technical knowledge. The examples he presented are very good and "real".

It is really great to see instructors outside the academia conducting these courses. Sometime what we need is practical and realistic examples rather than hypothesising a use case.

My next target is Software Testing course.

Big thank you to Udacity for presenting these courses.

My cert :-)