Refactoring a long method that simply populates
Posted
by Jeune
on Stack Overflow
See other posts from Stack Overflow
or by Jeune
Published on 2010-06-02T10:12:29Z
Indexed on
2010/06/02
10:13 UTC
Read the original article
Hit count: 213
java
|refactoring
I am refactoring a method which is over 500 lines (don't ask me why)
The method basically queries a list of maps from the database and for each map in the list does some computation and adds the value of that computation to the map. There are however too many computations and puts being done that the code has reached over 500 lines already!
Here's a sample preview:
public List<Hashmap> getProductData(...) {
List<Hashmap> products = productsDao.getProductData(...);
for (Product product: products) {
product.put("Volume",new BigDecimanl(product.get("Height")*
product.get("Width")*product.get("Length"));
if (some condition here) {
//20 lines worth of product.put(..,..)
} else {
//20 lines worth of product.put(..,..)
}
//3 more if-else statements like the one above
try {
product.put(..,..)
} catch (Exception e) {
product.put("",..)
}
//over 8 more try-catches of the form above
}
Any ideas on how to go about refactoring this?
© Stack Overflow or respective owner