Functional Life Without Side-Effects
Programming languages (and their users) are often grouped into two main intellectual schools of thought: Object-oriented programming (OOP) and functional programming. These two schools both claim to offer ways to manage the increasing complexity that comes with the development of larger and larger programs. Functional programming states that this complexity can be managed by following these principles:
- Functions are first-class citizens.
- Functions are deterministic.
- Functions should not have side effects.
- Data is immutable.
Not all of these are directly applicable to non-programming concepts, but twisting them somewhat gives us an interesting perspective on how do deal with complexity and chaos in our lives.
Functions
In order to apply functional programming to our activities, we must first endeavour to understand what a function is in this new context. Strictly speaking a function transforms each element in the set X into exactly one element in set Y. This is where the second point listed earlier comes from, since each element in set X must always output the same element (so no randomness).
A function must therefore produce something; it is not a activity merely done for the sake of doing. Things like doomscrolling or verbal communication are not functions, since they do not produce a tangible output — even if they may impact you in some way.
In what activities do we produce things then? One obvious example is cooking. Ingredients are combined or separated and should hopefully come together to create the dish that you aimed for. This will not be entirely deterministic since ingredients may be of a differing quality, your tools may be different from time to time, and your own skills as a chef means that you will inevitably make mistakes. In reality — as opposed to the realm of mathematics and computer science — the world will never be truly deterministic. But this does not mean that we should surrender completely, instead we must aim toward cooking a dish as we intended each and every time.
Side Effects
Another example where it is perhaps not imminently obvious that the activity is functional is cleaning. It also highlights the aspect of functional programming that is perhaps most famous, and is also present in the act of cooking: side effects. Side effects are when a given function not only transforms the input X into output Y, but also modifies a third value. This impacts the global state and may mean that the function, or any other function, behaves differently when run in the future. When I vacuum my apartment I have to move furniture to reach all of the nooks and crannies where dust settles. This means that the function whose purpose is to rid the floor of dust causes the side effect of moving my furniture. These side effects have to be managed and dealt with quickly, and critically be seen as part of the task itself.
It is easy to dismiss many side effects as something that can be dealt with later, but this dramatically increases the mental effort involved. Not only are you more familiar with the various items whose position and status must be dealt with, but you are also already performing the task, and so there is no “startup” cost involved with dealing with it straight away. Staring at the dishes that need to be done after a meal is an experience I expect most people to be familiar with, but it can often times be avoided when they are seen as inexirably linked to the task itself.
Conclusion
Life may never reach the pure determinism prized by functional programming, but adopting its mindset allows us to manage the increasing complexity of everyday life. By thinking of our actions as functions we can try and deal with side-effects immidiately instead of waiting for them to pile up over time, minimizing unwanted distortions in our personal “global state”.