9
drewbie
4y

How many recursive functions have you wrote at work?

Comments
  • 6
    Zero

    How many have I used that are recursive internally? Probably a lot.
  • 2
    @halfflat I saw an article that talked about gcc optimizations. It showed how in some cases gcc turns the recursive function into a loop. Really interesting how it smart enough to do that.
  • 4
    A few. Why the question?

    Typical cases: rendering nested navigation lists, recursively reading directories, working with any tree structures
  • 2
    A hand full but not a regular exercise.
  • 1
    @webketje as a frontend I've never had to do it, and going into interviews having to grasp it is difficult for me. That's all.
  • 2
    @drewbie It's mainly good for depth first traversal. They usually introduce it with fibbonacci numbers, but that's actually a bad example because it is better solved with a basic loop.
  • 2
    2 or 3.
    I'm really fond of recursion, my last project's main engine is completely based on asynchronous recursion.
  • 0
    @Demolishun aka tail recursion?
  • 2
    A lot. I mean when you are building a tree structure, recursion is a must imo because it is much more readable than other approaches.
  • 0
    @lamka02sk I disagree as a personal preference. Instead, I use a stack or queue depending on desired processing order. By doing so, any debugging deals with just a single while loop rather than a call stack N levels deep.
  • 1
    Intentionally? In 15 years, one. And I'm pretty sure the next dev took it out.

    Such is life in enterprise shops.
  • 0
  • 3
  • 1
    @hash-table well but shouldn't you do that the other way round? Generally using loops (if possible I know that there are problems that are not solvable without recursion) is more performant speed and space wise.
  • 1
    @BugsBuggy Totally fell for it.
  • 3
  • 1
    @drewbie in fact I had to do a test in an interview with a recursive function last week. It was the worst idea ever and I half-failed (had to spin up a user friendly React-Redux nested-grouped todo MVC app in 30 minutes, npm robbed me of 10). Imo if they want to test your skills like that they should give you an assignment with enough time to come up with a decent solution
  • 0
    @fuckwit If you need a stack, recursion is definitely faster since there's hardware support for the call stack. If you don't need to allocate memory, loops are probably faster.
Add Comment