Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Recursion is cooler. Plus it makes for more understandable course (occasionally)
-
mhudson12937yReally depends on the situation.
Recursion elegantly solves problems that would otherwise require a lot of condition checking for an iterative solution. -
Personally, I do a lot of work with embedded systems and RTOS, so I already don't have a big stack to work with most of the time.
I'd much rather have a bigger, iteration-based codebase that is harder to read, because that all exists on SRAM during runtime, and doesn't take up a lot of space.
Invocation records exist on the user stack, which fills up very fast unless you fork every recursive task into a child process. Then you're at least giving the recursion its own context that it can fill up, and the parent is somewhat protected if the child runs away. Even then you have to worry about communicating the results of your recursive call back to the parent, which adds a slight performance hit, and you have to make sure you're getting the right info from the right process, and you run the risk of zombied processes building up.
I guess I prefer to use iteration because of space and time concerns, and I'd much rather use comments in a complicated but fast loop. -
I wouldn't rely on recursion for complicated tasks unless I shunted the recursion inside a child process.
Small recursive tasks I'll either try to make iterative, or if it's really dead-simple, just do the recursion right then and there. But it would need to be a small task. -
Plus, putting the recursive call inside a child process means the parent can complete other code while it's waiting for the recursion to complete.
You could even have multiple, concurrent children performing seperate recursive tasks under the same parent, and complete other code while waiting for the recursive tasks to finish up.
Recursion or iteration, and why?
undefined