1. Checked Exceptions and Dependencies

    May 29, 2007 by Craig

    Eric Burke claims that check exceptions introduce implementation dependencies into your code:

    Because the DAO throws PersistenceException, the next higher layer of your application must deal with it. This is a big problem, because this means your business logic now has persistence framework dependencies. What if you want to write a DAO that uses direct JDBC instead? Or you want to switch to Hibernate? Oopsevery layer of your app depends on the old persistence API thanks to checked exceptions.

    Here’s his sample code:

    public class DefaultCustomerDao implements CustomerDao {
      public Customer findById(Integer customerId) throws PersistenceException {
        ...do something with the persistence framework that throws PersistenceException
      }
    }
    

    He claims that the calling code now has to know about PersistenceException, and that’s bad. He’s right on the surface of course, but I think he misses a point: the calling code already has to know about details of the persistence mechanism: it has to know about findById(), which in turn is a part of CustomerDao. (These classes are part of his fictional persistence framework that could be switched out for some other framework).

    If you’re already dependent on CustomerDao and findByID() for your calling code, then adding a dependency on PersistenceException is not much worse. Thanks to the Law of Leaky Abstractions you’re probably more dependent on your persistence framework for more than APIs anyway; your code will almost certainly be structured around the API’s paradigm, so switching to another one will involve a lot of work regardless of whether it uses checked exceptions or not.

    What you can do to minimize all this is to abstract your persistence mechanism behind a new API and paradigm that you control. This API would then call the API that does the actual work. You could swap out implementations of your API as you see fit (although due to the leaks it will still involve some recoding).

    Under such a system, whether a framework uses checked exceptions is entirely irrelevant; it’s completely up to you whether you want to use them or not:

    • If it does, and you do, you can wrap its exceptions in your own, and gain interface independence.
    • If it does, and you do not, you can catch them in your API and then implement your own error-handling system (RuntimeExceptions or error codes).
    • If it does not, and you do, then you can work with its error-handling system (again, RuntimeExceptions or error codes) and throw your own checked exceptions as appropriate
    • If it does not, and you do not, then you can possibly get away with doing absolutly nothing (ie: letting the RuntimeExceptions propagate). You still have the same options as in point #2 as well.

    I believe that your “real” code should be (at least) one abstraction away from any third-party library or framework, whenever possible. This gives you the least dependence on it should you want/need to change it down the road. Depending on the maturity of the library and the prevalence throughout your project, wrapping it in an abstraction layer is usually well worth the additional work: you gain flexibility (which is crucial in any project) and can “fix the problems” (as you define them) in the API as you go along.

    (Perhaps surprisingly, this is my first Java-related post. What’s surprising is that I do Java for a living; I guess that I talk enough about it during the day that I don’t feel much need to blog about it on my own time 😛


  2. Saving Gas?

    May 26, 2007 by Craig

    This one line sums up the entire article and similarly makes a societal statement:

    But with gas close to $4 a gallon near his home in Paso Robles, Calif., Mr. Collinsworth has been driving the Yaris instead of his BMW X5 sport utility and GMC Sierra pickup.

    This sort of thing is ridiculous both economically and environmentally. You’ll spend far more on depreciation and insurance on an extra car than you will save on burning less fuel. The amount of energy that went into making, transporting, selling, maintaining, and disposing of the car won’t be offset by your better mileage unless you’re driving a heck of a lot of miles (which, as a Yaris-driving commuter, you’re almost certainly not).

    Laura and I own a Toyota Highlander non-hybrid SUV which gets a mediocre 19/25 mpg. This is our only car. I work from home, and Laura works part-time relatively close to home. I’d be willing to go toe-to-toe on transportation and environmental costs with any two- or three-car family, even if they own hybrids. On top of our savings, we get a nice, comfortable vehicle with great visibility that can handle less-than-perfect roads (handy in the mountains) and haul a bunch of cargo when necessary.

    Smaller cars are often status symbols and political statements rather than conscious choices for better living. Be sure to recognize them as such.


  3. Snow

    May 21, 2007 by Craig

    October 2, 2006:
    Snow on October 2, 2006

    May 21, 2007:
    Snow on May 21, 2007

    The only thing that changed is the construction 😛

    Of course, it was 25C / 77F just a few days ago.


  4. Recap

    May 6, 2007 by Craig

    Spider-Sense does not work on:

    • Spitballs
    • Vengeful sons
    • Symbiotic doppelgangers

    Who knew?


  5. Division

    May 3, 2007 by Craig

    Laura is taking a math test for a job at a hardware store.

    L: “This (points to long division symbol) means divide this (dividend) into this (divisor), right?” (They do division with a different method in Costa Rica).
    C: “Right”
    L: “So it’s like I’m dividing 336 onions for 12 people?”
    C: Yes. (Laughs) In Canada we use apples and oranges, in Costa Rica you use onions and garlic.


  6. 09 F9 11 02 DE AD BE EF

    May 1, 2007 by Craig

    There are times when you know you’re witnessing something monumental. Think man-landing-on-the-moon. You’ll always remember where you were and what you were doing when you heard the news. I can think of two Space Shuttles being destroyed in my lifetime.

    Today we have a new candidate: Slashdot gets their smirking revenge on Digg.