Maximize the potential energy of your code

2016-07-04 2 min read

    Potential energy: the energy of a body or a system with respect to the position of the body or the arrangement of the particles of the system.

    Kinetic energy: the energy of a body or a system with respect to the motion of the body or of the particles in the system.

    I’m constantly striving to discover new ways of thinking about code and my latest is thinking about it through what many of us learned in high school physics - potential and kinetic energy. The definitions are above but a simple way to think about it that potential energy is what your system is capable of while kinetic is exercising that option. One can look at code the same way. Code that has a high potential energy can be turned into a vast amount of kinetic energy that can deliver new products and features at an amazing pace. This is code that is well architected and tested and is designed in such a way that it can be easily modified to handle whatever it comes its way. Code with low potential energy, on the other hand, is poorly designed with small changes leading to unintended side effects such that most of the time is spent fixing the code up. The comparison here between a rocket and an old, rickety car is appropriate. The rocket expends the bulk of its energy in minutes and travels hundreds of miles. The car breaks down every couple of miles and requires a skilled mechanic just to keep it going for another few miles.

    But in physics we have the law of the Conservation of Energy stating that energy can’t be created nor destroyed. This also applies to code! Taking code with a high potential energy and quickly modifying to solve a need may reduce its potential energy. In this case it’s up to us as developers to exert effort to bring it back up its high potential energy state.

    This metaphor is an obvious exaggeration but it does strike at what makes for good code and something we should all strive to write. It’s not about being brilliant or elegant or simple but about being flexible enough to support whatever the world throws at it and it’s up to us to keep it at that level. To keep pushing the physics - every new feature adds entropy to our codebase and unless we actively clean it up it only gets worse.