Recently it started playing up, the first symptom was weird colours on screen and vertical colour bars during boot. I ran the built-in hardware test (by booting while holding the D key).
The hardware test had no complaints. (I've seen bad RAM in the past). The clue was that resetting PRAM (parameter RAM) seemed to fix the screen bars briefly. In the end it wouldn't boot at all.
There is a battery to back up the PRAM and I read that the lifespan is about five years. This Mac is now eight years old.
I followed a guide on iFixit and extracted the motherboard, flipped it over, and replaced the battery. It's not the easiest repair job and oddly, the iFixit guide doesn't match my hardware even though the model and date matches. Reading the comments was a help.
Anyhow, here's the battery I replaced.
Now the Mac is working well although unfortunately I can't run macOS Mojave which I need to run Xcode 10.2.
Apple is often criticised for making expensive products but I have to say that features like built-in hardware diagnostics and recovery along with the fact that we've been using this computer for nine years make me think that their products are better value for money and I prefer to think about that rather than simplistic up front price.