One From The Vaults: Testing

Reading Time: 3 minutes
Looking for Rhino (real ones, not mocks)

Whilst I was looking for something else I found an explanation I wrote years ago to try to explain to senior management how testing had changed and become critical in the Agile environment. It’s not the easiest thing to try to get across: the business wants features that it can sell. I don’t think I did a bad job, so here it is.

Having now spent a few months with this project I’m very conscious that, at the moment, we don’t have much of a testing strategy. It looks as if we’re trying to offload all responsibility for testing onto the testing department.
This rather implies that our primary testing strategy is manual system testing.
Failed system tests are expensive – and if they impact promised delivery dates they can be a major problem. It would be better if we could have a good idea before we entered a system test whether or not it’s going to pass and that is a software quality management problem.

(Technical) testing should start in the software design. The questions the developers should be asking are;

  1. How am I going to achieve this task?
  2. How am I going to test it?

As an example, user interfaces are notoriously difficult to test, but if one of the MVC style design patterns is used it means that the view (the actual UI) can be separated from the rest of the code. This means you can write a program to test the logic behind the UI without having to try to actually click buttons programmatically.

There are any number of places within the design of software where adding a little thought to how it can be (easily or preferably automatically) tested is of significant advantage.
The more automated testing we can get in the better. If we can get to a position where a substantial portion of the code is automatically built and tested every night that would be great. It means that we, as developers, get continuous feedback about the state of the software.
We can then go into a system testing phase with a much higher level of confidence that it will pass.

This does mean however that we need to start investing in testing, which can be a difficult message to get across. If we don’t however then as the software and the functionality grows and the code-base becomes more difficult to maintain we will be taking ever increasing risks with the future of the product.

It’s a good idea to get this baked in now.

I would suggest that a feature is not finished unless the issue of how it is going to be tested is solved – and that means the automated tests and / or the manual tests written. If there’s no automated testing there needs to be an explanation of why.

The more bodges, work-arounds and spaghetti code there is, the harder it becomes to maintain and the longer it takes to develop each new feature. A product which leads the market can very quickly fall behind because every time someone tries to do something they have to try to unravel all the spaghetti, they then inevitably end up piling on more spaghetti just to make it work and making it worse for the next edit. Technical debt snowballs.

As with all things there’s a balance, I’m conscious we need to get features to market fast, but as the product matures the importance of testing will increase. Automated unit testing ensures that each building block of the project actually does what we think it should do. It’s not a silver bullet for solving all technical debt, but it’s a good starting point. Making code testable enforces certain good behaviours that will increase the longevity of the product.

I do not believe that the current development strategy is sustainable. I am therefore intending to phase in a plan that will, in time, see all new development covered by automated testing and will start to retrofit automated testing into the existing code-base. Inevitably this will slow down the speed we can get features to market, by a known and controllable overhead. Our business model is based on repeat business. If we fail to get a grip of technical debt the competition will overtake us in the mid term and it will invalidate that model.

Stop! Thief!

Reading Time: 2 minutes

“That image looks awfully familiar” I found myself thinking when reading an article on vinyl record run-out groove etchings. Then it clicked, it was one of my images, reproduced in someone else’s article without my permission and with no recognition that it was my image.

Image of Vinyl Factory 29 Dec 2017

I’m not a professional photographer, it’s true I do know which end of a camera is the sharp one, but trying to make money out of photos is just more hassle than it would be worth to me. Consequently I’m quite happy for my images to be reused for non-commercial purposes provided that I’m given due credit.

What I do mind however is when my images get re-used on commercial, for profit sites without even so much as an acknowledgement. Even on commercial sites it’s really not worth me pursuing them for revenue (although I reserve the right to do so), what I mind is the fact that it’s downright rude not to credit the original artist.

There’s really no excuse for this – even if someone found the image on a different site a Google reverse image search will very quickly identify its origin. Consequently I have no hesitation in naming and shaming places where I’ve found my images used without my permission and without any recognition.

Original Article Rip Off
A Porky Prime Cut Vinyl Factory
And Twitter
A Porky Prime Cut Two Good Ears
A Porky Prime Cut jack.canalplus.fr
IP Webcam MSN / Auntie Acid
(image and factual content)
IP Webcam Auntie Acid
(image and factual content)
IP Webcam estisuperba.ro

And again (different article)

IP Webcam smalljoys.tv
Goodwood Cobra (racing) conboy.us
Goodwood Cobra (crash) conboy.us
Goodwood Cobra (crash) armeniabirding.info
Goodwood Jaguar D Type (racing) 165.227.181.117
Goodwood Jaguar D Type (rear) 165.227.181.117
Grand Rosela Hotel, adelynndesign.us

Entity Framework Double PK Overwrite Gotcha

Reading Time: 2 minutes

