Eric Lefevre-Ardant on Java & Agile

June 4, 2010

Bob Martin on TDD in Clojure

Filed under: tdd — Eric Lefevre-Ardant @ 1:54 pm

Robert “Uncle Bob” Martin has just blogged on the differences in TDD styles using Clojure, as compared to more traditional languages such as Java. Though I am a Clojure-newbie, I mostly disagree with his conclusions.

His main point is that, because Clojure is a functional language, functions have no side-effects and therefore can be used directly in the tests.

For example, the production code

(defn update-all [os]
  (update os))

would be tested with something like

(testing "update-all"
  (let [
    o1 (make-object ...)
    o2 (make-object ...)
    os [o1 o2]
    us (update-all os)
    ]
    (is (= (nth us 0) (update o1)))
    (is (= (nth us 1) (update o2)))
    )
  )

There is no reason to believe that the (update) function is side-effect-free

Changing internal values is only one way of creating side-effect. I admit that Clojure encourages coders to write code that does not change variables (if I got it right, it is definitely possible to do so, but with some additional work). However, that effect only stops at the boundaries of the language. At some point, it might access the file system or a database. Clearly, the state might change there.

Correct implementation of the (update-all) function depends on the correct implementation of (update)

Bob Martin says: ”this test simply checks that the appropriate three functions are getting called on each element of the list”.
Suppose that the (update) function does not do anything or maybe does something that does not return a value, such as printing out to the console. Then, calling it will have the same effect as not calling it at all. The test above will pass even if the (update-all) function does not provide any implementation at all. When, later, the bug is found, it will be harder to fix.

The test could be clearer (with a more powerful test framework)

One of my biggest concerns is that the test looks a lot like the code itself. Looks like duplication of information to the reader.
If there was a mock framework for Clojure, I would expect to see something like

