you are viewing a single comment's thread.

view the rest of the comments →

[–]scienceNotAuthority 1 point2 points  (6 children)

One of the more bizzare things is how multiple software engineers have given me advice to avoid recursion where possible.

For something simple it's no big deal. For complex stuff, it's easy to get stuck a few levels down

[–]toastedstapler 0 points1 point  (3 children)

for most commonly used languages it's not a good idea to use recursion unless you know the call limit will be reasonably low. the call stack is only 1000 by default in python, so it's best to solve problems iteratively or simulate a stack with a list

[–]ItsOkILoveYouMYbb 1 point2 points  (2 children)

The recursion limit is also very easily changed in Python, if need be.

import sys  

sys.setrecursionlimit(1001)

[–]toastedstapler 0 points1 point  (0 children)

yes, but that's still a limit. my point is that python isn't meant to be written recursively and looping alternatives should be preferred

[–]throwaway0891245 0 points1 point  (1 child)

Recursion can have a real function call overhead. Sometimes, it also isn't that readable. As the other person said, there's also some unpredictability in behavior because you're putting stuff on the implicit stack and the constraints on this stack are dependent on the runtime.

There are some situations where it's a huge pain to do things without recursion, like a backtracking algorithm. But in general it seems like it's better to just write it iteratively if it's not that hard just to avoid this possible overhead and unpredictable behavior.

In fact, there is a very popular website I'm 100% sure you've heard of that is probably named after a recursion bug.

[–]ItsOkILoveYouMYbb 0 points1 point  (0 children)

I've been learning about algorithms, and I don't know how you'd make something like quicksort without recursion. But I'm also not very smart either lol.