I was writing some unit tests: largely out of completeness I wanted to test that you couldn’t insert two records with the same primary key. The code is simple enough.

    var car = db.Cars.OrderBy(g => Guid.NewGuid()).First();
    var pool1 = db.Pools.OrderBy(g => Guid.NewGuid()).First();
    var driverName1 = Guid.NewGuid().ToString().Trim('{', '}');
    var created1 = PoolAllocatedCar.Create(car, pool1, driverName1);
    db.PoolAllocatedCars.Add(created1);

    var pool2 = db.Pools.OrderBy(g => Guid.NewGuid()).First();
    var driverName2 = Guid.NewGuid().ToString().Trim('{', '}');
    var created2 = PoolAllocatedCar.Create(car, pool2, driverName2);
    db.PoolAllocatedCars.Add(created2);

    Assert.Catch<Exception>(()=> db.SaveChanges());

Car Id is the sole primary key of the PoolAllocatedCars table.

Being bit lazy I guessed it was quicker for me to run the code and find out what exception SaveChanges() it threw rather than trawl through the docs and work out what it should be.

The problem: it didn’t throw an exception. So I added some debug to find out what happened, the result is disappointing to say the least.

---===*** FAILED TO NOTICE PK CLASH ***===---

In Memory:
    Car Id [24] Pool Id [49] Driver [bda1d05c-8dae-4648-ab42-736eb8c44b71]
    Car Id [24] Pool Id [08] Driver [9dae73a0-5b8d-45bc-9d0a-f8b73141aa2c]

In Database:
    Car Id [24] Pool Id [08] Driver [9dae73a0-5b8d-45bc-9d0a-f8b73141aa2c]

It would appear that Entity Framework simply overwrote the first record with the second without giving any indication that there was ever a primary key clash.

Now I’m sure that somewhere in the documentation there’s a warning or a note about this but I haven’t found it yet…

Update: It Gets Worse

I was taken aback by the above, that it could be deemed acceptable to treat the explicit addition of a second object with the same primary key as an implicit update with no warning to the user.

I guess then I shouldn’t have been surprised that it even does this after an explicit call to SaveChanges()

The following test gives exactly the same result as the first. This is a massive gotcha.

    var car = db.Cars.OrderBy(g => Guid.NewGuid()).First();
    var pool1 = db.Pools.OrderBy(g => Guid.NewGuid()).First();
    var driverName1 = Guid.NewGuid().ToString().Trim('{', '}');
    var created1 = PoolAllocatedCar.Create(car, pool1, driverName1);
    db.PoolAllocatedCars.Add(created1);

    db.SaveChanges();

    var pool2 = db.Pools.OrderBy(g => Guid.NewGuid()).First();
    var driverName2 = Guid.NewGuid().ToString().Trim('{', '}');
    var created2 = PoolAllocatedCar.Create(car, pool2, driverName2);
    db.PoolAllocatedCars.Add(created2);

    Assert.Catch<Exception>(()=> db.SaveChanges());

The good news is that if you use a different DbContext it throws an exception :- in fact on the Add, not the SaveChanges.

I’m having trouble getting my head around this: I can’t see the logic. If you thought there was a chance that you’d want to update an object after you’d added it to the table then you should keep your own reference to it. If that’s a problem because of scope then your design is probably wrong.

The Jet Set

Reading Time: 4 minutes

I really genuinely hate air travel. I will do pretty much anything I can to avoid it. I’ve taken a 16 hour train journey from Suffolk to Barcelona to avoid a 3 hour flight. It’s not that I’m afraid of flying, there’s just something about the whole experience that I find fundamentally unpleasant.

South Africa however is a bit of a trek. Personally I’m completely up for a McGregor/Boorman type epic, but there are certain logistical problems. Mainly not having anything like the spare cash that Ewan McGregor has. Or time. He does seem to have a lot of spare time.

Consequently I’m currently sat on a plane and I’ve been here a while…

There’s never anything on the entertainment system that I want to watch. I have no idea why. It could have my favourite show on there and I still wouldn’t want to watch it. I’ve gone through it all, pawed the duty free catalogue, scanned the in flight magazine and read the safety instructions twice. It’s half past midnight and I am still quite unreasonably awake. It’s at times like this when I conclude that it’s a good idea to listen to Pink Floyd’s Atom Heart Mother.

I’m almost coming to terms with “Alan’s Psychedelic Breakfast” when I suddenly become aware of something plasticky in front of me. I’m just about to “put it over my mouth and breathe normally” when my nose informs me that it’s curry. I conclude that I could in fact eat, not because I’m hungry but because I’m bored and I’m hoping that redirecting some of the blood supply from brain to stomach might help me at least doze.

To this end I order wine too, “Cabernet Sauvingon?”, I enquire. “No, but we’ve got Sauvingon Blanc”, comes the reply. “No thanks,” I interject, “Cabernet Sauvignon is a red” but it’s too late and an open mini bottle of white wine lands on my tray table. I note that is a Marlborough and decide that it’s better the devil you know.

