Platinum Partner
java,languages,tips and tricks,clojure

My First Clojure Workflow

So, I was working with Clojure and… well… it seemed to be the worst combination of slow start-up and slow compilation… and it's supposed to be a dynamic language. So, I pinged the very nice people in the Clojure community and they pointed me in the right direction.

Basically, everything you do in Clojure, you do in the REPL. The REPL is a live coding environment.

Test First

I had been writing code and tests and then checking on my tests with: lein test But that was taking way too long (20 seconds per cycle on my MacBook Pro).

So, how does one test first in Clojure?

First, one starts the REPL:

lein repl

Then one changes the namespace to a test package:

(ns plugh.file-test )

And get the clojure.test package's functions:

(use 'clojure.test)

And run tests:

plugh.file-test=> (run-tests)




Testing plugh.file-test




Ran 2 tests containing 2 assertions.
0 failures, 0 errors.
{:type :summary, :pass 2, :test 2, :error 0, :fail 0}

Edit the tests and the test target, reload the tests and re-run them:

plugh.file-test=> (use 'plugh.file-test :reload-all)
nil
plugh.file-test=> (run-tests)




Testing plugh.file-test




FAIL in (foo-bar-test) (file_test.clj:22)
expected: (foo-bar)
actual: (not (foo-bar))




Ran 3 tests containing 3 assertions.
1 failures, 0 errors.
{:type :summary, :pass 2, :test 3, :error 0, :fail 1}
plugh.file-test=> 

Fix the function and re-run the tests:

plugh.file-test=> (use 'plugh.file-test :reload-all)
nil
plugh.file-test=> (run-tests)




Testing plugh.file-test




Ran 3 tests containing 3 assertions.
0 failures, 0 errors.
{:type :summary, :pass 3, :test 3, :error 0, :fail 0}
plugh.file-test=> 

The magic is when we :reload-all, then all the dependencies for the package being used are re-loaded as well. But in the REPL, the reload process takes no measurable time. This makes the cycle very, very fast. Change code, switch to the REPL, up-arrow twice, hit return, up-arrow twice, hit return and voila… your new test results.

Re-running the app

Next I'm going to tackle re-loading and re-running the app. This should bring the Clojure experience to something faster than the Scala/SBT experience.

Thanks!

Thanks to the folks on the Clojure mailing list who pointed me in the right direction.

And I will always keep the REPL at the front of my brain.

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}