Tuesday, January 17, 2012

Daily dig: interfaces and JDBC

It is funny how fast we sometimes jump to conclusions or try to use the solutions we know without even thinking if they are right for a given problem. Just yesterday I had an opportunity to witness such a thing (and, moreover, fall victim to it myself) when this seemingly innocent question has been asked: "In C++ we split the class code between its declaration (.h) and definition (.cpp). Can/should we do something like this in Java?".

Immediately, multiple answers sprouted, each of them more or less saying "Sure you can, you use an interface for declaration and a class implementing said interface for definition". The thing is, none of us really answered the original question; what it asked was purely if a class definition should be split for compiler's sake (obviously it doesn't, since Java compiler has no need for forward declarations). Instead, our first thought when asking about definition/declaration split was "Interfaces!".

Now, I know this is a pretty basic and trivial question - and I don't think anyone is really going to use interfaces for each and every class as a substitute for C++ headers (OK, lets not kid ourselves, someone somewhere will...). But if even with such basic things we so quickly jump to (incorrect) answers, what happens when we get to some really complex decision and have a choice between using a tool that we know (but which is not really a good pick for a given problem) and looking for a new tool ideal for the problem at hand? I guess we too often resort to the former instead of the latter. Blah, OK, I ramble now. At least one good thing that came from it is me reading a (pretty old) interview with Erich Gamma from GoF, which I haven't seen before. Good read about programming to an interface, take a look if you haven't yet.

In other news, I have recently stumbled upon this cute little library that somewhat mimicks Spring's JDBC template utility. Very nice! Sometimes you don't really need anything more powerful than JDBC, but we all know how painful dabbling in pure JDBC is. This utility is a nice compromise between going full ORM and not.

No comments:

Post a Comment