Experimental Programming

January 23rd, 2006

One advantage of test-driven development I’ve rarely seen emphasized is that it enables experimental programming. This was recently brought home to me while I was working on XOM. Steve Loughran had requested the ability to use a NodeFactory when converting from a DOM document. It was a reasonable request so I set off to implement it.

My first iteration satisfied Steve’s use-case. It was actually a fairly major rework of the code, but the test suite assured me that it all worked and I hadn’t broken anything that used to work. However, Wolfgang Hoschek quickly found a real bug in the new functionality I had added; and things got a little sticky here.

To satisfy John Cowan, the DOMConverter uses some fancy non-recursive algorithms. This enables it to process arbitrarily deep documents without stack overflows. However the algorithm is far from obvious; and even though the code is well-written it’s hard to come back to it a couple of years later and figure out just how it works.

But this is where test-driven development really shines: I don’t have to understand the code to fix it. Read the rest of this entry »

Braceless if considered harmful

January 16th, 2006

There are some things I’m reluctant to write about because everything that needs to be said about them has long since been said. This post falls into that category. Peter van der Linden explained what I’m about to say in Expert C Programming: Deep C Secrets years before Java was released, and I have nothing to add to what he wrote. Nonetheless very few C or Java programmers got his message so it’s worth saying again, and repeating until the community finally learns.

Always use braces on multiline if statements

Read the rest of this entry »

A minor glitch in type-safe enums

January 14th, 2006

I’m quite fond of the type-safe enum design pattern. However, Wolfgang Hoscheck recently identified a place in XOM where I had introduced a bug as a result of it. Here’s an example:

public class FontStyle  {

  private int value;

  public static FontStyle PLAIN  = new FontStyle(1);
  public static FontStyle BOLD   = new FontStyle(2);
  public static FontStyle ITALIC = new FontStyle(3);
  public static FontStyle BOLD_ITALIC = new FontStyle(4);

  private FontStyle(int value) {
    this.value = value;
  }

}

The bug shows up in another class that reference the FontStyle
class:

  private FontStyle style;

  public void setStyle(FontStyle style) {
    this.style = style;
  }

Do you see the bug? The problem is that there is one thing that can be passed into setStyle that is not one of the four legal values. Can you guess what it is?
Read the rest of this entry »

Do the Wrong Thing

January 12th, 2006

There’s a disturbing belief found throughout the programming community that it’s OK for an application to do the wrong thing by default, as long as there’s a way for the user to do the right thing. Stated that blatantly, it’s obviously false; and yet I keep running up against this belief time and time again in widely separated areas. Here are just a few:

Read the rest of this entry »

The Cafes Reloaded

January 11th, 2006

Friday I spent most of the day converting the old custom written site into a new spiffy WordPress 2.0 site. Why I did that was outlined in my previous post, Why Mokka mit Schlag?

Then I promptly caught writer’s block. For a good 48 hours I could think of absolutely nothing to say here. I could have pulled an old article out of the vault, but I wanted to relaunch with something new. So I went on a parrot walk at Brooklyn College, did a little owling in Prospect Park, and got to work on my notes for Macintosh Development with Java at Software Development 2006 in Santa Clara in March. This is a revised version of the Macifying SWT talk I gave at EclipseCon in New York in the Fall. However, this time the focus will be on Swing rather than the SWT. (Swing does a much better job at handling the Mac.)
Read the rest of this entry »