Thursday, April 14, 2011

Refactoring of very combine code

I now have to refactor some code, it's basically one method(its around 1000 lines of code) that does a lot of calculations and have a lot of variables. I'm not sure how to refactor it. Does code like

...
calculateSth(param1,param2,param3,param4,param5, params6);
calculateSthElse(param1,param2,param3);
...

look good?

I could introduce parameter objects, but those objects would only be used as params to some method, so it would look like this

...
calculateSth(calculateSthObject);
calculateSthElse(calculateSthElseObject);
...

or I could put everything in one big object and make it

...
calculateSth(calculateObject);
calculateSthElse(calculateObject);
...

however in that solution I would have to pull out everything that is needed in private methods on the beginning of the method and set at the end, and it would be a lot of harder to find out what values are used in private methods. Around half of variables are needed as output.

How would you do it?

P.S. Calculations are not trivial, so doing things like

calculateObject.setMagicValue4((calculateObject.getMagicValue() * calculateObject.getMagicValue2() / calculateObject.getMagicValue3()) 

would only make it hard to read.

From stackoverflow
  • I would spend whatever time necessary to make sure I understand what the algorithm actually does. Then I'd try to find out how I'd really solve the problem if I could choose, which would probably involve a number of classes & concepts. Then I'd try to introduce these concepts one by one into the existing code, making sure to get proper test coverage for each concept as I introduce it.

0 comments:

Post a Comment