Braceless if considered harmful

Monday, 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

(more…)

A minor glitch in type-safe enums

Saturday, 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?
(more…)

The Cafes Reloaded

Wednesday, 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.)
(more…)

Ant Tip 1: Write a master build file

Monday, January 9th, 2006

Many Java projects are divided into multiple subprojects or modules, each in its own directory. Often you’ll want to build subprojects individually, without necessarily building all of the larger master project. For example, in my XOM project, I have one master build.xml file that builds the software itself, and another build.xml file in the web directory that builds the web site. jEdit is divided into separate jedit, jeditshell, macros, and plugins directories, each of which has its own build.xml file.

Furthermore, projects may have dependencies on other projects. For instance XOM 1.1, JDOM, and dom4j all depend on Jaxen. For the latest and greatest JAR, Jaxen should be rebuilt using its own build.xml file, rather than bundling a stale JAR archive that’s months or even years beyond its expiration date.

You could cd into each separate diretory and type ant compile in each one, but that’s time conmsuming and error prone. Plus interproject dependencies may require this to be done in a precise order that makes this even more error-prone. It’s preferable to create a master build file at the top level that compiles everything by invoking targets in the other build files. The ant task lets you do this. For example, this task executes the build.xml file in the directory website (relative to the directory where the current build.xml file is) :
(more…)

Ant: A Case Study in How Not To Write An Error Message

Monday, May 2nd, 2005

After I complained about about build failures in Ant 1.6 on Cafe au Lait, a couple of Ant developers wrote to me with various suggestions and FAQ references. They were nothing I hadn’t tried before though, and didn’t explain why I could make the build work with Ant 1.5 and not Ant 1.6. However what really annoyed me about this whole situation was not the bug. It was the lazy error message.
(more…)