Skip navigation

Random snippets

It’s Sunday afternoon, I’m finally having that espresso I’ve been too tired to make all afternoon, so this is going to be a shorter post of some simple little tidbits (timbits, for any canadians out there).

Unchecking Exceptions

I don’t want to get into an argument about checked vs unchecked exceptions, but from time to time I’ve written the following to “convert” any checked exceptions to unchecked:

try {
  someMethod(); // throws some checked exceptions
} catch (RuntimeException e) {
  throw e;
} catch (Exception e) {
  throw new RuntimeException(e);
}

Occasionally people come up and “simplify” this to:

try {
  someMethod(); // throws some checked exceptions
} catch (Exception e) {
  throw new RuntimeException(e);
}

Can you please stop doing that – the code fragments are not equivalent. The former avoids wrapping exceptions that are already unchecked. Spread the word, thanks.

EasyMock error

EasyMock is a useful unit testing utility for Java that lets you create mock implementations of interfaces and assert that the code using it should make certain method calls.

People, including myself, occasionally run into the error message
“missing behaviour definition for the preceeding method call” and it’s not always obvious why, so here’s an answer for the search engines.

It probably means you wrote something like this: “expect(foo.something()).andReturn(someMethodThatItselfCreatesAMockObject());”.

Another reason Hibernate annoys me

If you write this code but you’ve forgotten to include “<class>Thing</class>” in META-INF/persistence.xml, what will happen?

List foo = session.createCriteria(Thing.class).list();

You’ll get an error right? Wrong – Hibernate will return an empty list.
HHH-1665 includes a patch, created almost two years ago, to correct this, but it’s been marked as Minor, Awaiting Test Case, and hasn’t been applied.

Sure, I could do the work myself and fix this, but I run into these kinds of issues every time I use it. Any suggestions for alternate JPA implementations?

Name for a book

Someone should write a book called “Boxes & Lines, a pragmatic guide to documenting software architectures”.

Hack

When talking to non-developers about a kludge that you want to apply, call it a “tactical solution”.

Semantic identifiers

When unique identifiers escape out of the database realm (e.g. into XML messages), it’s nice to tag them so they’re more than a number. For example “PROJ_INPUT_1234” instead of just “1234”. This makes it easy to spot bugs when you’re accidently using the wrong ids and also lets you encode information in the id so you can perform some processing without having to lookup a database record (which can be an expensive operation if you’ve got a million identifiers coming in at once).

A question

To finish, a question for your ponderance. What if computers were infinitely fast? No matter how slow your program, it would either run forever or terminate immediately.

What would programming languages look like? What would be the interesting problems in computer science?

2 Comments

  1. Gavin Keen
    Posted April 15, 2009 at 2:20 am | Permalink

    Hi Tim,
    Another possible cause for the EasyMock error you quote, is using a mock object before replay() has been called, which is very easy to do when on a roll and churning out test code like your favourite show starts in five minutes.

    Great blog – keep up the good work!

  2. timsuth
    Posted April 16, 2009 at 9:31 pm | Permalink

    Hi Gavin,
    Good point. Thanks!


Post a Comment

Required fields are marked *
*
*

%d bloggers like this: