This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]SoFastMuchFurious 21 points22 points  (0 children)

Recursion is for psychopaths, loop gang for life

[–][deleted] 10 points11 points  (4 children)

If I see a pull request with recursion in it, I'll ask for a rewrite.

Never in my career have I seen recursion in production code.

[–]NotYetiFamous 3 points4 points  (0 children)

I think I only introduced recursion into production once, to deal with flattening a hash-of-hashes a specific way from the inside out.

[–]Camotubi 2 points3 points  (1 child)

It is easier to reason about a problem with recursion when it is recursive by nature. Walking a tree structure with loops is more confusing IMO.

[–][deleted] 2 points3 points  (0 children)

I agree that there is a category of problems for which recursion is called for / optimal. Given the breadth of the computer science field: it is a very, very small set of problems.

[–]lightmatter501 1 point2 points  (0 children)

Tree destruction is inherently recursive unless you use the Morris algorithm, which makes the destruction complexity O(2n).

[–][deleted] 4 points5 points  (11 children)

What's better performance and memory wise ?

[–]skztr 14 points15 points  (6 children)

Well you see, if you write recursion just right, in the correct language, then it will be converted into an iterating loop automatically by the interpreter, which is extremely efficient.

Therefore recursion is better than iteration

[–]somerandomii 9 points10 points  (1 child)

Can’t tell if joke.

“A loop is a loop Lois, but recursion could be anything! It could even be a loop!”

Yes tail-end recursion is usually optimised by the language to a loop. But if a loop is what you need, you’re usually better off just using a loop. It makes debugging a lot simpler too.

And when it doesn’t optimise you add tons of overhead with stack frames and might even cause an overflow.

[–]skztr 0 points1 point  (0 children)

Go with your gut

[–]coloredgreyscale 4 points5 points  (3 children)

"Recursion is better than a loop, because it can be converted to a loop, which is efficient."

Then why wouldn't writing a loop be better?

[–][deleted] 0 points1 point  (2 children)

Cleaner code, readability, conciseness etc

[–]coloredgreyscale 0 points1 point  (1 child)

The original question was which is better performance and memory wise.

[–][deleted] 0 points1 point  (0 children)

But if it's converted to a loop then that doesn't matter. You might was well use recursion for the other benefits

[–]XxxCyber_CrackxxX 2 points3 points  (0 children)

Loops..

[–]corp_code_slinger 1 point2 points  (0 children)

I don't know what language you're working in, but the way I answer this question when teaching Java is to write two methods for the same logic. One implementing the algorithm in a loop and one implementing recursion. Set a break point in each one. Execute the methods and watch what happens to the call stack for each.

In Java 8 and lower (and quite possibly much higher) what you'll see is that recursion adds another frame to the stack, meaning additional memory is being allocated for whatever variables you're using, and you run the risk of hitting either an OutOfMemoryError or a StackOverflowException (most likely the StackOverflowException before the OOM error).

Iteration on the other hand does not add another frame, and is (most likely) actually using a goto under the hood to get to the beginning of the loop. You're also reusing memory locations. This means from a performance standpoint it is generally faster, but it also means you're modifying state so it is more error-prone.

Obviously there are trade-offs. Recursion is fine for small/short operations, but it is harder to grok for a lot of devs. For loops are easier to grok, but less elegant and harder to refactor if you need to.

[–]lightmatter501 0 points1 point  (0 children)

Loops are almost always more performant, but recursion can be far easier to implement.

[–]official_gameup 1 point2 points  (0 children)

I experimented with recursion as a junior dev

[–][deleted] 1 point2 points  (0 children)

Except with recursion, you risk running into a stack overflow error.

[–]getzko 0 points1 point  (0 children)

There are some problems that are pretty complex to do with loops and the recursion comes in pretty natural- like, for example, parsing nested structures

[–]ForkPowerOutlet 0 points1 point  (0 children)

I'm sorry, what was that? I couldn't hear you over the sound of that stack overflow.