Java News from Thursday, May 21, 2009

Google has released Guice 2.0, an open source annotation-based framework for obfuscating codedependency injection. New features in this release include:

I've been using Guice at my day job for the last year, and not by choice. I find it makes our code base far harder to comprehend and work with. The new provider methods In Guice 2.0 do help, and I can see where the module overrides could be useful. However, I still feel that our project would be better off if we ditched Guice and dependency injection completely. As far as I've been able to see, all dependency injection does for us is make unit testing edge cases a little easier (Not make them possible, mind you. The tests are already possible without dependency injection. Just easier.); and that's just not worth the price of admission.

One should not complexify a class's API purely to make testing easier. Testing exists to serve the model code, not the other way around. To cite the classic dependency injection example, if the only reason you pass a Clock object to a constructor instead of creating one privately inside the class is to support unit testing, then don't do it. Unit testing is no excuse for heavily coupled classes exposing their private parts and violating encapsulation. When forced to choose between good object oriented design and testability, choose good object oriented design.