Mind of State State of Mind

It is a joy to experience software that just works. Quality, bug free software is hard to pull off and yet we are spoiled rich with more and more cloud services and apps that just work. Certainly, this is not an atomic outcome. I mean we didn't just wake up one auspicious morning and began writing perfect software.

What makes software hard are 2 things. The inherent nature of software we build and the inherent nature of the operating system that runs our brain. Let us explore a little more.

Essential complexity

Software is complex primarily because it deals with states (e.g the switch to control the fan can be in 2 states: “on” or “off”) and state transitions (e.g an “on” switch can be turned “off” and vice versa - giving us 2 state transitions). Software is always changing. Users demand features all the time. These causes software systems to gain complexity over time.

Consider a simple software component that takes 3 single digit inputs and has 1 output. We are dealing with 10^3 or 1,000 possible states. What happens when we add 1 more parameter to the input? Complexity increases 10 X and we now are at 10^4 or 10,000 possible states. This is staggering ! If we throw in more inputs, more interactions and for good measure, add real world concerns like concurrency, special cases etc, the situation deteriorates beyond repair. Complexity as measured by number of states overwhelms everything else. In the wake of complexity which is invisible, many visible aspects we worry about all the time become irrelevant. This includes tech stack, project methodologies, hiring process, hard work, noble intentions, motivated engineers etc. System complexity trumps 'em all.

Growing complexity is the reason why software often crumbles under its own weight. The question is not "if" but "when" this collapse would occur. Any useful software mutates many times over during the course of its useful existence. The more features we add, the more complexity we have to fight against. And the function mapping the two is exponential. If complexity is left to proliferate on its own, our systems will soon be infested with un-verified behaviour and hard to track down bugs rendering it ineffective and unusable. This is how you achieve the opposite of software that just works.

Darwin Operating System

Now you may shrug all this off as excessive paranoia. If so, congratulations on being human! Your mind's operating system is working really well. The OS that runs the human mind is incredible, very effective but also very primitive. Sure, there are updates and incremental improvements which are brought about largely through trial and error. But these have taken place on a geological time scale over millions of years. These upgrades are delivered through the un-agile methodology of natural selection. Lets call this operating system - Darwin OS. Large parts of this OS were designed and optimised for the needs of our ancestors and their primordial contexts from millions of years before.

Darwin OS cannot deal with more than an average of 7 pieces of information at a time. Apart from being single threaded, its "in-built math module" falls short when dealing with very large numbers, very small numbers, probability, imagining exponential growth, combinatorial mathematics etc. Darwin OS is littered with bugs and has many blind spots. Its worst aspect is that it lies to us with a straight face when computing gets even mildly demanding. Its trade offs are designed to always favour strategies that use lesser resources over those that yield accurate and precise answers.

This design is perfectly logical for an OS that is primarily concerned with maximising chances of survival of its subject. They are not just not designed to excel in modern endeavours. No surprise then, for example if a developer or a project manager has completely ignored complexity or grossly mis-estimated time and effort for the tasks in hand on the basis of gut feel. Natural selection is perfectly ok to let Darwin OS get away with fuzzy and embarrassingly poor approximators.

How to get real ? - The mind of state - Darwin OS's "hotfix"

Assuming we cannot wait a million years for Darwin OS's scheduled updates, we need to take action by deliberately patching its buggy modules on our own. How do we do this while making software? During planning, creating and testing phases, we need to actively block gut feel, put pen to paper and work out "un naturally" and in cold blood reason about all states and transitions our software could go through. This process often involves applying combinatorial Algebra we all had to endure back in school. This unnatural thinking is the "mind of state". Essential to get a realistic sense of the invisible monsters we are up against.

Alt Text

Every time I look at my phone's settings above, I am overwhelmed by its complexity, because the mind of state calculates in the above figure at least 2^13 = 8192 possible states the device could be at any moment in time. The mind of state marvels at the complexity of the OS that runs my phone and the sheer volume of tests (does Bluetooth work in Airplane mode while Super Power is on?) this phone would have gone through, before it was put in the hands of its first user.

While thinking about software, it pays to re-program our brains through deliberate application. It pays to acquire a "mind of state" and make this the state of mind before we embark on any long battle against complexity . This mind of state does not feel natural and you have to deliberately wear this "thinking hat" at many points during the software development life cycle.

Most big wins we've had in software engineering, involve methodologies that take the problem of state head-on. These "mind of state" methodologies include encapsulation in object oriented programming, test driven methodologies, generative and automated testing, minimising state and state mutations through functional programming, state and state transitions oriented design through reactive frameworks, modelling systems as finite state machines and so on.

The simultaneous awareness of Darwin OS playing tricks on us all the time and state as our enemy number 1 is the first necessary step towards creating software that just works.