Hey Functional Programming community. A week ago I first watched a talk about Functional Programming in Javascript and learned a lot about it afterwards. But now, a week later, I'm so frustrated. Is it me, do I not understand FP good enough? Or is it just a neckbeard "im the real programmer" hype as it feels like now? Please help me understand the difference and why you would choose for the first approach. Thank you so much!
Okay; All I want is to display a button on a webpage.
The HTML:
<div id="app"></div>
Now we have to create a pure function that will create a HTMLButtonElement:
const newButton = _ => document.createElement("button");
But, we also would like to have a label on our button right? So we need a textNode and append this to our button. Two different functions in Functional Programming:
const newTextNode = text => document.createTextNode(text);
const append = (child, target) => { target.appendChild(child); return target };
Now we need an other function to combine all these functions to create some function inception just to create our fucking button and have it in the right order as well:
const button = label => append(newTextNode(label), newButton());
Finally we can create our button and append it to the body:
const myButton = button("Click me!");
document.body.append(myButton);
To have an end result of this pile of shit:
const newButton = _ => document.createElement("button");
const newTextNode = text => document.createTextNode(text);
const append = (child, target) => { target.appendChild(child); return target };
const button = label => append(newTextNode(label), newButton());
const myButton = button("Click me!");
document.body.append(myButton);
This seems to me like it's overly complex. Especially when we have this super simple, human understandable OOP version:
class Button {
constructor(label) {
const button = document.createElement("button");
const btnLabel = document.createTextNode(label);
button.appendChild(btnLabel);
return button;
}
const myButtonOOP = new Button("Click Me!");
document.body.append(myButtonOOP);
Please help me understand why one would prefer the first, the FP approach, over the latter, the OOP approach. Especially when we would like to add an, for example, callback to the button. In the OOP version all we have to do is alter the constructor making the callback optional for reverse compatibility:
constructor(label, callback = false)
And we have to add the event Listener if a callback is present:
if(callback) {
button.addEventListener("click", callback);
}
If we would like to do the same, super simple change, to the Functional approach. We have to create this function inception that's almost unreadable. I'll probably write it completely wrong as it's too complex to come up with:
const listen = (target, callback) => target.addEventListener("click", callback);
const button = (label, callback = false) => listen(append(newTextNode(label), newButton(), callback);
It just doesn't make sense to me. The latter is so disgustingly unreadable?
I get that the pure functions are an amazing concept that make a lot of sense. But at what cost?