File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Refactor this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Refactor this" Watch "Refactor this" New topic

Refactor this

santosh kulkarni

Joined: Jul 21, 2004
Posts: 18
Hi, I would like the expert opinion over here on making this piece of code less smelly.

It is working code so while loop exits properly on some condition
It is basically processing a screen line by line and populating objects
based on the information on the current line.The methods on the summaryPageDetails return false if the line does not match the regular expression

[EJFH: Added CODE tags to preserve formatting ]
[ November 29, 2005: Message edited by: Ernest Friedman-Hill ]

Impossible is not a fact its an opinion
Rick Goldstein

Joined: Oct 10, 2003
Posts: 21
If all the checks to see if a match is found are performed on the summaryPageDetails object, then I'd move the entire contents of the loop into a method on that class.

Then, make each method into a class of its own with a common method call, like public boolean doesRowMatch(...), with whatever arguments are appropriate. Loop through a collection of instances of these classes, calling doesRowMatch() on each instance until one of them matches. You can even make these processor into inner classes so they have access to common data of the caller.

Does that make sense?

[ November 29, 2005: Message edited by: Rick Goldstein ]
santosh kulkarni

Joined: Jul 21, 2004
Posts: 18
Rick, Thanks for the reply
I was kind of thinking along those lines but your explanation made it very clear. It will need a little more work though. SummaryPageDetails is already an inner class!
if the file becomes huge i'll do some more surgery
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I'm seeing the code-smell of repeating the block, and the 'matchFound' being easily eliminated:

Step 1:

use a shortcut-OR:

if all blocks can be combined this way, we don't need the 'continue' any longer:
I agree. Here's the link:
subject: Refactor this
It's not a secret anymore!