Testing times

Some thoughts on testing while I’m waiting for my commit email from CruiseControl…

Inversion of Control: Pass stuff as parameters, don’t instantiate objects inside methods just to make queries on them. It makes it much easier to use mock objects or stubs if the things you need are passed as parameters. If I had to come up with a pithy catchphrase for this it would be something like: “Methods should only create objects that will be returned from the method.”

This leads me neatly onto my other thought for the day – “Tell don’t ask”. Code should look like a sequence of commands, not a bunch of questions. If all you are doing is querying an object about its state, shouldn’t that code be inside the object? I think the anti-pattern is called something like Feature Envy, or Data Envy or something. Code that follows the ‘tell don’t ask’ principle is generally more polymorphic, more modular and easier to refactor.

Like stress? Try DIY

I’m not talking about putting up a couple of wonky shelves here. Oh no. As you may recall, I had been without heating or hot water for three weeks. The repairs were covered by British Gas, who eventually (after several ‘robust’ telephone conversations) sorted someone out to fix it last monday. This involved moving the lounge radiator to a different wall and running a new section of pipe to it. As a result I was left with two pretty deep holes in my floor (which is concrete, despite being on the second floor). My cover didn’t include making good, so this weekend it was off to B&Q and a spot of repair work. It was not a good day. And my choice of career was once again vindicated – I would be a truly awful builder.

Most of the day was spent either (a) panicking, or (b) waiting. I now know far more than I ever wanted to about how to prepare a concrete floor for filling, including the exact ratio of water to PVA needed to seal it (3:1) or form a bonding coat (1:1). I also know that self-leveling floor filler requires a hole deeper than 6mm to be filled with pea gravel to a depth of 5mm below the surface. Fascinating. Hah.

Of course no DIY story would be complete without a humorous cock-up, so I went the extra mile and had two. Firstly, about halfway through the day I locked myself out of my bedroom when the door first swung open, hitting the door stop (which is at the exact height to activate the push-button lock thingy on the inside handle), then swung closed. With my keys inside. That was the low point of the day. I took a screwdriver to it. It didn’t last long.

The second error was failing to realise just how much filler it would take (despite buying a huge tub of it, which I thought would be about 50% too large), leaving the second hole with about a centimetre to go. Naturally by this time it was too late to do anything about it. Luckily I was too drained to care. But on the bright side, it does mean I get to have almost as much fun tomorrow evening as I did yesterday. Can’t wait…

Erlang. Exceedingly Exciting

How cool is this:

Controlling windows 95 from a remote machine
This shows how to set up a two node distributed system. One node runs windows95 Erlang, the other runs linux Erlang. There are two example programs. A program which allows the linux Erlang to manipulate the windows 95 registry and a bouncing ball that bounces back and forth between the windows and the linux machine – the latter example illustrates platform independent graphics, and various distributed programming techniques.

And the winner of the language to learn this year prize is… Erlang.

(Added later: JInterface – an erlang/java interface module. Groovy.)

Final finality

Lots of good comments were made about my last post. Just wanted to add my own thoughts (its my blog after all!). The example is inane and trivial, and yes it could be refactored in many other ways. The point is, if you have something that must always occur just before the execution thread leaves a block of code, a finally block is the way to go. Its also interesting that while the try/catch/finally form is well known, try/finally is much less frequently seen.

Finally blocks

Finally blocks are underrated. They’re really very useful for removing duplication in code with multiple exit points. Consider the following:

public int foo(boolean condition1, boolean condition2) {
setMouseBusy();
if (condition1) {
doOne();
setMouseNormal();
return 1;
}
if (condition2) {
doTwo();
setMouseNormal();
return 2;
}
setMouseNormal();
return 0;
}

Compare with this:

public int foo(boolean condition1, boolean condition2) {
try {
setMouseBusy();
if (condition1) {
doOne();
return 1;
}
if (condition2) {
doTwo();
return 2;
}
return 0;
} finally {
setMouseNormal();
}
}

