A week of fire fighting (aka why you should <3 unit tests).

I feel like I’ve spent most of this week debugging some PHP code, and writing unit tests for it. Thankfully I think this firefighting exercise is nearly over.

Perhaps the alarm bells should have been going off a bit more in my head when it was implied that the code was being written in a quick-and-dirty manner (“as long as it works….”) and the customer kept adding in additional requirements – to the extent it is no longer clear where the end of the project is.

“It’s really simple – you just need to get a price back from two APIs”

then they added in :

“Some customers need to see the cheapest, some should only see some specific providers …”

and then :

“We want a global markup to be applied to all quotes”

and then :

“We also want a per customer markup”

and so on….

And the customer didn’t provide us with any verified test data (i.e. for a quote consisting of X it should cost A+B+C+D=Y).

The end result is an application which talks to two remote APIs to retrieve quotes. Users are asked at least 6 questions per quote (so there are a significant number of variations).

Experience made me slightly paranoid about editing the code base – I was worried I’d fix a bug in one pathway only to break another. On top of which, I initially didn’t really have any idea of whether it was broken or not – because I didn’t know what the correct (£) answers were.

Anyway, to start with, it was a bit like :

  • Deploy through some weird copy+pasting manner due to Windows file permissions
  • No unit test coverage
  • No logging
  • Apparently finished
  • Apparently working (but the customer kept asking for more changes)


  • Deployment through git; Stupid Windows file permissions fixed.
  • Merged in changes by third party graphics designer – should be much easier to track any changes he makes in the future
  • ~80% test code coverage. I’ve had to modify the ‘real’ scripts to accept a new parameter, which would make them read a local XML file (rather than the remote API/service) – in order for the tests to be reproducible (and quick to run)
  • Logging in place, so there’s some traceability
  • Better error handing
  • Calculations manually checked and confirmed.

Interesting things I didn’t like about the code :

  • Premature attempt at dependency injection – the few functions there are/were have a $db variable being passed in – but often don’t use it.
  • There is significant duplication in the code base still.
  • The code didn’t (and still doesn’t really) support testing particularly well – I’m having to retrieve output through a Zend_Http_Client call (i.e. mimicking a browser) – which means I can’t get code test coverage stats easily.
  • In some places a variable was being set to 0 (e.g. $speed) which would be used to determine a special case (if $speed == 0). Having multiple meanings in/on the same variable makes it difficult to follow what’s going on – and is a pain when the customer requests it behaves a bit differently. Really a separate parameter should have been used.

State of the union – sort of (my 2011).

Well, perhaps not quite a State of the Union Address, but here’s a random update on my life in general which perhaps sums up the last year (as we’re almost at the end of the year, it’s probably fitting I somehow, somewhere write something like this).

  • My children (Rowan and Anya) are both growing up rather too quickly. Anya’s about 18 months old, walking and almost talking (‘tree’, ‘cat’, ‘that’, ‘tasty’, ‘mum’, ‘daddy’ and so on) while Rowan (~4) is busy playing/asking questions/learning to write/bashing things with hammers and so on. As far as we can tell they’re unaffected by Katherine and I splitting up. I’m lucky to be able to see them regularly (4 times a week) and usually ‘good enough’ for hugs and cuddles after they fall over / chuck up or whatever depending on the star alignment or who ever told them off most recently…..
  • Pale Purple (work) wise – everything’s going well, we’re busy, have plenty of work lined up over the next few months and there are a number of interesting projects almost ready to start. Over the last year there has been a distinct increase in the amount of development we’re doing in mobile applications – specifically towards Android and business apps (e.g. for a delivery driver to use to see what jobs they have to do – rather than the traditional Microsoft Windows CE based thing). PHP is still the main focus of the company with other supplementary bits (training, security audits, systems administration and so on). We took on an industrial year student this year – so there are currently five of us full time.
  • I’ve moved house – so I no longer live in a one bedroom flat which was always cold [no central heating]. Now I’m in a 2 up, 2 down house like thing, so the children can have their own bedroom, or spread their toys over a wider area. Soon there will be a trip to Ikea and they’ll have a bunk bed and random other things no doubt.
  • I still run / cycle / exercise – although not as much as I might like to. Mr Patch, the Jack Russell, went to live with my aunt at the beginning of the year (I think?!) – where he’s apparently behaving well, and has become somewhat wider; likewise his absence here is partly to blame for my weight gain – but conversely not having him makes looking after the children / work / jetting off to Spain (I can’t think why…) so much easier.
This post was brought to you by two great bottles of beer from MyBreweryTap, who happen to be a customer of mine – and subscribed me to their 52 bottles a year package for free. “A++++ will drink their stuff again!” as people would say on eBay!
Enjoy 2012 readers. I don’t know what things will be like by the end of the year, but I’m pretty optimistic at the moment.