Nevertheless my plan works and half an hour later I’m in the land of nod.

I awake to the rather uncomfortable realisation that I ordered Asian Vegetarian food on this flight. I usually do because it seems more difficult for airlines to murder curry then it does any other type of food. Unfortunately breakfasts can get interesting as I’m about to find out when I’m served bland, generic cheese, coleslaw and sort of bread that tastes like dehydrated semolina pudding.

On the bright side I conclude that breakfast service must mean that I got a good few hours sleep and that there can’t be that much time left on the clock. I don’t dare look however.

I fire up my music player and decide to give the Amaranthe album I curiously downloaded on Spotify a listen. The first track is good, but I very quickly conclude that although Euphoric Dance / Death Metal crossover might sound like an interesting and challenging concept, right here, right now it’s just very, very annoying.

Thankfully the entertainment system has some Chopin and my eyelids are starting to feel heavy again.

There’s an eerie silence when I wake and another strong smell of curry. Chopin has finished and my noise cancelling headphones are busy cancelling out the drone of 4 enormous GP7000 series jet engines.

I lift the lid on the tray in front of me: the airline chefs have surpassed themselves this time and reached a whole new level of achievement. Somehow they have managed to screw up a paneer curry.

Fortunately there are enough other dishes to make a meal. There’s even some spare cheese, just in case cheese in lightly spiced cream sauce wasn’t enough dairy for one meal.

By the time I’ve obtained a cup of tea I’m feeling brave. I’ve had two good sleeps and three bad meals. We must be nearly there, right?

3 hours to go. There’s nothing out the window but cloud. The aircraft cameras are showing nothing but cloud.

Now all we need is some… whoa! Where’s the floor gone? Is that kind of jolt within the tolerances this plane was designed for? “A bit bumpy”, why yes Captain I think we’ve all noticed that. There was a small hint in that if I hadn’t been wearing my seatbelt I’d be sitting in the overhead locker right now.
I then spend the next few moments marvelling at how, firstly, relatively minor atmospheric conditions can hurl a 500 tonne plane about like a piece of confetti and secondly what a superb piece of engineering it is that a 500 tonne plane can be hurled around like a piece of confetti without the wings falling off.
Now however the baby is screaming and the bloke next to me is pale as a ghost and clinging on to the arm rest for grim death.

I try to think of some words that might be of comfort to him, but everything I can think of either makes me sound like a smug git or contains the word “plummet”. I do however make a note to stop jokingly referring to our destination – a well known London airport – as “Splatwick”.

Still more than 2 hours to go but on the positive side, I’m starting to wonder if there’s some mileage in trying to write a blog article about why I hate flying so much…

A Tender Subject

Reading Time: 7 minutes

I’d barely got into the corridor before I found myself being bundled into the wall by a rotund sales guy. “What the fuck do you think you’re doing?” he barked in my face, “If you ever pull a stunt like that again I’ll making fucking sure we put you out of business. Consider this a warning.” he stuttered a little and I could see the fear in his eyes: he was not just angry, but terrified. “And I don’t give second warnings. Understand?” he eventually added and with that marched angrily away.

I’d got maybe 15 metres further before another salesperson came up beside me and sniped, “If that’s how you’re going to behave you can forget ever partnering with us.” before he too shuffled off.

What had I done to deserve such attention? Insulted their mothers? Murdered a puppy? No, just give a sales presentation.

One of the way the UK government keeps track of technology is through seminars, they take various forms but this was essentially a day-long event where vendors of a particular type of technology were invited to present their products to a bunch of interested parties within government.

Somehow – despite being a tiny little firm – we’d managed to blag our way onto the list. I’d been to these events before, as the “tech guy” that answers the difficult questions so I knew the format. What I hadn’t done before was actually sit through a whole day of them.

They were dismal. The scripts were poorly thought out, badly structured, omitted important details and contained much that was irrelevant. The presenters were worse. Monotonous at best, incoherent and rambling at worst. By the second or third presentation I was genuinely ashamed that this is supposedly the best of what my industry can offer.

By the second session after lunch half the audience were asleep and I was reasonably convinced that the other half were dead.

When we eventually took to the stage my opening words after introducing myself were “I’m sure you’ll be delighted to hear that this is the only Powerpoint slide I’m going to use.” I then talked for a few minutes about who we are and what the product was whilst my colleague set up a fully working system. We put it up on the projector, handed our various devices to the audience and spent the rest of our time role playing live scenarios that demonstrated the product features.

It was a bold move and quite a risk, there were some very senior civil servants in the room who could have taken umbrage, but they didn’t, they did the exact opposite. At the insistence of one of them our slot was extended to allow for an additional 5 minutes of questions. My colleague and I both ran out of business cards in seconds and took to writing our details on the backs of other peoples. From a sales point of view it could not have gone better for us.

