July 10, 2013

Live development is the way of the future

(With Reviews of Fireplace.vim and LightTable)

Live development is nothing new; it’s been well-explored in Lisps and in other languages like Smalltalk and Erlang since more or less the dawn of man. Emacs was for a long time – and by many still is – considered the only way to develop for Lisp, thanks to SLIME.

But I with projects such as Light Table, and a growing popularity of functional and dynamic languages in general, I think we’ll see live coding become seriously popular as capable mainstream tools collide with capable mainstream languages. After all, there’s so much to be gained.

(Having said that about mainstream tools and languages, fully half of this post deals with using Vim with Clojure, so don’t get too excited.)

What is live development?

Have you ever used a language with a REPL? Python has one when you just run python, and Ruby has one with irb. PHP has a little-used one in php -a, and of course every Lisp ever made considers it an essential feature.

When you take a language with a REPL, and use an editor or tool that lets you send the code you’re writing to the REPL and evaluate it, you’re live-developing.

Live development is really excellent for exploratory programming – trying out solutions to problems so you can understand the problem better. This can be anything from writing a small, one-off script to index your MP3 collection, to figuratively flinging code at an undocumented SOAP API to see what you can get to stick. If it’s not a trivial problem, chances are doing some live development can help you understand it better.

Python, JS, and Clojure Live Development with LightTable

It’s not yet my IDE of choice, but more and more I’ve found myself using Light Table for exploratory programming. It provides a no-fuss all-in-one package to start live development in Javascript, Python, and Clojure, and I’ve used all three.

It’s easy to use, too; just highlight a line containing a top-level function (JS & Python) or form (Clojure) and press Ctrl+Enter (Cmd+Enter on OSX) to evaluate it. The returned value is displayed right next to the last line, and output to stdout can be viewed in a tucked-away terminal.

Light Table is a really great project, and I’m looking forward to seeing what’s next It’s still in Alpha, and already it’s fantastic for what it’s best at.

An earlier version of this post asserted that virtualenv with python didn’t work, and that clojure editing outside a leiningen project was not supported. Well, the man himself found me on Twitter, and it turns out neither of these were true. The former is solved (on osx) by running LTCLI=true open -a LightTable, and the latter, well, I don’t know what was up but it got better when I removed and re-installed LightTable. Maybe I forgot to eval an (ns) block, that still seems to cause problems.

Clojure Live Development with Vim

I’ve been using Vim as my go-to editor for some years now. Whatever language I write, I use Vim (save Java, because tooling is really Java’s strong point, and because IntelliJ has a competent vim mode).

Clojure is no exception to this. However, when I started using it way back (early 2012), there was no really great way to make use of Clojure’s advanced REPL. I used lein-swank and slimv, but that arrangement broke often and easily, and was buggy at the best of times.

Thankfully, it’s been a whole year and a bit, and now there’s a new kid on the block: Fireplace.vim (Formerly foreplay.vim).

Written by Time Pope of Pathogen fame, Fireplace circumvents the problem of having a crappy repl window by instead providing a more vimmish experience. A quick set of keystrokes, and your code is evaluated and the result displayed in a temporary buffer.

LightTable may have become my tool of choice for Python and JS live editing, but Fireplace provides several killer features that make it (and Clojure by proxy) an exceptionally productive development environment

Getting Started

Fireplace uses nREPL to connect to a running Clojure repl if one is available. So, you’ll want to open up vim/gvim/mvim to your clojure source file in a lein project, then run lein repl to provide a place for that code to run. If no repl is running and you ask Fireplace to eval your code, it will still do it, but it’ll have to start up its own repl to do it, so you’re dealing with a JVM spin-up every time. Once the repl is running, though, evaluating forms is nigh-instant.

Evaluating Code

Once you open a file with vim, you can use :%Eval to evaluate the whole thing. (As is typical for vim instructions, I’ll just be listing the keys you need to press). Any errors will be reported. You can also select a bunch of lines using visual selection mode; using :Eval from there will evaluate just that block.

To evaluate a single form, use cpp. Between cpp and Eval, I’ve found all my in-place evaluating needs satisfied.

There are a few ways to access the REPL directly. To open up the Clojure REPL for a single statement, use cqp. To open up a small buffer that will be evaluated as soon as you press Return, use cqe.

If you change the ns form of a file, such as to add an include, or if you change a dependency, you can use cpr to Reload the current file in the repl.

Learning on the go

Besides evaluating, Fireplace.vim provides two small but important features. To read the documentation for a given function, put your cursor over it and press K. But, should the documentation fail you, you can press [d to view the source of the macro or function your cursor is on, right there in your vim window! This is an enormously useful feature when working with new and/or less-than-perfectly-documented libraries.

This is nothing special to an IDE user, of course, save being way faster and working from vim. But what more do you need?

If you are writing Clojure, and you’re not using some sort of integrated REPL in your development workflow, you are seriously missing out. And if you’re doing a lot of your development in Javascript or Python, go give Light Table a shot; I guarantee you’ll love it.