Archive for May, 2008

JAOO Brisbane 2008

Saturday, May 31st, 2008

When I saw the schedule for JAOO 2008, two names stood out for me; Erik Meijer (Haskell) and Don Syme (F#). My second thought was “why are the smart guys in Microsoft Research?”. I don’t really have an answer to that, but it’s an interesting question.

Meijer opened with a 1 hour talk on the software crisis and how we can solve it. A quick glance around the room quickly confirmed my suspicion that much of the audience was lost with the lambda-notation and various other squiggles on his slides, but deep inside, I could feel myself pushing him to continue. He had hit the nail right on the head with how to solve the so-called software crisis (perhaps an alarmist name, but there is a phenomenon of some sort).

I was disappointed that I could not disagree with Erik — not even in the slightest detail. When I saw him emphasising his displeasure with Haskell’s unsafePerformIO, I thought I had him. This was my opportunity to disagree with someone smarter than myself. But it wasn’t to be, he answered my question with almost exactly the same rhetoric that I would have used myself. I just think his emphasis on unsafePerformIO was because he knew he was standing in front of mostly Java/C# users. Perhaps that’s the lesson to learn; target your audience the way he did. A consolation prize at the very least.

From the introduction, I knew I was in for a great ride, however, I believe there were only two types of people in theatre during Meijer’s speech:

  • those who understood what he was saying in his limited time slot
  • those who didn’t quite understand but really want to, whether they know that fact or not

It is this second point that I wish to elaborate. I found many speakers thereafter had a common theme in trying to resolve some sort of crisis in software development; Dave Thomas and some of the problems he described, Rob Martin and his Clean Code, Michael Feathers and his Unit Testing and even Martin Fowler and his Enterprise Patterns. With some deeper thought, you can see that these speakers are — in some vague sense — highlighting the fact that there exists some kind of critical problem and each his trying to put forward his/her answer. Many of these speakers described the same problem, but then gradually and sometimes quickly diverged from what I think is the correct answer. Meijer nailed it guys! No seriously, the solution is in his slides and his exaggerated body language.

I know many people think Meijer’s solution and mainstream topics are disjoint and not unifiable. How could Meijer’s funny lambda symbols have any bearing on Fowler’s enterprise pattern or Feathers’ insistence on not testing that (dang side-effecting) database? No, there is no fence to jump. All these guys are just acknowledging the same problem in some vague notion and looking for an answer, believing they have found it. It is the realisation of the unification of this funny mathematical stuff with the euphemistic terms like “TDD” and “Agile” that brings the greatest reward. Is it not a big enough hint that the in-crowd is reinventing some of this stuff in a diluted and not-so-useful form?

Rob Martin, yes, the world is counter-intuitive and your analogy to Quantum Mechanics is perfect. However, what would Einstein say to your shunning of the application of thought experiment? You nailed the problem, but the solution was slightly misdirected. Meijer was pointing you to the answer. The theoreticians have it right and in the most practical sense; is history not a good indicator? No Rob, the jury is not in; you’ve screwed up. Are you open to this possibility? Any professional would be ready to accept an unheard argument, right?

If, as a participant of JAOO 2008, you were to take the time to truly and deeply understand the topic that Meijer was alluding to, then I give you an unreserved promise of a moment of realisation that will tingle your spine, fill your eyes with water and euphoria (you know that feeling right?) that far supersedes any other moment that you may have had in your programming journey so far. I say this with utmost confidence. And here’s the thing, there is an entire community of people waiting to help you to understand that funny lambda symbol, those strange arrows, what it means to write code from types, what code documentation and verification truly means and much more. We love answering your questions! The scientific community truly embraces ignorance; no really it does. Please don’t be afraid to express it; learning is fun.

So here’s my little contribution to the progress of software development; I’ll happily answer your questions about Meijer’s topic right here or by private email if you prefer (however, I don’t have the slides, so you’ll have to be specific in your query). Hopefully then, you can open JAOO 2009 and I can indulge myself by disagreeing with you in some way! :)
From a fellow programmer who wants to solve these problems too so please ask questions.

Reductio: Testing equals/hashCode

Tuesday, May 27th, 2008

So you wanna know how to test Java’s equals/hashCode huh?

Here is a true statement that should hold for any equals and hashCode implementation:

forall x. forall y. x.equals(y) => x.hashCode() == y.hashCode()

You can read => as implies. So if x equals y, then this implies that the hash code of x is equal to the hash code of y. This is called logical implication and is often written in English as “if p then q”, for some p and q, just like I did in the previous statement. Easy peasey eh?

How might we test the equals and hashCode implementation of MyClass? If we were using JUnit (or some other non-automated test tool):

assertEqual(new MyClass(7, "abc").hashCode(), new MyClass(7, "abc").hashCode());
assertEqual(new MyClass(42, "CBA").hashCode(), new MyClass(42, "CBA").hashCode());
... etc. etc.

What we would really like to say is something like:

I assert that for any two equal instances of MyClass (call them x and y), then the hashCode of x is equal to the hashCode of y. Dear test tool (Reductio!), please try your best to show me why this is false if you find it to be so.

Well, that’s just an English way of saying the logical implication statement above, right? We could write this statement in Java (boiler-plate omitted):

Property p = property(arbMyClass, arbMyClass, { MyClass m1, MyClass m2 =>
      bool(m1.equals(m2)).implies(m1.hashCode() == m2.hashCode()) })

Read on…

Reductio: Testing for the Top Java Programmers

Saturday, May 24th, 2008

Reductio is open source (BSD) software that provides some very clever ideas adapted from QuickCheck. In particular, Reductio allows you to write algebraic properties about your software and the software then attempts to falsify these properties using automation of function domain value generation. Reductio can run 1000 unit tests in 4 or 5 lines of code, and that is Java code! (No, I am not kidding; really take a look at some of the code examples)

Reductio also provides shrinking of counter-examples in the event of proving a property false; for example, a + b == a - b is not always going to be true (for all values of ‘a’ and ‘b’), but would you like Reductio to provide the counter-example a = 1934, b = 4571? I think you’d prefer a smaller counter-example such as a = 1, b = 1 right? I would too :) By providing the smallest reasonable counter-example, Reductio makes it easier to debug your code in the event of finding fault.

Another extremely useful feature of Reductio is automated mocking. That is to say, Reductio can generate implementations of an interface for you so long as you provide the mapping from one or more transformations (this exploits a programming pattern called a Functor) to your type. Doing so is guaranteed to be a straight-forward task for the user (so straight-forward that you could automate it with reflection; an add-on feature perhaps?). This allows you to quantify across interface types. For example, you might state a property about your software using some interface, but you assert that the property holds regardless of the implementation of the interface. Reductio will generate hundreds of implementations if you like! All automated of course ;)
Reductio makes it trivial to write generators for user-defined types (again, another potentially automated task that requires Java’s meta-programming facility ala reflection), however, Reductio also provides many existing implementations. Following is a non-exhaustive list.

If you’re into robustness in the verification of the correctness of your Java software, take a look at Reductio.
http://reductiotest.org/

———————————————————————————————————

java.lang
  • Boolean
  • Byte
  • Character
  • Double
  • Float
  • Integer
  • Long
  • Short
  • String
  • StringBuffer
  • StringBuilder
java.util
  • ArrayList
  • BitSet
  • Calendar
  • Date
  • EnumMap
  • EnumSet
  • GregorianCalendar
  • HashMap
  • HashSet
  • Hashtable
  • IdentityHashMap
  • LinkedHashMap
  • LinkedHashSet
  • LinkedList
  • PriorityQueue
  • Properties
  • Stack
  • TreeMap
  • TreeSet
  • Vector
  • WeakHashMap
java.util.concurrency
  • ArrayBlockingQueue
  • ConcurrentHashMap
  • ConcurrentLinkedQueue
  • CopyOnWriteArrayList
  • CopyOnWriteArraySet
  • DelayQueue
  • LinkedBlockingQueue
  • PriorityQueue
  • SynchronousQueue
java.sql
  • Date
  • Time
  • Timestamp
java.math
  • BigDecimal
  • BigInteger

ScalaCheck 1.3 yippee!

Monday, May 12th, 2008

ScalaCheck 1.3 is available.

  • ADDED Arbitrary instance for functions.
  • FIXED ConsoleReporter updated according to changes in Scala’s printf method.
  • CHANGED Properties and Commands now extend Prop, so they can be used as single properties.
  • ADDED Properties.include for grouping properties.
  • ADDED Prop.main for convenient test execution.
  • CHANGED Fixed Arbitrary for Option so that None does not occur so often.
  • ADDED Arbitrary instance for scala.Either (Added in Scala 2.7.1-RC2).
  • ADDED Existential quantifier in Prop.scala.

How to Share?

Thursday, May 8th, 2008

I have a situation that I am wondering how others may have solved. I have an open source (BSD) project whose copyright is owned by the company I work for. Another person, working for another company (in another country!) wishes to contribute to this project, which I think is great.

How is the ownership of copyright usually sorted out in these situations? Is it shared by both parties? Does each party maintain copyright to their respective contributions? Does the party wishing to contribute sign over copyright ownership? Is there another alternative that I haven’t thought of? Surely this situation comes up a lot; how is it usually handled?