Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refactor "stringly-typed" code?

I'm currently working on a codebase where there are a few classes of variable, like database paths, which are simply represented as Strings. Most of the operations on these (non-)types are defined in a utility class.

I have created a new class to represent a database, with operations defined as instance methods, in traditional OOP style. However it is quite laborious to go through the large codebase and refactor it to use the new types. Does anyone have any advice as to how to do this quickly and effectively?

like image 248
MikeFHay Avatar asked Feb 25 '13 10:02

MikeFHay


2 Answers

Migrate the utility class to use your new class. Then the utility class methods should only contain two statements. One for creating your class and the other is invoking your class. After that, you can inline the utility class methods thereby eliminating the need for it.

When you are finished with that, you need to look for a way to not instantiate your new class over and over again. This should be done by refactoring the local variable to an instance field which is initialized at construction time.

like image 164
SpaceTrucker Avatar answered Sep 28 '22 16:09

SpaceTrucker


Database paths sound like they should be Strings to me. What else makes sense? And they should be externalized, either in configuration files or a database. That's the least of your problems.

Persistence has been so many times over (e.g. Hibernate, Spring JDBC, iBatis, etc.) that I'd wonder how you could possibly improve on them. If you have to go to the trouble of refactoring - and you must - I'd advise using anything other than what you've done.

If you must write something, Google for "generic DAO". You'll get stuff like this:

http://www.ibm.com/developerworks/java/library/j-genericdao/index.html

If your work isn't patterned after something like that, throw it away and re-think things.

like image 35
duffymo Avatar answered Sep 28 '22 16:09

duffymo