Safer Java: Constants First

22. January, 2008

Here is a simple change in your Java development style that will save you a lot of time: When comparing something against a constant, always put the constant first. Examples:

    if (0 == x)...

    public final static String RUN = "run";
    if (RUN.equals (mode))...

That will look strange at first because we’re used to have the constants on the right hand side (from assigns). So what’s the advantage of this? There are three:

  1. It will save you a lot of NullPointerException when using equals().
  2. It’s more readable in big if/else “switch-alikes”, when you compare a variable against a lot of values.
  3. It avoids the accidental assignment as in if (x = 0) (which should have been if (x == 0) … and if you can’t see the difference between the two, you really should always do it this way!) because if (0 = x) generates a compile time error.

The Dreaming Void

22. January, 2008

The Dreaming Void is not an insult but the latest book of Peter F. Hamilton. It’s been sitting in my shelf for quite some time, now, and since I’m sick with the flu, I had a couple of hours between fever attacks to read.

I’m again impressed how Peter can flesh out characters with a few sentences. As an aspiring writer, it’s always both intimidating and relieving to read a good book. On one hand, it shows how much more one has to travel, on the other hand, it shows it’s an effort well spent.

All in all, a good story, maybe a bit confusing because the author skips back and forth between so many characters, storylines and timelines which makes it hard to track what happened in which order and why something is important. It shows Peters talent as a writer how he can manage all these details without ever stumbling. He’s also probably the only SciFi author who can get away at writing a couple of pages how to renovate a flat including buying a new kitchen and a bathroom for it. 🙂

There is one sore spot, though. In one scene, Aaron breaks into a high-security memory-cell vault and gets pummeled by two heavily armed guards. In the process, a lot of damage is dealt to the environment, especially the racks with the memory cells and their valuable content.

Peter, please. No one in their right mind allows heavy arms near valuable, delicate stuff. Next time, put these guards in the corridor before the vault, so they can hammer away at any intruder with a fat, reliable forcefield between them and the cells with takes the excess damage. That would make it a bit more realistic.

Other than that, a great book. Recommendation: Buy.