2 minute read

I think I can say that I’m like most typical engineers in saying that my fundamentals can be quite shabby. I will happily admit that my knowledge of recursion is weak. To make this a slightly more entertaining enterprise, I will more or less take notes of my charter into the unknown.

le intro

So let’s get into it. When I was learning development in school, recursion was glossed over - Fibonacci happened - and then we were done.

That sucked.

Everyone says it’s an elegant way of approaching certain problems, and I can see that for anything related to trees. But, for the most part, I haven’t seen many tree-related data structures in my work.

le basics

A key for when a recursive function is required is when you don’t know how many levels of for loops are required.

Solve the simple, and extend the solution to call itself on bigger problems. Divide and conquer, simplifying the problem.

Try to identify the base case - the simplest form and once you’ve finished identifying the case, throw it to the recursion function and forget about it. It’s not your problem and you just have to believe that it’s coming back correctly.

Base Case - when to exit, the ideal case.

Recursive Case - how to make the problem smaller

I think most problems look like the following:

function doSomethingRecursively(param) {
  if(param === 0) { // base case life
    return; // terminate me plz
  return 1 + doSomethingRecursively(param - 1); // continue but make me easier each time

I have no idea what that function above is trying to do, but I know that it will hit 0 and stop eventually. And that it will guaranteed hit 0 if we start param off as a positive integer.

Whelp, that was a lot of googling and medium.

Let’s try doing something

For my recursion needs, I’ll be trying puzzles from Coding Bat. After doing a couple, I feel like I’m just shooting in the dark as to what my base cases are.

Another note, recursion feels like a bunch of if statements. eek.

On another side note, I find functional programming with streams 10x easier than recursion. sigh.

Fast-forward a couple hours

After playing with my niece and doing wedding prep, I’ve not learned much about recursion other than that I come back and try to learn recursion and I get a little closer everytime. How meta. I feel like if I get the puzzle piece to fit correctly, I insta-become a RECURSION GOOOOOOOOOD. But otherwise, I feel like a noob and read javadocs like a good boy.

Couple minutes later

holy crap, I might be getting it or I might have just been doing the same problems too many times. Onwards with the investigation!


Maybe I just have to accept that I can do recursion sometimes, and sometimes I can’t. I guess that’s pretty normal. There are just some problems you can’t solve without significant thinkingsauce and investment in time.

Anyway, I’ll get back to y’all next time. Hopefully, with more code.

Code can be found here: codingBatScratchPad