This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
1) I would use a ConnectionPool rather than obtaining the connection via username/password each time.
2) I agree on the boilerplate comment. Many people write their own superclass with the boilerplate or use an ORM framework like Hibernate.
3) startNewThread() is way too long. While the exact length of a method is debatable, most people agree is should fit on a screen or two. Look for snippets you can extract into new methods - like reading from the resultset.
4) I would list out the columns rather an "select *". In fact, I would also specifically list the columns I needed - in this case threadId. Returning columns you don't need as a performance impact.
5) threadRS.close() should be in a finally block.
6) Having to null out variables is an anti-pattern that signifies the code that uses those variables should have been in a separate method.
7) I'd name "selectThread" as "selectTheadStmt". The former sounds like it is of Thread type.
8) "// get Unix time stamp value in seconds " - These comments are also an anti-pattern that the code the comment applies to should be in method named getUnixTimeStampValueInSeconds() and the like.
9) Why do you clear the parameters when the prepared statement has just been created?
On the length thing, it isn't just boilerplate. It is that there is so much going on that it is hard for a reader to figure out what the code does. What if someone other than you has to maintain the code? For that matter, what if you have to in a year?
Compare the code in the first post to the following. Your method names would be clearer of course as they could describe what the queries do. They'd also be more real and pass the proper variables around.
In addition to fitting on a screen, my version has business meaning - you can tell why each code block is needed. It also provides chunks that are possible to reuse.
[edited to fix typo]
Joined: Nov 16, 1999
Hi Wilson and Jeanne,
Thanks for going through the lengthy code. Your comments are valuable. I will be changing the code as per guidelines.
Joined: Dec 27, 2009
Danish you are welcome. Let me know if you need any help with the conversion.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com