That’s all I’d done. I’d hadn’t bad-mouthed the competition, said anything that was untrue or in any way behaved unprofessionally. We turned up, we told jokes and tried to be as engaging as we could whilst demonstrating a live system that could be set up in less than 5 minutes.

That is the kind of behaviour that the competitors needed to stop. For 30 years they’ve been telling government that software systems are big, complex and expensive. That they take years to develop, months to install and configure and that they require huge, expensive racks of equipment.

Then two guys turn up with a laptop and a bag of kit and prove that none of this is true.

The issue is more complex than one of straight deception, though. These organisations were formed at a time when these things were all true. The organisations themselves have grown to provide a heavyweight framework. They have little momentum and colossal inertia.

They also have huge problems with legacy code-bases. The requirements of government tend to evolve slowly, hence re-writes are rare. Instead old code lumbers on and on and velocity nosedives as technical debt increases [more on that in a minute] and new development techniques, tools, frameworks and languages accelerate the pace that can be achieved within the industry in general.

These factors have reciprocated a framework within government that has grown to expect and indeed facilitate it. The tendering process results in excessively detailed documents that emphasise the minutia and neglect the actual business requirement for the system. These are often produced by committees and are stitched together from requirements drawn from multiple sources – or in other words they’re actually just wish-lists and can at times can be incoherent.

This is a thoroughly unhealthy relationship, it’s not just wasteful, it’s actively counterproductive. It stifles technical innovation and guarantees mediocrity.

More than that though it damages the engineering quality of the products as developers rush to strap on extra features and to modify existing ones in order to meet the letter of the requirement where in reality the product was designed to solve the overall business need and does so perfectly adequately.

This is often a panic scenario too. In order to gain a higher score on the tender a business will commit to what is it at the absolute limit of what it can achieve – often a little more than. The result is the in order to meet the delivery, development have to cut every corner possible. Inevitably this piles on the technical debt.

As the development team crashes from tender to tender its velocity plummets, the business throws more developers at the problem and you end up where we are today. The products are bloated, resource hungry, hideous to configure, often still include archaic technologies that (now) require special environments and are generally impossible to demonstrate anywhere but a dedicated, static suite specifically for the purpose.

That is why 2 people turning up to a sales seminar with a laptop and a bunch of hand-held devices and doing a fully interactive live demonstration caused such a furore. Yes I do believe that everyone else there on that day was at least one of: incompetent, phoning it in or just plain burnt out. None of them however could have done a live demonstration even if they’d wanted to.
To the competitors watching it must have seemed like we’d taken the bar for a sales presentation, which was set barely higher than the crash-mat, and smashed it through the roof. There was no way they could compete.

The reality however is that we weren’t a real threat to them. If we wanted to win tenders then we had to tick boxes. We could undercut them only a handful of times before the same thing happened to us as happened to them 30 years ago. Thankfully we recognised that and stopped responding to additional tenders.

I have to admit that I don’t know what the answer is. As a business or an individual one can make purchasing decisions however we like. If we see something that doesn’t meet what we thought our requirements were, but if we thought about the problem in a different way is actually a far better solution, we can buy that and change the way we work.

Government is different, a democracy must always be accountable to the people. The government must be able to justify why it chose supplier X over supplier Y. A qualitative response is always going to be open to challenges. It is far more robust from a point of view of the accountability of government to take a quantitative approach: supplier X was able to meet more of the requirements than supplier Y. It is also more legally robust, many of the suppliers are large companies that are not afraid of litigation if they believe it might give them a competitive advantage.

One could perhaps say that this adds weight to the argument that the government is trying to do too much and that many of these key areas should be privatised, where a more effective purchasing strategy could be implemented. This however has its own problems: ultimately it’s still public money and all this is doing is to move the point of accountability higher up. The same end could be achieved by just increasing the threshold at which a tendering process is required.
I’m also not particularly keen to live in a country that has a private Police Force and I rather suspect I’m not alone.

The counter-argument is that all such systems should be developed in-house by the government itself. In an ideal world this would be the answer. Unfortunately the world isn’t ideal and the list of reasons why this is a terrible idea is really rather long.

However we spin the model and try to look at it a different way, it’s difficult to get away from the idea of a tendering system that’s essentially scored quantitatively. There have been schemes to try to improve the situation such as to get people from the software industry in ahead of the tenders being written, however their influence has to be carefully managed in case of any allegations of impropriety and they are inevitably associated with the very organisations that will be responding to the tender. Thus this is has not proved and effective means for improving the situation.

I rather dislike finishing an article without at least suggesting something that I think might improve the situation, but I have to admit that I’m struggling with this one.

—=== END ===—