Why is the second form better? Because no matter how the method exits (including throwing exceptions), the finally block will be executed. In the first form, if either of the ‘do’ calls throw an exception the call to setMouseNormal will be skipped, possibly leaving the application with a misleading hourglass cursor.

No, please no!

This is just horrible! How many characters needed? I can feel my wrists ache just reading it (which is also strenuous due to all the XML taggage). XML is designed to be easily parsed by machines, not people! What is the point of a language where ‘XML is the underlying construct, not ASCII’? Call me a luddite, but I mean really, what does it offer over good old fashioned text?

Cool Programming Language Concept: SuperX++.

Cool Programming Language Concept: SuperX++

This is neat: Super X++.  It is a language where XML is the underlying programming construct as opposed to ASCII.  And, yes Virginia, it is Open Source.[_Go_]

Here is a simple example from the FAQ:

How do I code “Hello World!” in Superx++?
The following code will be a full Superx++ program that returns the string “Hello World!” to the Superx++ client (whatever process calls a Superx++ program):

<xpp>
   <xout>Hello World!</xout>
</xpp>

And here is a complex example:

How do I define a class?
A class in Superx++ is defined using the <class> statement. An example follows:
<class name=”XTree” inherit=”XPlant”>
   <construct>
      <scope type=”public”>
         <Chlorophylic>yes</Chlorophylic>
      </scope>
   </construct>
   <scope type=”public”>
      <func type=”string” name=”GetChlorophylic”>
         <body>
            <return>
               <eval object=”Chlorophylic” />
            </return>
         </body>
      </func>
      <func type=”void” name=”SetChlorophylic”>
         <parm type=”string” name=”sVal” pass=”val” />
         <body>
            <eval object=”Chlorophylic”>
               <eval object=”sVal” />
            </eval>
         </body>
      </func>
      <func type=”int” name=”GetAge”>
         <body>
            <return>
               <eval object=”this” member=”Age” />
            </return>
         </body>
      </func>
      <func type=”void” name=”SetAge”>
         <parm type=”int” name=”sVal” pass=”val” />
         <body>
            <eval object=”this” member=”Age”>
               <eval object=”sVal” />
            </eval>
         </body>
      </func>
   </scope>
   <scope type=”protected”>
      <var type=”int” name=”Age”>0</var>
   </scope>
</class>

The statement above declares a class called XTree which inherits from the class XPlant which contains an object called Chlorophylic. Every time an object of class XTree is instantiated it will be instantiated along with a contained object called Chlorophylic. The class XTree also defines four methods and one member variable. For more details on classes click here.

Thanks to Dr. Dobbs Journal for turning me on to this.

[The FuzzyBlog!]

Language Lessons

Joe’s Jelly

…it’s a new year now and I’m looking for a new language. So what do I learn?

Smalltalk has been recommended but I think I may have already learned some of it’s most important lessons through Ruby.

From chatting to colleagues, I’ve narrowed the list down to:

  • LISP (with CLOS and MOP)
  • Scheme
  • Haskall
  • Self

So, which one should it be? How will they change the way I think? What will you be learning this year?

Last year I looked at:

Python, Ruby, Lisp, Scheme and Haskell. I bought books about 2 of them. I actually ended up learning Perl. Which, if you’re a zealot, means you learned the best bits of all of them. Not my place to comment on that though. I also dabbled a bit in Smalltalk, to the extent I can now read it, if not yet code fluently in it.

From your list, Joe, I’d say go for Scheme if you feel like exploring just how much can be done with just lists and heaps of recursion. Or try Haskell for some truly functional coding. Both paradigms are significantly differerent from (ahem) run-of-the-mill OO development.

My current thoughts for this year are that I want to try Ruby or Haskell or something truly off the wall like OCAML or Erlang.

I’ll probably end up learning Python as I’ve started getting into Marathon Man, which uses Jython to script its tests. I find learning a new language without a context to apply it to somewhat dry and rather unfulfilling. Its similar to how much more quickly you learn a foreign tongue when you are in that country than you do from books and lessons.