I think that a significant number of people in the Salesforce ecosystem first encounter the idea of recursion when they are studying for the Platform Developer 1 exam. This was the case for me. And, if you don’t have a computer science background it’s unlikely you would have encountered the concept in the past. My own experience was that this created a very limited understanding of recursion. I only understood it as it applied to unwanted trigger re-entry or an endless loop. That is to say that I thought ‘recursion = bad’. But the reality is that it is powerful and fundamental concept in computer science. And even those us of that work with languages that are not really friendly to recursion, like Apex, should still understand it.
What is Recusion and How Do We Use It?
A recursive method is nothing more than a method that calls itself. Anytime a method is called it goes on the top of something known as the call stack. Methods are executed from the top of the calls stack to the bottom. In the case of Apex we have limit to our stack depth of 1000. That means if you have a recursive method it can never call itself more than 1000 times. This is useful in preventing a endless loop and ensuring the fair use of resources when we are dealing with the multi-tenant architecture of the Salesforce cloud. Here is great article from Free Code Camp detailing the call stack and recursion.
Let’s look at a simple of example of a method in Apex that calculates a factorial. This is probably the classic intro to recursive code. If you are rusty on your high school math a factorial is the product of an integer and all the integers below it. The factorial of 4 or 4! = 4 * 3 * 2 *1 or 24. The code below is an implementation of a recursive factorial calculator in Apex.
The condition in the If statement is what is known as our base case. This is what is telling the method exit from the recursive calls. Without it we will create an endless loop. If we pass in the number 3 the method would execute as follows: 3 is greater than 0 so the method calls itself with a return value of 3 multiplied by the the return value of our method but now the variable num = 2. The method calls keep getting placed on the call stack until the value of num = 0. When that happens we exit out of our recursive loop and return the value of 1. Now all the previous method calls that we were waiting on the call stack get popped off and execute in reverse order. We multiply 1 times 2 and then 2 times 3 for the result of 6.
If we take a look at our debug log we can see the recursive method in action and that the correct result was returned.
Note that if we try it with a larger number we can see that we will hit the Apex stack depth governor limits.
Below is an example of an iterative factorial calculator -this is what we would implement in Apex unless we could be certain we will always be dealing with fairly small numbers. Now, rather than the method calling itself we are using a standard for loop and iterating over the number. The iterativeFactorialCalc method will only go on the call stack one time and will remain in the loop until the variable i is greater than or equal to the number being evaluated.
The use cases for recursion in Apex development are limited. But, recursion itself is not an inherent evil and is commonly used in applications with languages that support it. While it will never be a a go to technique for a Salesforce developer or architect, I think it is a critical concept to understand.