One of the problems I’d previously had while developing a rather complex mobile app with the Angular-UI-Router was that the state.config file got pretty gnarly with more than a few abstract and nested states.

In the process of looking for a different solution, I came across a merge note that mentioned there’s now a way to use modular state.config files.

“Note: You still need to manage module dependencies.”

I don’t see a clear path to using this pattern & the BaseState class constructor pattern, but I still think we should convert to the modular version.

If you’re not familiar with Grunt, you should know that you can get documentation at their website and you can get their code on github. There’s a great getting started tutorial at their website. This post will cover a lot of that, specially tuned for AWS EC2 instances with NodeJS, Neo4j, Angular, Stylus, Topcoat, and Express. This post assumes you’ve already got those packages and their dependencies installed on your AWS machine. Feel free to reachout if you’re in need of help.

Recursion is a concept that can be made easier if you have an easy way to think about it, and what it is you’re actually doing. For this discussion, I’ll be considering the Fibonacci function, which returns a number that is the result of adding the two previous fibonacci numbers. This function is bound by zero, so starting at zero you’ll get:

1. It’s turtles all the way down… until you’re at the base:

A recursive solution is suitable for the types of problems that require you to support your next result with the outcomes of previous invocations of your function. Like the old story of what supports the flat earth, recursion can go on and on indefinitely. Unlike the fable, however, we must have a base case or our recursions will fall apart. At the base case, your recursion stops recursing and finally begins to really operate. So you always need to make sure that you have a “base case” to stop the function from depending on itself for any more arguments. In the Fibonacci example, the base case is contained in the else statement: When n <= 2 return n.

By returning a value which doesn’t require any further code execution, you enable the recusion to stop. Your base case is the first solution that your code will reach, and it’s the result which will support the final results. It’s often easiest to think of the base case as the bottom, but that won’t always be true.

2. Always send flowers (unless you don’t have to):

In life, flowers serve the function of reminding the recipient who sent them. (Yes, they serve other functions as well, but for the sake of this discussion I’m using them as a literary device.) In this example, they serve the purpose of reminding you to ALWAYS MODIFY THE INFORMATION YOU’RE PASSING FORWARD. When you’re not at your base case, you have to send flowers to the next step of the function in order to set the stage for future invocations to reach the base case. In the above example, our flowers are sent by the returnstatement when n > 2:

returnfibberfunk(n-1)+fibberfunk(n-2);

Notice that each successive function calls the same function with a modified argument. In our case, we’re reducing the value of the argument we’ve passed in which brings us closer to our base case of having n <= 2. By modifying the argument we pass to subsequent invocations of the function, we’re “sending” information… in our case, the information we’re “sending” is that we’ve taken one step closer to the base case. This is called the “recursive case” and you might have more than one type of recursive case in your function, in ours there is only one.

It’s very, very important that you send flowers, or your function will never know when it’s reached the base case.

3. Recursion is not iteration

The last trick I want to share is not so goofy, but I think it should be taken seriously. I tend to remind myself that recursion isn’t the same as iteration so I don’t create for loops to solve recursion problems. In an iterative function, we say something like this:

While we are passing information forward through the for loop, by incrementing the index, notice that schmiterative(); is not dependent on the result of the base case to be able to calculate all the other results. Our incrementation will continue indefinitely until i = num but we’re able to calculate the result of every return statement without relying on the result of the base case.

Let’s hope that any of the tricks will help you get a little bit closer to solving your own recursive riddle, and if you’ve got some suggestion of how to improve or correct anything I’ve written, please let me know.