Things I hate about Java – The C programmer’s identity crisis

Here is an anti-pattern that I see all the time and I remove it from the codebase whenever I find it:

public void outerFunction() {
  // do some stuff
  if (!writtenByAFormerCProgrammer()) {
    throw new SomethingFailedException("The function writtenByAFormerCProgrammer returned false");
  }
}

public boolean writtenByAFormerCProgrammer() {
  try {
    // make some library calls that might throw execptions
    return true;
  } catch (Exception e) {
    return false;
  }
}

Now, there was just a problem in production, and something went wrong.  I deal with files from partner systems all the time, so let’s say I am trying to find out why some file didn’t get processed.  I go to the logs and what do I find: “The function writtenByAFormerCProgrammer returned false.” AAAAAAAAARRRRRRGGGGGGGG!  The real problem is probably something easy like trying to create a file in a directory that doesn’t exist or maybe a permission denied error because of a filesystem error, but where is that message? It’s lost because some moron converted it into the value false.

Next step: Find said moron and confront them about their horribleness.

“But I logged the message, why are you whining to me?”

So in real life the situation is never as nice as this little sample.  In the real world that carefully logged message is on the other side of an RPC call, or it was logged at FINE and you are only logging WARNINGs, or it was logged by a different thread and so it shows up in the log but out of context.  It’s so easy to do the right thing, there is no excuse for this kind of identity crisis.