I’ve had a number of comments since writing this article. I can split them largely into 3 categories.

The saddest is the number if people who’ve also reported being directly threatened by larger firms within the industry.

I hadn’t realised how much technical jargon I’d used. Basically “velocity” is the speed at which you can make changes to a product and “technical debt” is what happens when you keep making changes without the proper procedures:
eventually you end up with a big bird’s nest and it gets harder and harder to make changes and it all gets more and more out of control.

A few people within the industry have commented that I’ve made some generalisations and simplifications. This is true, it’s a trade-off between writing a fastidiously accurate article and writing one that makes the point sufficiently well but is engaging enough to appeal to a wider audience.


Tom Fosdick is a Software Architect specialising in Control Room systems for the Emergency Services. This article refers to an event that happened in a previous role.

Tanya And Tom Do Racing

Reading Time: 5 minutes

I have been known to point a camera at a thing – quite often that thing is a racing car. I do it as my own personal challenge but other people seem to like the results, which is one of the reasons behind the TanyaAndTomDoRacing Instagram account.

I do find however that I get asked two questions quite a lot and as Instagram isn’t really the place for a FAQ…

You Must Have Some Really Good Kit?

The direct answer is no, I don’t. In fact this is a pet peeve of mine, not just in photography but in many hobbies.

If you have a good eye, enough knowledge, skill and technique then any entry level major brand Digital SLR camera and many bridge cameras are perfectly capable of taking very good photos – in many cases professional quality photos.

The key about a Digital SLR camera is that it puts control of the optics and the sensor into the hands of the operator, allowing the photographer to make artistic decisions about how the light is captured.

This is one of my favourite (but not technically best) photos, taken at Goodwood Revival in 2015.

It was taken with a Nikon D3000 and a Nikkor AF-S 55-300 F4.5-5.6 D G VR DX lens: entry level kit. What makes the photo is the framing, the choice of subject, the angle to get the reflected sun, the 1/25 shutter speed and a big chunk of luck with the panning.

You can’t compensate for deficiencies in any of those things by throwing more money at it.

Yes, I would have liked a higher resolution sensor and in an ideal world I would probably have used a lower ISO value and larger aperture but I could easily pay 10 times as much for kit to get a photo that is only marginally better.

Digital SLRs are not supposed to be smart, they’re not about image processing and fancy modes to try to make something out of nothing, they’re all about controlling how the light falls on the sensor. There’s only so much that clever electronics and software can help and the best of that is often found not in SLRs – where it might actually degrade the photographic quality – but in compact cameras.

Here’s a case in point:

I like this photo, but it’s not one of my favourites. Taking the standard sharp foreground, blurred background photo is like shooting fish with a Digital SLR.

This photo wasn’t taken with a Digital SLR though, it was taken with a Sony DSC-HX50, a £200 compact camera.

It’s possibly my favourite ever camera, simply because of the amount of power it puts in a genuinely pocket sized piece of equipment.

It does have modes to put optical control in the hands of the user (as seen above), but what you can do is limited – mostly by the physical size of the device. To compensate for the fact that there simply isn’t room for better optics it’s stuffed full of electronics and software.

This shot, for instance, was taken hand-held at a beach cafe somewhere in Indonesia using all the electronic aides available (and needing them).

In an ideal world I would have had a Digital SLR mounted on a tripod and I could have taken a slightly better picture. In an ideal world the person in the picture would be a model who wouldn’t have moved.

The problem is that the person in the picture isn’t a model, she was one of the tour group and I couldn’t really ask her to hang about whilst I set a tripod up or stay still whilst I took the photo. The moment was there, I was able to capture it specifically because I had a compact camera with a night mode that I could select in a second.

The bottom line is this: you can take really good pictures with a sub-£100 camera. The problem is that the capabilities of that camera will be limited, which will limit where, when and the type of picture you can take.

If you spend a bit more, around the £200-£300 bracket you can get cameras that are far more capable and put a good amount of control in the hands of the photographer.

Above that you hit the wall of diminishing returns pretty hard. Between £100 and £200 there’s a huge jump in capability and quality. Between £200 and £300 less so. When you get above that then you can find yourself paying an awful lot of money for very little improvement and you have to ask yourself whether it’s really worth the money.

If you’re thinking of turning professional or if you spend every waking, non-working moment dreaming about photography then fine, maybe it is. For most of us it’s not.

But You Should Sell Your Photos / Turn Professional!

I’m flattered, sincerely and genuinely that some people think my photos are good enough to make money out of, but the reality is that they aren’t.

OK, some of them are and course if people want to pay me to use / print them then we can talk about that… the point is that it’s not worth me actively pursuing trying to sell them.

As I said above, taking the standard “sharp foreground, blurred background” racing car photo is shooting fish in a barrel with a Digital SLR – anyone with any understanding of the principles of photography can do it.

