Check out my
new game!

jlongster

Clojure and Incremental Development

January 28, 2010

Clojure

I just rebuilt this site in Clojure. I have a goal for this webserver: to only install libraries needed to support a Lisp or Scheme implementation. That means no Python, Ruby, etc. I only want to use Lisp.

That doesn't mean I'm condemning other languages. You should use whatever you think is the best; the right tool for the right job. However, part of my vision is that Lisp is really great. But it has social problems. It doesn't fit in. If only we were to work on its social skills, maybe others would see the power within.

So my experiment does two things: lets me use a language I enjoy but also lets me feel the pains of supporting my full-time work with Lisp-based languages. If I know where the biggest pains are, I can work on them myself and hopefully make Lisp look a little cooler everyday to a few people.

Not only am I only using Lisp for this website, I'm only using Lisp (or Scheme, rather) for my development work. I am currently developing games in Gambit Scheme. Again, I get to use a language I really enjoy, but I get to feel the pains of using Scheme for actual work. And I mean real work, like building an iPhone game that will be released in a couple weeks. Porting Scheme to the iPhone was the first and easiest task, and I learned a lot while building a 3d game from scratch. I will explain more about this in a post-mortem soon.

I just left my job to go full-time to pursue this experiment. I really want Lisp to be more popular, you see, and I think it can be. Now, enough with that, I want to talk a little about Clojure.

Clojure is awesome. It is a Lisp which compiles to the Java Virtual Machine. I thought it was really awkward at first, but that's only because I have a heavy Scheme, not Lisp, background. The key point which I understood Clojure was when I figured out how to create and operate on Java objects, at which point the whole world of Java libraries opened up to me. Clojure's documentation was frustrating until I realized that I should be scouring the Java docs for the Java String objects instead of looking for a string-replace function in Clojure.

I am running this site on Compojure, a web framework for Clojure. It's a great framework and made it easy and fast to build and rollout this website (only took me 2 days having never used Clojure before).

Incremental Development is King

One thing really stood out to me during my Clojure experience: incremental development. This technique is going to be the keystone of my arguments for Lisp. The idea is to fire up a Lisp process once, and then send code to be evaluated to this process. This process could stay alive for hours as you build up the program by continually evaluating pieces of it.

If you don't understand how great that is, consider using Compojure/Clojure under SLIME, an Emacs library for managing the connection between the Lisp process and your code. When I start developing, I call slime-eval-buffer in my main top-level module, which loads the whole program into memory in the Lisp process. Now I tell SLIME to evaluate (start main-server) to start the webserver. From here on, I just re-evaluate code when I want to change something. For example, I could send over a function definition by pressing C-x C-e, and my new function is immediately running within the program.

No server restarts. No complicated attempts at poking at source code files to see if something's changed (ahem, Django...). When I was using Django, it would be common for me to be in the middle of something, save the file, and for the Django server to crash because of a syntax error (since it automatically reloads source files when they change). Manually restarting the server got annoying quickly.

For another example of how sleek incremental development can be, check out my video about using it to develop 3d graphics applications in real-time.

With all that said, Lisp can be awkward, but that's for another article. It's this awkwardness that I hope to mitigate in several ways, and I hope you'll stick with me in my adventure of crowning Lisp/Scheme as king of all languages.

Comments are not implemented yet. In the meanwhile, send me an email.