Is Recursion Worth it?
Tags: recursion, recursivefunction, recursiveprogramming
Published: 2021-03-14 02:20:14

I've come across many things during my programming career. One of them is recursive programming. I often thought of recursive programming as some sort of magic that does a lot of heavy lifting for us when we are writing code for sophisticated structures like binary trees. However, having read many articles and discussions on the internet and encountered a number of bugs related to recursion, is it really worth it?

I asked different Developers from different industries and different backgrounds of what they thought of recursion including people who actually studied computer science in university. A lot of experienced Developers believe that recursion is beautiful, creative, etc...; which led me to think why are we still writing recursive functions if the industry's best practices clearly state that we should "Avoid" writing recursive functions due to the notorious bugs they cause and the difficulty of maintaining code that runs through recursive calls.

The answer lies in the fact that recursive programming is still taught in universities until now as part of Computer Science subjects. Another reason of why recursive functions are still being used is also how much is the motivation of the Developer to spend an hour or so to write instead iterative function that solves the same problem attempted using the recursive function.

I won't bother you with the benefits of recursive functions since you'll find tons of resources on the internet that worship recursive programming. I will mention the cons of recursive programming supplied with examples.

###**1. Stack Overflow Error**

One of the most common problems caused by recursive functions is Stack overflow error. Stack overflow error occurs when the recursive function never reaches the condition to break out of the loop usually due to programming error.
```
    static int returnZeroNum(int number) {
        if(number == 0)
            return 0;
        else  {
            number--;
            return returnZeroNum(number);
        }
    }
```

If you look at the above example, the function at first seems to be correct. However, a simple mistake like passing a negative value to the function will make it impossible to terminate, and eventually you will get a runtime error in Java for example.

###**2. Bugs are harder to find**

Imagine in our previous code example that instead of int, we have some classes or objects that contain 10s of attributes of other classes. When you run the debugging tools to find the bug, you're not able to isolate the root cause because in a recursive function, everything is running within a single line (or 2 lines in best case scenarios), so you will end up checking each object passed to find out which one was the culprit in case the function is receiving objects from different sources or in case the function is shared between different components around the project.

###**3. Recursive functions are slower than iterative**

In the case of Binary Search Trees traversal, the iterative function was faster than the recursive one. Below is the time in nanoseconds for each of the iterative and recursive approach on the same tree containing a total of 6 nodes:
```
Iterative: Average is :266525 Nanosecond
Recursive: Average is :392921 Nanosecond
```
Despite the fact that many popular books that became a standard of good coding practices in the industry expressed an opinion against recursive programming (e.g. Clean Code by Robert C. Martin), we still find some developers who still argue that recursive programming is still a relevant and effective way of solving problems and Devs are asked to write recursive functions on coding job interviews sometimes.

I'm personally in favor of the iterative approach since I strive on writing code that is both efficient and easy to maintain; and I learned this the hard way!

Feel free to comment or share your thoughts on this topic. Which approach do you support and why? :)