Avoiding the D’oh (aka assumptions are bad)
I’ve just had one of those d’oh moments, if only I had written a unit test for 1 simple constructor I would have saved myself about 3 hours of pointless frustration.
I am currently integrating NHibernate into my current project, which basically means I am writing a lot of mapping files. Having never used NHibernate before I don’t have that invaluable store of wisdom built up from previous implementations – so I am relying on some very simple unit tests to make sure my mapping files are correct.
The problem came about when I was trying to map a collection of value types (EmploymentDetails) that hold the details of the entities (Employee) employment history over time (essentially what store they worked in when). While I was looking at the domain object I realised I could clean it up a little and make it express the intent more correctly as well as a little more efficiently, so I quickly hacked together the changes and carried on building the mapping file – BIG MISTAKE. No tests were written, it was only a simple rename and move a couple lines of code around in the constructor right? WRONG. I forgot 1 crucial line of code and that just cost me about 3 hours chasing what I thought was a problem in the mapping file, when it was actually a problem in the code.
Integrating a new technology (defined as one you have no prior experience with), is a high risk exercise. The simplest way to reduce risk is to change as little as possible and make sure it works. Unit testing allows you to follow this cycle easily, but you have to commit to it completely, otherwise the value of your investment is significantly reduced – you get a false sense of security about the state of your code and build on those assumptions.
It’s also wise to remember that no matter what – you are a terrible coder and should therefore check ALL your assumptions that your current problem is built on before trying to solve the problem – sometimes it is incredibly useful to assert these as preconditions for your test – just so you really are sure your assumptions are correct.
Lesson learned, I’m off to retrofit a test or 2