So I am writing a pacman in clojure to have something to do while I get the Overtone problems worked out. The day before I started I went to a local software craftsman meeting where we did the pacman kata. I paired with a groovy developer. So we were working through it with a object oriented approach.
Having that in mind, I started writing pacman trying to use a similar approach. So I started by doing this:
(def pacman {:direction 'left})
I tested change of direction with something like this:
(= 'right (direction-to 'left pacman))
When I got to adding position to pacman, I discovered that I had to update the pacman var. I don’t know how to update a var yet, and I didn’t want to know since that introduces state. Although this is not exactly true, it would be state in my mind. I want to see how long I can drive this project without side-effects.
So instead of of having a var pacman, I decided to create a function that would return the initial state. This looked like this
(def start-game [] {:direction 'left})
Now, at this point I ignore how bad this is for performance. I supposed very bad. But in terms avoiding side effects this is golden for me. There is a sudden shift in my mind that we are not updating variables. I am creating a new map.
This may seem like a minor change in term of syntax or even actual execution. Yet semantic changes like that are significant because it makes me, the programmer, think differently.
So far I have added to the state map direction and the position of pacman. Let’s see how far I can get with this model.