More efficiency by being lazy

Gauravarapu Pavan Subbarao
2 min readFeb 5, 2022

It is certainly obvious to say a person is a metaphor for the spirit of service, when we know he/she is an earnest worker and works to the best of his/her abilities with no expectations.

Spending our time to evaluate him at the very beginning, is to waste our time and demonstrate the world ‘how not to be

Now let's see how this allegory translates to the programming language

Pure function in the context of functional programming is untarnished immaculate being who enjoy their work to the hilt with no side effects. In doing this they are being promoted to a better setting. let's explore what it takes to go beyond lazy constructs and make functions eligible for lazy loading in JAVA

In Java, if we have to initialize the value later, we are tempted to assign it to null and there is surely no contract that the value later will be initialized to the right one.

can this mutable nature be referentially transparent?

Obviously No — so, what's the solution to make it immutable and not resort to null reference?

Perhaps we may have to think functionally to make our functions composable so that we can use them swiftly in the bigger calculations.

And to think functionally is to -

1) reduce mutation

2) avoid implicit dependencies

3) cutoff side effect

In building so, we are building a system, which is much greater than the sum of its functional parts.

method is now a first class citizen and can be used a like any other variable

The code here is not just avoiding the null references but is also ensuring it does not throw any exception when the method arguments are null or when divided by 0 (just a hypothetical ex — mathematically absurd though! ).

This division function by accepting supplier as its arguments is choosing not to evaluate its arguments and that is alright. For, the function above is neither dependent on the implicit data, nor embracing mutation or having the side effects.

It is a perfect candidate to be called as referentially transparent and therefore it is fair just to note that it must be evaluated sometime in the future when it is being used than acting on it when not necessary.

I am repeating stuff in different variants, but this is surely a way we learn more. For in doing this, we are all picking up on different subtilties in it.

About me: I’m working as a software test consultant who cares for helping employers enhance and simplify their testing efforts through smart application of tools.

--

--