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 »

Verifying SSH Host Fingerprints

January 10th, 2006

If you use ssh you’re familiar with this message:

~$ ssh rich.elharo.com
The authenticity of host 'rich.elharo.com (192.168.254.36)' can't be established.
RSA key fingerprint is 5a:65:0f:5f:21:bb:fd:6a:5a:68:cd:62:58:5d:fb:9f.
Are you sure you want to continue connecting (yes/no)?

If you’re like me, you regularly just type yes and continue without much thought:

Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘rich.elharo.com,192.168.254.36’ (RSA) to the list of known hosts.

But is that really safe? Read the rest of this entry »