DEV Community

Discussion on: What Subset Of The Language Do You Not Use?

Collapse
 
macsikora profile image
Pragmatic Maciej • Edited

It's interesting as recursion is the base concept of FP. Everything in FP is based on recursion. If you take list and any operation on list its pure recursion as you operate on head and you call itself on tail. This is also represented in Haskell by x:xs

Collapse
 
functional_js profile image
Functional Javascript • Edited

Thanks for your input Maciej.

For one thing, we're talking about Javascript; so we'll keep the criteria analysis narrowed to that.

And we're talking recursion where a function calls itself within itself, where each call adds another frame onto the stack, which is extremely non-robust and less performant than a loop.
If another language has a way to protect its stack, that's a benefit; but the JavaScript compiler doesn't. The stack will explode once exhausted "RangeError: Maximum call stack size exceeded". Thus it fails the robustness test. (If you know a way to make JS optimize for tail-call recursion let me know and I'll test it out.)

Also, when one is designing systems, one should not think in terms of "should I use recursion or some other implementation". One should think in terms of, "what are the candidate solutions, and which is the most performant while being the most robust". On that criteria set, recursion loses every time in every case I've seen. In a case where it would win out on the criteria set over other candidate solutions, then it would be apropos to use it.

Also, something that is not in the criteria set for choosing best solutions is thinking that one is "supposed" to use it, or "it's how everyone else is doing it", or "it's considered a best practice by somebody somewhere", or "you're not doing X paradigm if you're not doing Y pattern".

I describe the criteria set analysis here. (If you have ideas to add to it let me know)...
dev.to/functional_js/squeezing-out...

So to sum up, I'm not against recursion. I'm against code that is less performant and less robust.