This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Not what I meant, but on the right track--but instead of just putting comments between sections, take the "sections" out and put them in to new methods.
The Wikipedia "refactoring" article gives a broad overview. In this case, with your code, I would start by pulling out small "chunks" of functionality into separate methods. This has two benefits:
- The code that *doesn't* deal with paging becomes easier to read
As you do that, you'll also find a good way (or several good ways, and occasionally some bad ways :) to make the paging code work for any type of object, on any page, and so on.
By the way, having the connection and result set be instance variables is a really bad idea--servlets must be thread safe.
For what it's worth, after some refactoring, I'm up to 11 methods, and am at the point where the abstraction/extension points are pretty clear, assuming we're generating our HTML in a servlet (not a great idea).
The paging logic (somewhat convoluted, has at least one strange redundancy) is around 150 lines (too long!). If getting the total record count, and paging link URL generation, the code is marginally general-purpose as-is.
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
David Newton wrote:For what it's worth, after some refactoring, I'm up to 11 methods, and am at the point where the abstraction/extension points are pretty clear, assuming we're generating our HTML in a servlet (not a great idea).
Thanks for your favorable reply. 11 methods!!! great I am not thinking even a single method due to my incompetency. Can you please explain why generating HTML with servlet is not a good idea?
2) can you please copy paste some of your developed methods from 11 methods
David Newton wrote:
By the way, having the connection and result set be instance variables is a really bad idea--servlets must be thread safe.
Can you please explain it little bit more as to why I not instantiate them?
True -- But before you actually make it re-usable the first thing do to would be Separate the View from the Model. Look up for MVC and JSP to get some idea. Once you have some of the view separated out from the code -- you'll see that it is pretty manageable...
Also look for paging in JSP -- you should find some code snippets that would help you ...
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Sam Mercs wrote:True -- But before you actually make it re-usable the first thing do to would be Separate the View from the Model. Look up for MVC and JSP to get some idea. Once you have some of the view separated out from the code -- you'll see that it is pretty manageable...
Also look for paging in JSP -- you should find some code snippets that would help you ...
I am learning. Really I learned a lot at this forum
Please keep this thread open and I'll back to you
Best Regards
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Sam Mercs wrote:True -- But before you actually make it re-usable the first thing do to would be Separate the View from the Model. Look up for MVC and JSP to get some idea. Once you have some of the view separated out from the code -- you'll see that it is pretty manageable...
Also look for paging in JSP -- you should find some code snippets that would help you ...
I tried a lot but unable to fix the problem
Can you please split my code according to MVC. that'll be very helpful to start with MVC
How about showing us what you've done so far? Break out the functionality piece-by-piece: we're not going to do it for you, but we're happy to provide hints, etc. For example, here's a list of the methods I created during my first draft refactoring: