Monthly Archives: January 2013

“Growing Object-Oriented Software, Guided by Tests” (Book Review)

“Growing Object-Oriented Software, Guided by Tests” by Steve Freeman and Nat Pryce has been on my to-read list ever since I saw Steve at 33rd Degree 2011. Even though I did not really like the presentations, somehow I became intrigued enough.

The first two parts of the book explain what object-oriented programming and test-driven-development are. It explains the “tell, don’t ask”, encapsulation and information hiding, some pieces of design and architecture (ports and adapters). It contains a short explanation of what TDD is (the basic 3-phase cycle known to everyone) and extends it beyond unit tests, up to blackbox integration tests.

The third part is a long case study – writing a real, nontrivial application with Swing GUI, XMPP and interesting domain. The study is pretty long, and even though I’m not a fan of them this one is just perfect. It’s very easy to follow, taking small well-explained steps. It’s a rich and very practical example of TDD walking hands in hands with elegant object-oriented design.

The fourth part is a long and interesting catalog of “test smells” – shows the links between test complexity and readability and quality of covered production code; tells how to write elegant, useful tests and assertions; gives advice on how to write tests that won’t be too constraining in future; and more. The last part explores some practical sides of testing in multithreaded environment and around persistence.

I loved the book! It’s an awesome piece of work that covers so many topics in such a clear, deep and interesting way that I would call it a must read for everyone. It’s a great book on testing, but not just that. It shows where testing fits in the software development process and explains the very important link to object-oriented design. It’s full of small, hidden gems (not only about software testing itself) that I will remember for a long time. At the same time it’s also very practical, pragmatic and concise.

I think it has much to offer to people of any skill level (perhaps except for novice). Even though I have been doing OOP and TDD for years, I really enjoyed it and I believe I learned much new stuff.

If you haven’t done so yet, do yourself a favor and go read it now!

ClojureScript Painkiller (for OOP)

When I learned and used ClojureScript, I really hated writing code that looks like this:

(defn Bag []
  (this-as this
           (set! (.-store this) (array))
           this))

(set! (.. Bag -prototype -add)
      (fn [val]
        (this-as this
                 (.push (.-store this) val))))

(set! (.. Bag -prototype -print)
      (fn []
        (this-as this
                 (.log js/console (.-store this)))))

(def mybag (Bag.))
(.add mybag 5)
(.add mybag 7)
(.print mybag)

That’s so much ceremony and repeated waste!

I know it’s not how you’re supposed to write ClojureScript, but sometimes you have to (for example when working with OO libraries).

What do you do with such code? Cover it with macros!

So I wrote two little macros that you can just pick up and use right away in your ClojureScript, so that it can look like:

(defn Bag []
  (this-as this
           (set! (.-store this) (array))
           this))

(set-obj-fn Bag.prototype.add [val]
            (.push (.-store this) val))

(set-obj-fn Bag.prototype.print [val]
            (.log js/console (.-store this)))

Or just a this-as shortcut:

(set! (.. Bag -prototype -add)
      (obj-fn [val]
        (.push (.-store this) val)))

Source with complete sample is on github. The library is on Clojars, so to use it all you need is [cljs-painkiller "0.1.0"] in your project.cljs. Enjoy!

Get Started with ClojureScript with Leiningen Templates

When I was about to get started with ClojureScript, I was discouraged by the fact that I apparently had to figure out so much before getting a trivial project up and running.

Eventually I learned, built and showed a minimal application running with just Leiningen and Ring, and a little bit of jQuery in ClojureScript.

Some time later Kyle Cordes showed me cljs-template. It’s a Leiningen template created by Chris Granger, also known as the guy behind Noir and Light Table. That was quite fun. All you need to get a project up and running is:

lein new cljs-template my-project
cd my-project
lein run

That’s it, you’re now running a Noir application with ClojureScript in client (jQuery included). You can start hacking at the CLJS source and see changes in browser immediately.

I soon discovered that it was a few months old, using Clojure 1.3, dated build of ClojureScript and pretty much everything. Eventually (thanks to Kyle and Raynes) I got push access to the project and updated everything, so it should be in even better shape now.

I am not sure where cljs-template is going though, with Noir itself going away. I also found one bit missing: That template is awesome to get up and running and show off a demo, but you would still need to do some manual plumbing to make such a project work for a real application (with leiningen hooks on compilation etc.).

That’s why I created another template: cljs-kickoff. Like my first steps, it’s really minimal: just Ring, lein-cljsbuild and ClojureScript. Fewer files, fewer dependencies, very easy to grasp.

To kick it off, just run:

lein new cljs-kickoff my-project
cd my-project
lein run

It will compile the ClojureScript file included in the project and start Ring server with it.

In another shell, you can run:

lein cljsbuild auto

This will start lein-cljsbuild in the auto-compile mode. Whenever the CLJS source changes, it will be automatically recompiled and the running application will pick it up after reload.

Compared to cljs-template, this template is much smaller and only uses very basic, popular and mature pieces (just Ring and CLJS). It also has all the “real” Leiningen hooks in place: CLJS compilation is included in lein run, lein jar and lein uberjar.

I hope it all makes someone’s life easier by making the first step on CLJS path as easy as possible. Happy hacking!