loading...

re: The beauty of Functional Programming VIEW POST

FULL DISCUSSION
 

While I do prefer a FP-esque style of programming, I don't think that the recursive myCount is more readable.

It's also less safe, at least as it's currently written, since it'll cause "infinite" recursion for any i bigger than 10.

The imperative version, on the other hand, will default to 0 for any such values

function iterativeMyCount(n) {
  let count = 0;
  for (let i = n; i < 10; ++i) {
    count += i;
  }

  return count;
}

I generally prefer to use local mutation (when circumstances allow) instead of going full recursive - unless the problem is tailor made for it, like tree traversal or something.

I find it easier to reason about and avoids the parameter growth of completely mutation free cases - especially if optimizing for TCO or memory.

 

Okay, i agree partially with you, because for many friends is confuse the syntax of the loop reassigning the count value many times
i = i+1, and the math perspective this is dont make sense, in recursive way this is declarative, you express what you want and the recursion make the things, although in many scenarios i prefer using iterative way. Since this is my particular opinion and the example was too confuse, i removed. But yes, with recursion we have other problems

 

I agree. I doubt many people would consider the recursive version to be more readable in this case. Recursion is a very powerful tool which is essential in some scenarios and just plain more elegant and understandable in others. However, here it really is unnecessary. Recursive code can also be extremely difficult to debug. This is my own personal experience from working a lot with trees in several different contexts.

code of conduct - report abuse