Monday, September 7, 2009

When learning how to use an API, I generally start by copying examples. That's probably how most other people start. And the examples are usually enough to get stuff to work. And, I suspect that that's where most people stop, because I see plenty of code that makes clumsy use of APIs. I suspect that these people limit themselves to the examples that they have.

Here's one example, based on actual code using JDBC:

long id = resultSet.getLong(resultSet.findColumn("ID"));

It would be better written as

long id = resultSet.getLong("ID");

but I'm guessing it was written the way it was based on copying something else.

I generally start by copying examples, but once I get a feel for what I'm doing, I'll look at the documentation or the source code. Often, I'll find that there are better ways of doing things if what I'm doing is not exactly what is done in the examples. That's one of the reasons the second and third and so on times I write something, I'll write it better than the first and second and so on time I wrote it.

No comments:

Post a Comment