It used to be difficult because with (wet) film you had no idea how good the photo was until you developed it. You can’t do that at the track-side. That means you needed a lot of experience to know exactly what settings and techniques were required because you were shooting blind.

With a Digital SLR you can press the shutter and have the image on a HD tablet within a couple of seconds. You can closely analyse the picture, work out what needs changing and then go again with an improved set up within a few seconds.

At any Formula 1 Grand Prix there must be at least a thousand people doing pretty much that and a good portion of them will be producing shots as good as mine or better. There are only a handful of places you could sell the pictures to and they’re all looking for something that is not just technically good, but has something extra that makes it stand out.

Perhaps something like the first picture in this article, but with a little more sharpness on the helmet and without the smear in the bottom left caused by the head of some bloke who wandered into shot.

I have thousands of photos in and around race tracks and other motorsport events. I have only a handful of photos that I genuinely believe anyone would pay money for.

If you want some idea of the differences then look at the Instagram streams of the professionals:

James Moy @f1photographer
Lollipop Magazine
(Joshua Paul)
@lollipopmagazine (my favourite)
Peter J Fox @peterjfoxy
Sutton Images @suttonimages
Darren Heath @darrenheathphotographer
McLaren @mclaren
Ferrari @scuderiaferrari
Williams @williamsmartiniracing
Red Bull @redbullracing
Toro Rosso @officialtororosso
Force India @forceindiaf1
Renault @renaultsportf1
Sauber @sauberf1team
Haas @haasf1team
Mercedes @mercedesamgf1
Prema @prema_team
Racing Engineering @racingengineering
Russian Time @teamrussiantime
ART @artgp_official
DAMS @damsracing
Campos @camposracing
Trident @trident_team
Rapax @rapaxteam
Arden @ardenmotorsport

 

 

My Favourite Little Friend

Reading Time: 5 minutes
Lily Asleep in the garden

[originally written February 9th 2017]

You know before you step out the door. You’re going to the rescue centre, there will be something small and cute there and you will fall in love. You might tell yourself that you’re just going to look, but you’re not. You’re going there because you want to fall in love, you want to share your life.

You also know that one day it will end. The dog that jumps up to greet you after a hard day’s work that somehow makes it all better. The cat that sits and curls up on your lap and just purrs gently when everything seems lost. One day they will die. You know this, but when you’re stood in the rescue centre trying to work out a way of leaving without promising to take all of the animals you don’t accept it. You’re choosing a new life companion and in your head that companionship will last forever.

You know however as time passes that the day it must end is coming closer. When my cat Lily was about 14 I saw her laying motionless on my lawn. I found myself thinking that if she’d passed away I wouldn’t be so upset. 14 is a good age, she’d led a good life and right up until that day she’d been scampering round like a kitten. Yes, this was a good way to die, to find a sunny spot on the lawn on that warm spring day, to curl up and drift off into the big sleep just as she’d drifted off into any other sleep.

I crept closer to see if I could spot any sign of life. As my shadow fell over her I heard the faintest “mip” and she rearranged her paws. She was fine, just far more deeply asleep than usual.

I smiled and carried on walking, but that image stayed in my mind. If that’s how it happened, if one day I just found her curled up having just gone to sleep never to wake up, that would be OK.

Barring a miracle, that is not how it’s going to happen.

Looking back she wasn’t quite right this summer. We used to lose her in the summer, she  rarely came into the house. She’d be out prowling or sleeping either in the garden or in the nearby fields. She’d get sun-bleached, by late August she’d not be a black and white cat, more of a sort of brown stripy cat, not that we really had a cat you understand – she was nature’s child, wild and free.

This summer she spent a lot more time with us. We just put it down to old age and maybe not being as sprightly as she once was. As Autumn became winter however we noticed that she was losing weight fast. We tried a few different foods but none of them seemed to make a difference so we took her to the vet.

Initially all seemed fairly bright, there can be any number of reasons for older cats losing weight and many of them are treatable. As time progressed however there were more test results and we noticed more symptoms and the field of diagnosis narrowed. Sadly it was the benign things that were being eliminated. We’re now in a world where all probability points to her being terminally ill. We’re still not quite sure what the root cause is, but the weight of evidence points in that direction.

I hope that she isn’t in too much pain. Right now she doesn’t seem like she’s in too much pain so I still have that dream, that I walk out into the garden on a warm spring day to find her curled up in the sun, having shuffled off this mortal coil in the gentlest of sun and lightest of breeze.

The reality however is far more brutal. At some point she will be in pain. At some point the pain will be too much. At some point it will be better for my Lily, my dearest companion of the past 16 years if she were no longer here.

And I’m sat here now with tears streaming down my face as she walks all over the keyboard and rubs her cheek against mine.

At some point I will have to kill her.

I am terrified.


