I am currently facing a situation where I am not quite sure how to best proceed.
Background
I am planning a new data import process, which will import personnel and salary information from a text file produced by a different system. We already have an import process for the data in question, but the format of the import file will change completely. Thus, while the data that is read will remain the same (employee names, pay grades, past salary payments...), it is expressed very differently in the new file format (different format, different fields, different coding of data, different representation of special cases such as unpaid leave or retroactive changes of salary).
How to reuse?
Now I would like to re-use as much of the existing import code as possible. The problem is that most of the existing classes and methods have parts that could be reused (mostly the parts that deal with writing the data into our own database, which has not changed), but also have many details of the old import format hardcoded, so direct reuse is impossible.
I now see two alternatives:
- try to refactor and rework the existing code to be more independent of the particular import format, so it can handle both old and new format
- cherry-pick the parts of the old code that still apply (extract them to separate methods or similar) and reuse that
My problem
I'm finding it difficult to judge which approach is best. On the one hand, I believe reuse is important, and 1) seems to allow me to reuse more. On the other hand, from looking through the code I have a gut feeling that at least 30%-50% of the code no longer apply, so I fear 1) might be a lot of refactoring work, and in the end I'd only reuse little more than with approach 2).
The problem is (I feel ) that I'll have to decide this upfront, because it's difficult to switch approach once I have started: If I start with 1), and after a few day of refactoring I find that little actual "meat" remains in the generalized functions, I have to throw away all the refactorings and start afresh with separate code that calls the old code as needed.
So how do you go about solving new problems when you have code that is broadly similar, but differs in many details to what you need? How do you judge which approach to code reuse makes more sense?