
The above optical illusion is an excerpt from a book by Stephen Covey – the picture has both a young and old lady all in one. It’s all about perspectives.
Please listen more about this here.
Introduction
Naturally, OOP has been our one and the only lens through which we saw everything. But, functional programming is uprooting and shaking our OOP foundational beliefs slowly and steadily ,or is it overrated? Are we setting into the programming perceptive generational gap? At the end of this post we will grasp the functional way of thinking (immutability) and how is it different from OOP. I always believed in conceptual understanding rather than million lines of symbols, formulas ,or code. Concepts precede code. We talked about FP in the previous post but this post is even a precursor to that.
Thinking in a certain way
Composition: Functional Vs OOP
OOP is all about objects and collaboration between them. An object is a collection of [properties/data + functions or behaviors] that ideally should mimic the real-world entity. A given object can take different states overtime (on each function call) and any given snap-shot or instance holds the state of the entity in memory. Functional composition is about the state or data flowing through functions.
Mutable Vs Immutable
To understand immutable data, you have to grasp mutable data first. When a function in an object is called it usually changes or alters the state/data/properties values of the object in context. This process is called mutation. When a programming language, restricts this mutation by “syntax”- for eg: when you modify/alter/insert a data point in an array, it returns a brand new instance of that array itself. Just to buttress your thought here-remember how strings are handled in C#/JAVA.
Here is a so-called pseudo code that describes immutability-
ArrayVariableA = referenceX to an array list of integers.
NewArrayVariableB = referenceY is returned by [inserting a new data point of another integer into the ArrayVariableA array]
Now the ArrayVariableA and NewArrayVariableB are not referentially the same. They are two different data structures.
This precisely is called immutability (a special type called a read-only restriction is applied above). I would like to reiterate here — Where the programming language “syntactically” doesn’t allow you to modify the data at will and even if you change it restricts by creating an immutable data structure(new reference). But WHY? Why does OOP allow to mutate and FP restricts by immutability? Within the limits to my knowledge, I believe that it’s because of curbing the bugs in code, security reasons, writing thread-safe high-performance code that leverages the modern multi-core parallel architecture, etc. We have dealt with all the above scenarios in our past blog.
Conclusion
We have informally described one of the pillars of FP (immutability). Various mainstream programming languages have introduced immutability into their repertoire of powerful features. Like .NET (Freezable), JAVA (final) and Javascript (Object.freeze). These changes indicate that FP is clearly ruffling feathers in the existing programming community but are we ready for a pure FP language similar to Haskell that will sweep the scientific and commercial computer science community. I believe that is quite possible in near future. But for now, OOP will rule the world by introducing FP features alongside Object-Oriented Programming.
It’s a gift to see things as it is.