My car is poorly. Yesterday there was a “big metallic bang”, according to my wife, and then it started “clanking”. Gotta love these technical people! The guys at Kwik Fit soon diagnosed the problem: the front passenger side coil spring had snapped. Great, more expense. And all due to the state of the roads. Thanks, local council.
But one thing the Kwik Fit bloke said interested me. Looking at the broken spring he commented how it was good the car manufacturer had started putting a plate at the bottom of the spring, as in days gone past springs would snap and go through the tyre. That would, obviously, have caused a serious accident. But the spring fortunately broke in a safe way, and I’ve got a reasonably drivable car.
When writing software we have to do the same thing. We code for the possibility that bits will break, to protect other bits and the application as a whole. There are a number of ways we do this, here’s a quick list of the ones I can think of.
- Ensure that if we’re going to use a variable, it is set
- Check the type of variables: if a variable must be an int then make sure it is an int
- Checking whether we need to do an operation at all, for example not looping a collection if there’s nothing to loop
- Checking a collections length before trying to get an item with a non-existent index
- Catching exceptions
- Providing meaningful error messages
- Persisting form information so users can try again if their submission doesn’t work
- Checking variables are within the required range, for example validating a birth date
And there are probably loads more, including ensuring that the UI looks and functions reasonably, even if the user doesn’t have the latest, greatest browser.
With all of these things we’re aiming to ensure that if something breaks – and it will, let me assure you – it doesn’t cause an accident. Car manufacturers have figured this out, and rightly so as they have a responsibility towards the safety of road users. I don’t want to think of how many tires were blown before they added those safety plates.
Here’s an old, but true, saying; an ounce of prevention is better than a pound of cure.