Hardscrabble 🍫

By Max Jacobson

See also: the archives and an RSS feed

reverse polish notation

March 17, 2015

Earlier tonight Carlos tweeted this:

Nowhere in there does he specifically ask me to provide my take on that problem, but I did anyway. I don’t know why.

The problem is, I think, to write a reverse polish notation calculator in Ruby. Carlos used TDD to drive his solution. I looked at it and thought it was cool, and then I wanted to do the same thing, and I made a video, because I am a ham.

Here it is:

It is very long. There are a few moments where I removed sound to take away coughs. I might have missed some. I probably did!

You’ll hear every thought that passes through my mind as I arrive at a solution, which I pushed here: https://github.com/maxjacobson/calculator. A lot of it is me struggling to understand the very premise of the notation, which confused me perhaps too much?

Using tests helped me get this working because when it wasn’t working, I wasn’t sure which part wasn’t working, and I was able to add more tests to describe the parts, until I knew which parts were behaving how I expected and which weren’t. That’s really helpful. Accomplishing that meant extracting some of the responsibilities into a separate, small, testable class, which I think is a good example of lettings tests drive the design of your code. Ultimately the implementation of that class is kind of awkward and not great, but it’s also really contained and could be easily rewritten because there are tests to catch mistakes in the refactoring.


Note: I don't have comments or analytics on this website, so it's hard to tell if people are reading or enjoying it. Please feel free to share any feedback or thoughts by shooting me an email or tagging me in a post on Mastodon @maxjacobson@mastodon.online.