I wrote this post a few months ago and I didn’t publish it because at that time I thought it was – well – just too sad. It felt like I was writing her obituary long before she had died.

A few days ago Lily came to see me whilst I was asleep, she curled up and went to sleep next to me. For the first time I can remember when I woke she was still there. I dearly hoped that’s what I’d be able to tell you, that she’d died whilst peacefully asleep next to me. Sadly, as I predicted, that’s not how it was.

She had a brief spell where she seemed to recover a bit, but it soon became clear that it was temporary. She continued to become evermore frail. Over the past 2 weeks however it started to become noticeable that she wasn’t moving or even standing in the same way she used to. Her life seemed to consist of long periods of sitting on her favourite rug staring into space, going to the food bowl, going to the litter tray and coming to us for affection and comfort.

Reluctantly we concluded that by continuing to feed and care for her we were extending her life beyond its natural limit. As it was now clear that she was in pain we decided that to continue to care for her without there being a dramatic medical intervention was unethical.

We visited the vets last Friday and we were not able to come up with any medical solution that would have any reasonable prospect of giving her any further valuable life. We therefore took the decision to end her life in the most peaceful way possible.

We buried her in the garden on Saturday, in one of her favourite spots – almost exactly where the above photo was taken. Currently the spot is marked by a white lily plant. We say hello to her every time we go to work. She’s still with us, still part of our story, still part of who we are.

Rest in peace Lily, 17/04/2001 – 21/04/2017

[original artwork by Emma Green]

Never Work With Scientists or Engineers

Reading Time: 2 minutes

Forget never working with children or animals. Never work with Scientists or Engineers.

Picture the scene, dear reader; the air is being turned blue with the kind of language usually reserved for the rugby scrum. Things are being thrown.

A calming voice comes from a distant room enquiring what the problem might be. I respond using “language” that mounting the picture in the frame has not gone as well as I had anticipated, it’s not quite central. I may have used the word “disaster”. I can’t recall what the other words were but I seem to recall them being quite short and decidedly pointy.

“But you can fix it,” comes that calming voice again, “you can fix anything.” I take a deep breath, think for a bit and conclude that yes, I can fix it.

There then follows a few minutes of calm and concentration before once again the demons are unleashed, the air turns blue again and more everyday household objects find themselves travelling at unnatural speeds towards various hard surfaces.

“Problems?” enquires the calming voice.

“Yes, ” I reply. You will understand of course that my actual reply was somewhat longer than this but there are international treatise preventing me from disclosing the full transcript. I then continue to explain the cause of my frustration, “have you ever tried to cut 1/4 of a millimetre off the side of a piece of A4 card? It’s impossible!”

It was at that point it hit me, I had a steel ruler on top of a piece of card, both clamped to a cutting mat and I was using a razor blade to trim off a tiny amount to correct an error that only I was ever going to know was there. It was pointless, I should have just put the card in the frame and forgotten about the tiny mistake.

That 1/4mm level of passion, that level of attention to detail is entirely the sort of thing that’s required if you’re an engineer that is, say, responsible for ensuring that 999 (911 ,112)  calls get dealt with correctly (which I am). When mounting a picture however it’s more like an anti-skill, a trait that does more harm than good.

Intlerocked.Exhange and the Atomic Option

Reading Time: 4 minutes

