Trying ClojureScript

First of all I should say that ClojureScript is a great tool for writing single page applications with complex client-side logic (take a look at prismatic, they use it). Another great feature of ClojureScript is that you can re-use you server-side clojure code in your scripts.

But here I want to list some small disadvantages, things that I found uncomfortable while trying ClojureScript.

Logging

Remember old good console.log:

  console.log('Hello world');

Which looks more cleaner in CoffeeScript:

  console.log 'Hello world'

Now look at ClojureScript’s version:

  (.log js/console "Hello world")

You can search github and you’ll find code like this:

  (defn log [obj]
      (.log js/console (pr-str obj)))

or this:

  (defn log [& more]
    (.log js/console (apply str more)))

People don’t like to be typewriters :)

JavaScript objects

Every time you want to interact with js objects you need to use js-obj construct:

  (js-obj "foo" 1 "bar" 2)

For example to send some json to server you’ll need:

  (send-to-server (.stringify js/JSON (js-obj "foo" bar)))

Again people end up with helper functions for clojure map -> js object conversion:

  (defn clj->js
    "makes a javascript map from a clojure one"
    [cljmap]
    (let [out (js-obj)]
      (doall (map #(aset out (name (first %)) (second %)) cljmap))
      out))

</script>

Seems like there is an issue for that in clojure’s JIRA.

Debugging

Sometimes ClosureScript compiler crashes with NullPointerException and without ANY line number. It is very difficult when you need to comment lines to guess which one is failing.


All of these I’ve experienced during conversion of chat-websocket js scripts to ClojureScript. I think that ClojureScript is not good for small utility scripts because of the runtime and necessity for helper functions. Next time when I need to write some simple frontend code I’ll use CoffeeScript. But I’m still learning and trying cljs, so there will be other posts about it.