Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring legacy C code into MVC design

I am working on some OLD (as in older than me) C code that needs to be cleaned up and brought up to date so that (amongst other things), it is easier to maintain and integrates more cleanly with current code.

The existing code is quite messy, and freely intersperses GUI logic with business logic and data access logic. The only saving grace is that it is NOT spaghetti code, and that it is modular (as most code from the seventies tends to be).

My question is this: Can anyone provide me with a guideline on how to go about refactoring the code into MVC (BTW I am also moving the code from C to C++ whilst undertaking this task - but that is the least of my concerns, as I am quite aufait with both languages).

BTW, I am fully aware that this is not a trivial task. I just want to know what the steps are from going from modular code that mixes DBAL/BL/GUI to a cleaner MVC implementation.

like image 216
oompahloompah Avatar asked Jul 28 '11 06:07

oompahloompah


1 Answers

I'm not convinced that there can be a definitive set of steps, what we do will vary with the structure of the existing code.

I agree with @Jesus Ramos that figuring out a test strategy is key here. The problem for you is likely to be that the code is currently not unit-testable, because there are effectively no "units", we can't test the business logic,say, without testing the UI.

I would give very serious consideration to rewriting the the thing rather than refactoring.

If you are going to refactor, then my guess is that you'll take a kind of "Swiss Cheese" approach. Drill out pieces, leaving a central mass with lots of holes. So pull out database access code, focusing on providing a clear API and set of Data Objects - these become the basis of your Model. Pull out the GUI code into a view layer. What's left is the Controller logic, which you can then refactor.

like image 66
djna Avatar answered Sep 21 '22 15:09

djna