Developers are now having to deal with concurrency issues far more than we ever have in the past. Our languages are evolving more and more features (like the Parrallel.* class in C#) that allow us to take advantage without too much pain.
These are great, but they’re not always the answer and they don’t negate the need to understand the fundamental issues of concurrent programming. This is in fact the very subject that this blog started with several years ago...

Today I’d like to talk about Interlocked.Exchange. Its purpose it to exchange two variables in an atomic way. It’s the atomic nature of the exchange that’s the important factor here, it means that nothing can catch the exchange in an incomplete state. It’s either completely one value or completely another value.

Thread Safe Exchange

The significance of this becomes evident when we consider the traditional method of exchanging two variables.

var spare = first;
first = second;
second = spare;

This is not thread safe. The problem is that a second thread could interleave into the first. Consider the following path of execution. Let’s assume the value of first is 5 and the value of second is 10. The table shows the values of the variables as two threads interleave.

Thread Code first second spare 1 spare 2
1 var spare = first; 05 10 05
1 first = second; 10 10 05
2 var spare = first; 10 10 05 10
1 second = spare; 10 05 05 10
2 first = second; 05 10 05 10
2 second = spare; 05 10 05 10

Oh dear… that didn’t work, did it?

If we’d used Interlocked.Exchange we wouldn’t have the problem, because the exchange is atomic no interleaving can take place, the first thread will finish then the second will take over.

Thread Safe Changes of Scope

When it gets really interesting though is the fact that Interlocked.Exchange returns the original value.

public static T Exchange(
    ref T location1,
    T value)
    where T : class

The beauty of this is that we can use this to change scope. There’s a wholly unsafe pattern that gets used in Dispose handlers all the time:


class Blah: IDisposable
{
    SomeDisposableType someObject;

    ...

    public void Dispose()
    {
        if(someObject != null)
        {
           someObject.Dispose();
           someObject = null;
        }
    }
}

It’s possible that two threads could interleave between the null check and someObject being assigned to null, resulting in someObject being disposed twice.

Alternatively…

public void Dispose()
{
    var myCopy = Interlocked.Exchange(ref someObject, null);
    if(myCopy != null)
    {
        myCopy.Dispose();
    }
}

What the Interlocked.Exchange does here is to set someObject to null in the object scope and return its (former) value to myCopy which is in the method scope.
If two threads call Dispose at exactly the same moment, then both of them will succeed in setting the value of someObject to null. In the case of the thread that calls Interlocked.Exchange first, it will return the original value of someObject to its myCopy and set someObject to null. When the second thread calls Interlocked.Exchange it will return the value that the first thread set someObject to, that being null. It will then proceed to set someObject to null again.
The effective is that someObject is set to null twice, but only one of the threads gets the original value of someObject, so only one will pass the null check and only one will call Dispose on someObject.

Note that this isn’t a good Dispose pattern fullstop however. Microsoft have written some guidelines.

This scope switching trick can be useful in other places too. Consider if you’re writing a log file of some description. Writing an ever-expanding log file causes problems, it’s good to have a cut-off and write a new one every so often. A common method is to write one file per day of the week.

public class LogWriter : IDsiposable
{
    StreamWriter writer;
    public string FilePath {get;set;}
    ...

    public void WriteLog(string s)
    {
        writer.WriteLine(s);
    }

    public void StartNewDaysLog()
    {
        var newWriter = new StreamWriter(FilePath + DateTime.Now.DayOfWeek.ToString() + ".log", false);
        var oldWriter = Interlocked.Exchange(ref writer, newWriter);
        if(null != oldWriter)
            oldWriter.Close();
    }
    ...
}

With this implementation multiple threads can safely call WriteLog continuously. When StartNewDaysLog is called a new StreamWriter is set up ready to go, then the two are switched in an atomic fashion. Nothing can catch this out half way through the switch – as far as anything calling WriteLog is concerned one entry was written to one file and the next to another: it’s seamless.
After the switch, StartNewDaysLog is left with the old StreamWriter which it then has to Close (which in turn calls Dispose).

Conclusion

Interlocked.Exchange is a surprisingly useful little tool. Its plain usage – to simply exchange a value in a thread safe way is useful, but where it really comes in handy is in its ability to replace a value and return the original one into a narrower (thread safe) scope. This is particularly handy if you need to move or consume something in a simple way that doesn’t imply graduating to a mechanism such as ReaderWriterLockSlim or SemaphoreSlim.

Callback Trace Listener

Reading Time: 2 minutes

lightSo you know about the Trace facility in System.Diagnostics, right? If not then you should because it’s really rather handy. It provides a set of static methods that you can call to write out trace information to any number of connected “Listeners” which you can define in code or in config.
The default trace listener writes to the Win32 OutputDebugString stream, you can view this in Visual Studio’s “Output” window or using a viewer such as Sysinternals DebugView.

Sure if you need more extensive logging and tracing then you should look to something like log4net, but if your requirements are simple then System.Diagnostics.Trace and System.Diagnostics.Debug are really handy.

Anyway, that’s not the point of this article.

Quite often I end up writing non-production apps – little gizmos for this or that – which could do with informing the user of what’s going on via some sort of rolling log on the UI.

So I wrote this tiny little class to help.

public class CallbackTraceListener : TraceListener
{
    public event Action<string> WriteToOutput;

    public override void Write(string message)
    {
        if (null != WriteToOutput)
            WriteToOutput(message);
    }

    public override void WriteLine(string message)
    {
        Write(message + Environment.NewLine);
    }
}

It’s a Trace Listener just like the one that writes to OutputDebugString. You need a little bit more code to make it work, in the initialisation of the your application you need to register an instance of it as a trace listener…

public MainWindow()
{
    var uiTracer = new CallbackTraceListener();
    uiTracer.WriteToOutput += UiTracer_WriteToOutput;
    uiTracer.Name = "UITraceListener";
    Trace.Listeners.Add(uiTracer);
    Trace.WriteLine("Debug window initialised");
    Trace.WriteLine("Ready...");
    InitializeComponent();
    ...
}

Now every time you use any of the Trace methods that write output, the UiTracer_WriteToOutput callback will be called with the string that’s written. You can add the strings to a ListBox, or put them through any other kind of processing you want.
It’s a really simple and useful way to subscribe to up your own Debug/Trace stream.