Danger Variables. Don't Blow Your Stack!

Whenever you are writing a recursive function, you run the risk of the recursion never ending and causing a stack overflow. One way to prevent this is to create a “danger” variable. Essentially, the danger variable is a way to provide an exit from a function that is running in infinite recursion.

How it works:

1) Create a global danger variable and set it equal to zero.

2) Every time you enter the recursive function, increase the count of that variable and check if that count is greater than the amount recursive calls you expect.

3) If the danger count ever exceeds that number, exit the recursive function.

A simple example:

var someFunction = function (){
  var danger = 0;
  
  var recursiveFunc = function (){
    danger++;
    if (danger > 200) return;

    // write the body of the recursive function here
    
  } 

}

Note: This technique can also be applied to “while” loops and other forms of code that run the risk of infinitly looping.

Written on December 20, 2015