(testing "update-all"
  (let [
    pre-conditions (
      (should-return (update 1) 1.5)
      (should-return (update 3) 3.0) )
    o1 (make-object 1)
    o2 (make-object 3)
    os [o1 o2]
    us (update-all os)
    ]
    (is (= (nth us 0) (1.5)
    (is (= (nth us 1) (3.0)
    )
  )

Bob Martin is right to conclude that “Clojure without TDD is just as much a nightmare as Java or Ruby without TDD.”

But he should also make it clearer that it is lacking a mock framework (he does point to Brian Marick’s work on this).

It should be noted that it is possible to get a similar implementation style in Java as in Clojure, though it is significant work. In fact, that’s often how we use it here at Algodeal. That means mostly relying on immutable objects and state-less methods. Immutable collections from Google Collections help a lot, too. Still, we like to use mocks in our tests (too much for some, probably).

In the end, Uncle Bob’s post is another aspect of the (almost) age-old debate described by Martin Fowler: classicists vs. mockists. If you haven’t already, read Fowler’s article, it’s worth it.

October 21, 2008

More about Coding Dojo at CITCON Amsterdam

Filed under: citcon, tdd — Eric Lefevre-Ardant @ 9:52 pm

As a nice followup to my earlier post, Willem did a great write up of our Coding Dojo session at CITCON Europe, in early October. His post is supplemented with pictures by Marc.

October 8, 2008

Coding Dojo on Legacy Code

Filed under: citcon, facilitation, tdd — Eric Lefevre-Ardant @ 5:31 pm

At CITCON Amsterdam last WE, Willem van den Ende and I facilitate a Coding Dojo session on both Mockito and Legacy Code.

Willem and I thought that we had missed some of our goals (especially demonstrating Mockito), but still many people at the closing session mentioned that they enjoyed it :-)

Coding Dojo with Legacy CodeWe tried to prepare a bit before diving into the session. However, in practice, it became a bit chaotic, as many participants tried to make their opinion heard. This was very different than when I do that in trainings or even at Valtech. I guess this is because there were quite a few people (~20) and most of them were rather experienced and strong-headed (I guess they wouldn’t be at such a conference otherwise!).

Coding Dojo with Legacy CodeWe managed to have a quick retrospective at the end. Here are some of the things we learnt:

  • We need to prepare the session better; half of the session was spent fixing an existing bug in the original application, instead of adding features. Also, Willem wanted to take advantage of Mockito, which was quickly forgotten as people concentrated on fixing the bug.
  • At first, do Safe Refactorings (the ones that can be done automatically by Eclipse and such tools), though you still need to be careful of what you are doing
  • It is OK to add code, not OK not remove code from the legacy application
  • Add collaborators using setters to facilitate testing
  • However, be careful: getters and setters can quickly get out of hand
  • Singletons are bad, not just because they make code hard to test, but also because they create secret coupling
  • Apparently, many (bad) coders want to write singletons and static methods (which are both frown upon in the context of testing), simply because they do not want to type the ‘new’ keyword
  • We could try to rotate the copilot on a longer basis (for example, every 10 mins, instead of every 5 mins for the coder)
  • I (Eric) got concerned that the Coding Dojo format prevents participants from thinking; facilitating the session sometimes seemed to be like herding cats
  • it was clear that many people was interested in working on legacy code
  • in the end, a Coding Dojo with an appropriately complex application seemed like a good way to learn about handling legacy code
  • I need to experiment a bit more with Coding Dojo for Legacy Code, but I feel that the format is rather good. Next year, we’ll do better!

    October 1, 2008

    Next Talk: TDD Training

    Filed under: tdd, valtech — Eric Lefevre-Ardant @ 9:04 am

    I am giving a 3-day TDD training session at Valtech Training premises, from Monday, October 6th. I’ll introduce a Coding Dojo, like we did in the TDD training last week.

    September 28, 2008

    Improvised Coding Dojo

    Filed under: tdd, valtech — Eric Lefevre-Ardant @ 11:50 am

    Coding Dojo at AgileOpen 2007In the past three days, I have taught Test-Driven Development to a group of Java developers in Brittany. I thought it was a good idea to arrange an inpromptu Coding Dojo. That contained a moderate risk, as all dojos I knew of were attented by volunteers.

    It turned out very well. Though one or two participants managed to check their emails at some point during the session, most were paying attention at all times.

    A couple of things to remember for future dojos:

     

    • In the past, I have usually attented dojos were either everyone was in front of a computer (either coding or co-piloting), or a single pair was coding in front of the others. After watching Dave Nicolette and Ryan Hoegg facilitate a dojo where all got to code in front of the public at Agile 2008, I thought this was a good way to get participants to do their best, plus let others to read code with a watchful eye.
    • A participant mentionned that the exercice helped him realize that names for test methods were very important. Yay!
    • For first-timers, always start with a very, very simple exercice. We did the dictionary kata (given a collection of words, find all words in a String that are not in the collection). We managed to complete it satisfactorily in 2x 30 minutes.
    • We did our best to run through the code Test First, writing the tests with as little prior thinking as possible. That said, one participant suggested that pairs should talk a little before doing so, in order to get the pair to bond better. Sounds like a good suggestion, though it would probably require more time than what we had available.

     

    All in all, I am happy with the outcome. We should include it in our course curriculum.

    May 12, 2008

    A few words on Test-Driven Development

    Filed under: tdd, test, valtech — Eric Lefevre-Ardant @ 10:27 pm

    Cédric Dhénin from TV4IT has an interview of me talking about TDD (in French). (more…)

    October 24, 2007

    CITCON Brussels 2007: Jester & Jumble

    Filed under: citcon, java, tdd, test — Eric Lefevre-Ardant @ 9:03 pm

    I was very excited when Squirrel suggested a topic about mutation testing. I had looked at Jester & Jumble before, but gave up after a couple of hours, as they are not very easy to setup, and mostly not maintained.

    The idea of those tools is to make changes to the source code (Jester) or the bytecode (Jumble), and check if the tests still pass. If they are well written, they should fail when the code is changed. If not, then the code is not covered properly. Ideally, you should only have one test failing; otherwise, it means that you have redundant tests.

    Ivan, the creator of Jester, was present, but admitted that he had not even used it in years. After seeing the interest of the participants, he seems willing to give it more time. Hurray!

    Check out my notes on the conference wiki.

    October 2, 2007

    CITCON Brussels Registrations are going nicely

    Filed under: citcon, continuous integration, tdd, test — Eric Lefevre-Ardant @ 11:47 am

    We now have more than 80 registrants for CITCON Brussels, more than 2 weeks before the 19th.

    This year, I have volunteered to help organize registrations, so I get to see every single registration request. It’s interesting: sometimes, you don’t see anyone registering for days (oh my god, is this going to be a turkey?, then 7 people register in a single day. The general pattern, though, is that the number increase more or less regularly.

    Also, about one request in six is pure spam.
    We should reach the threshold for the maximum number of seats a couple of days before the event.

    February 22, 2007

    Test Driven Design and Data Access Layer

    Filed under: hibernate, java, tdd, test — Eric Lefevre-Ardant @ 10:50 am

    I am currently writing the Data Access Layer of a Java project using Hibernate and I want to apply TDD on my work. The part that is concerned with the database is the mapping of objects on the database schema, and the queries.
    As a recap, there are the options:
    (more…)

    December 7, 2006

    Great interest in OpenSpace and FIT technologies

    Filed under: java, openspace, tdd, test, valtech — Eric Lefevre-Ardant @ 1:11 pm

    I arranged a presentation last week to my colleagues, with Guillaume Tardif as co-host. We were quite pleased that many more people than expected showed up.

    The most part of the presentation was about OpenSpace Technologies. We are planning to arrange one such conference within Valtech next year. I cannot wait! (15/02/07 update: it did take place!)

    Jerome Baumgarten thought that OpenSpace was similar to BarCamp. I rather disagree, as BarCamp seems focused on coding, or at least on very geeky activities. OpenSpace is more about getting people from different background together, even the security guy (see Rockport Shoes). Not to say that BarCamps are not useful; I’m hoping to participate in one next year.

    The, there was the presentation on FIT, and the success we met when introducing it in a bank.

    We had planned more talks about QALab and Agitar, but it appeared we foolishly underestimated the popularity of OpenSpace and FIT!

    Powered by WordPress