I see recursion as a very useful tool. But it shouldn't be used in all cases.
In my opinion, if it leads to more elegant and more maintainable code, then you should use it. Be cautious, though; you should limit the depth of the recursion.
But there are cases where recursion is simply overkill, or will lead to unnecessarily complex code; then it should not be used. A stack-based approach should be used instead.
I generally "feel" when recursion is needed or not. I generally find it a bit harder to understand at first, but then it becomes much more clearer.
2 comments
0 u/effusive_ermine 08 Mar 2017 22:32
Children are taught recursive algorithms in grade school. It isn't hard to understand.
0 u/mbenbernard 10 Mar 2017 19:04
I see recursion as a very useful tool. But it shouldn't be used in all cases.
In my opinion, if it leads to more elegant and more maintainable code, then you should use it. Be cautious, though; you should limit the depth of the recursion.
But there are cases where recursion is simply overkill, or will lead to unnecessarily complex code; then it should not be used. A stack-based approach should be used instead.
I generally "feel" when recursion is needed or not. I generally find it a bit harder to understand at first, but then it becomes much more clearer.