Recursion My Biggest Fear

juan vargas
3 min readSep 2, 2020

Let me preface this article to admit that while many aspects to it are going to be technical; fact of the matter is this article is entirely my personal opinion and I’m 100% opinionated, it is also in hopes to relieve some of the “fear” in you if you have it!

When one is learning to code in javascript pretty early on you will be introduced to the concept of ‘loops’. A pretty simple concept we want x event to happen x times which in Js (javascript)looks something like this:

These are some of the most common applications of loops out there(do something x amount of times OR do something to every element).

Now Pretty early in Js one also gets introduced to Loops ugly cousin “recursion” a more complicated way to get X event to happen X times, only difference being this time we don’t necessarily create a loop but instead call a function inside itself. I may know what you're thinking doesn’t that create an infinite loop? And the answer to that is:

It most definitely will, like 95% of the time hahaha. Unless you set it up correctly your browser will either crash or exceed maximum call stack(you called a function more than x times, depending on the browser). and it looks like this :

For that reason I tried to stay away from it for as long as I could! Which was like 2 weeks. Soon You will find out some algorithms cannot be solved otherwise but it is so easy to mess up the recursion that often i find myself going for a (For Loop) even though I know that recursion is 100% the way to go about it and end up wasting time because eventually i come back around and begrudgingly use a recursion. Recursion applies mainly when you don’t know how many loops it is going to take. Array inside an array: Easy just do a double for loop see first image, bu what about an array 4 or 5 or even 10 levels deep? it is in cases like this that recursion becomes useful! Trust me when I tell you you don’t wanna try a loop 5 levels deep, it is a mess to say the least.

This whole rant has 1 purpose, and it is that if you to some degree relate to this fear and despise of recursion, I would like to make it a little better for you by giving you the advice that made recursion bearable to me:

  1. Start form the smallest case. I know it may seem counterintuitive that the whole reason I’m doing it its because I have lists within lists, but you have to figure out how to stop it before you start it.
  2. Do NOT be afraid of using a helper function and just defining it inside your function. While it is possible to resolve most recursions inside a return statement with no helper function, more often than not you will have to use the binding of “this” for that and that is another can of worms you don’t want to open.
  3. You CAN use the function itself as a variable or add it to a number or string, or array. Remember that you’re using whatever the function evaluates to not the function itself.

I would like to provide more advice than that but I’m also in the middle of figuring out recursion myself. If there is one more piece of advice that i can give you and this applies not only to recursion but also to Computer Science or programing in general; it is that while it most certainly will not get better, it will for sure ‘feel better’ once you understand that it is OK to not know.

--

--