I often find myself imagining applications as physical machines, full of wierd and wonderful parts working together in harmony (most of the time anyway) like some kind of Studio Ghibli inspired masterpiece (/monstrosity). Where a traditional engineer would use cogs, pulleys and levers, we use classes and messages to piece together our creations.
As a kid of the eighties, it is inevitable that one of my favourite childhood movies was the classic Back to the Future. One of my favourite parts was the very start where we get to see the inside of Doc Brown's house including the awesome floor to ceiling guitar amp.
We also see that the rest of the house is littered with exactly the kind of crazy half-baked inventions that I suspect a lot of my apps would like like if they were ever converted into physical machines.
If you're not familiar with the movie, it's probably best to first go buy the entire trilogy and watch it obsessively a few times before continuing.
So now we're all on the same page, what has this got to do with writing code?
Well it turns out that even though Doc Brown, so far as we know, didn't write a whole lot of software we can still learn a few tricks from him. In particular it turns out that the whole trilogy is only possible because the Doc knew a thing or two about loose coupling and managing his dependencies.
With that in might let's have a look at each of the parts in the trilogy to see what lessons we can learn from the mighty Doc Brown.
In part 1, we learn that the Doc has created a time machine. In order to work, his time machine has 2 key requirements that must be met
- The time machine must be travelling at 88 miles per hour
- 1.21 jigowatts of electricity must be injected into the flux capacitor at the precise moment the time machine hits 88 miles per hour
This translates to 2 key dependencies
- A way of accelarating the time machine to 88 miles per hour
- A source of 1.21 jigowatts of electricity
To provide the 1.21 jigowatts of electricity required for the time circuits to do their thing. the Doc builds a mini nuclear reactor into the time machine and steals a stash of Uranium-235 from some not-so-friendly terrorists.
When Marty gets stuck in 1955, one would imagine that nuclear fuel would be kind of hard to come by. Fortunately for him, the Doc has done a solid job of isolating the dependencies of the time circuits. When building a nuclear powered time machine into an 80's sports car, there are a number of options as to how the different parts could fit together. For example it must have been tempting to use the same nuclear reactor to power the car and the time circuits. If he had, Marty would have been pretty screwed. The machine would have been completely dependent on a nuclear power source in order to meet either dependency. The absence of nuclear power would have rendered the whole machine useless and Michael J Fox would never have made it back in time to finish filming Teen Wolf (shame...).
Fortunately the Doc knew better and instead of making his entire system dependent on one specific and hard to maintain component, he separated out the key dependencies and in the process made them as loose as possible. This meant that the first dependency - being able to propel the machine forward at 88 miles an hour - was independent of the second and so unaffected by the loss of the nuclear fuel.
Similarly the second dependency was just that 1.21 jigowatts of electricity was fed into the flux capacitor at the correct moment. There is no requirement as to where this electricity came from or the type of fuel used to generate the electricity. As a result, the impossible task of finding Uranium-235 in 1955 became the relatively trivial task of harnessing a bolt of lightning at exactly the right moment and letting it pass through the Doc's body into the flux capacitor - easy peasy!
In part 2 (ok, technically the end of Part 1), Marty and the Doc travel far into the distant future, all the way to 2015. This time, the Docs loosely coupled system with minimal dependencies yields some pretty juicy performance benefits. Rather than relying on nuclear fuel or fortuitous lightning strikes for the 1.21 jigowatts of leccy, the Doc has had the time machine kitted out with a Mr Fusion home energy reactor. Again, the loose coupling and minimal dependencies mean that the old nuclear reactor could just be swapped out for the newer version as technology improved and everything else continued to function as normal.
Similarly, the first dependency was a way of accelarating the machine to 88 miles per hour. There is nothing that stipulates that this needs to happen using the petrol engine of the car or that the car even needs to be on the ground. As a result, when he upgrades another part of the system - ie gets the wheels replaced with flying jet-car super wheels - it is seemlessly integrated into the rest of system. Imagine how disappointing it would have been to have to land and crank up the old petrol engine just to jump a few years into the future. It would have completely ruined the "where we're going, we don't need roads" line!
Finally in part 3, Marty and the Doc are stranded 100 years in the past in 1885. We fear all is lost but then we remember that the Doc is a pro and his loosely coupled system with minimal dependencies saves the day again.
First off, when the time machine gets struck by lightning (clearly the bad lightning this time, not the good stuff) it fries the flight circuits. Because the Doc has kept these isolated, only the flight circuits are damaged and the time circuits are intact. Even better, because of the upgrades in Part 2, he can still use Mr Fusion to generate the all-important 1.21 jigowatts of juice.
That means all that is left is to accelarate the time machine to 88 miles an hour. The key point here is that it is not even necessary for the time machine to accelarate itself to 88 miles an hour - just that it gets there somehow. That just leaves the simple matter of hijacking a steam train and using that to push the time machine up to the necessary speed. Otherwise, with no flight circuits and petrol being pretty hard to come by in 1885, things would have looked pretty bleak for the BTF boys.
What can we take from this
There are lots of lessons we can take from this movie. I would suggest "don't steal nuclear fuel from angry terrorists" and "if you get it on with your mum you will cease to exist" are two of the most important.
Those aside, we can also see that by isolating the different components of his system and keeping the dependencies minimal and easy to satisfy, the Doc was able to adapt the time machine so that when the environment changed the system could continue to function without having to rebuild everything from scratch. He was also to swap out components so that when newer and better versions came along, they could be fully integrated without too much fuss.