I can’t even count the number of times that I’ve written recursion in my code – directories, tree nodes, object graphs… whatever the case was. It’s a fairly important part of dealing with things in software, it seems.
But in all these times I’ve done this in the past, I’ve usually had some sort of ugly group of functions or separate class / object with an ugly group of functions to handle this for me.
Hide The Ugly Parts
The end result is pretty nice:
Now I don’t ever have to see the “extra” function for recursion on my object’s API. It’s hidden inside of the actual API call that I care about.
This may not be anything new or revolutionary (nothing I do ever is!), but it was a fun little “aha!” moment for me, today.
P.S. In case your wondering, the code above if MongooseJS code. Need to learn more about Mongoose and MongoDB? Check out my WatchMeCode screencast series on MongooseJS Fundamentals.