DEV Community

John Kazer
John Kazer

Posted on

Why functional programming? Stay in control

As I delve a bit more into the experience of converting myself into a semi-literate functional programmer, I still wonder what is the point.

Well, one interesting realisation occurred today and it is about control.

I was looking at some code, a straightforward JavaScript array.forEach construct. I thought about what was needed to convert it into something more functional - e.g. a array.map. What would happen? I'd need a return value to use as the logical effect of the function. What was I doing right now? It was hard to say - need to read the code and find out what the effect really is.

And then it hit me - using forEach means I was losing control of the code. It didn't feel 'safe' somehow. If I was asking for a return value then I'd still be in control because then I can do something with it. Using forEach gives all that responsibility away. I have to trust the called function to do the right thing.

Maybe this is a consequence of writing functional code and my emotional response to code is changing?

Hard to say, but it gave me some insight into the psychology of code wars and declarative (functional) vs procedural approaches.

Conclusion: Functional programmers like control and certainty, procedural programmers are more chilled (or maybe more anxious).

Top comments (1)

Collapse
 
mjsarfatti profile image
Manuele J Sarfatti

Love this take!