It is just a code but it’s not the whole point of the conversation. For this case, we are not talking about code but paradigm. Now, you can say “your code is not functional programming”, AND THAT’S MY WHOLE POINT. What is functional programming and what is not?.
But lets me talk about your example:
Let’s say our project is huge, 100k lines of code and lots of moving parts here and there.
It is your code
apple = fruits.find(f => ‘apple’ === f.name);
if (apple) applePrice = apple.price;
Now, let’s say that
apple = fruits.find(f => ‘apple’ === f.name);
is found somewhere, maybe in a different file. Why?. Because we want to reuse it.
And in another part of the code, we found the next line:
if (apple) applePrice = apple.price;
Question: If we only have access to this code, what it does? Is Apple a variable, a class, a function or what?
Now, let’s say the same line written in OOP
class AppleService {
function find(object) {
// ….
}
}if(AppleService.find(apple)) {
applePrice=apple.price
}
Long but it is almost self-explanatory. We find an object apple (using apple service), if the find it, then we store the price. We won’t need to peek at the definition of “apple”. The object just values while the service class is a collection of function. What’s the goal? What if the function fails?. Simple. We go to AppleService, find the function, edit and that’s it. We won’t need to hunt for the code.