DEV Community

sandrockj
sandrockj

Posted on • Edited on

Recursion or Loop?

When should you use a recursive function instead of a loop statement? Likewise, when should you use a loop statement instead of recursion?

Sometimes I feel unsure of how to answer this question, and I find myself reminiscing of a toy problem from the past. However, before we move further I wanted to briefly go over the concept of recursion as well as the concept of looping for anyone that might be unfamiliar with either.

Recursion is the process by which a function calls itself until it satisfies a certain condition or a set of conditions. A recursive approach can be great for some problems, such as working within lists or sequences. However, a recursive approach has a greater deal of code complexity than a looping approach. As a result, the performance of our code may be affected in the long-term if we develop an overreliance.

Looping is the process by which we continuously repeat a part of code until a certain condition or a set of conditions are met. The main difference between looping and recursion is that a looping statement does not "call itself", we will explore this further when we get into our toy problem. There are quite a few ways to loop, and the methods available to you depend on the coding language. For the purposes of this post, we will discuss looping in JavaScript.

You might notice that the definitions for recursion and looping are quite similar, and that is because these two processes are doing the same thing: they are performing a task for a certain number of iterations. Now let's get into that toy problem and see how differently these approaches are structured, despite their conceptual likeness. The instructions for the toy problem that I mentioned earlier were quite similar to the following:

Write a set of code that will evaluate whether or not the provided String variable is a palindrome. Your code should determine a boolean value reflecting this condition.

It took me a while to solve the code, given some of the constraints and edge conditions. However, eventually I was able to solve this problem through the use of a recursive function.

Image description

After a while I wondered, would this be possible with a For Loop? And so, I set out to find an answer and began experimenting. What I found is that, sure enough, this can be done with a Loop statement as well.

Image description

As I reflected upon the two sets of code, I found myself somewhat frustrated because it felt like I still had not grasped why you would prefer a recursive statement over a Loop. I came across a discussion on StackExchange that shed some light on the topic. A developer by the name of Scant Roger stated the following:

"Ultimately, there's nothing recursion can compute that looping can't, but looping takes a lot more plumbing. Therefore, the one thing recursion can do that loops can't is make some tasks super easy...Often the recursive solution to a problem is prettier."

It became apparent to me that Roger was absolutely right; I found that it was much easier for me to write the recursive solution than it was to develop the same solution with a Loop statement. If it is easier to create a recursive solution, and to later redesign it as a Loop (to reduce code complexity) I think that is fine.

What are your thoughts? I am still new to coding, and I would love to hear from everyone else!

Top comments (0)