10

Possible Duplicate:
I’ve inherited 200K lines of spaghetti code — what now?

I'm dealing with legacy code. It contains some BIG classes (line count 8000+) and some BIG methods (line count 3000+). For one of these methods I wrote a unit test that covers at least some code. This function has no specific structure, it is a mess of nested loops and conditions. I'd like to refactor but still I have no idea how to start.

I actually started by extracting one single function. In the end it had to have 21 parameters :-/

Should I ...
1. continue extracting awful functions knowing that in the long run I will succeed?
2. start extracting even bigger functions? So I expose the structure of this monster and then I can start the real refactoring.
3. only extract small "good" functions (and / or classes) just because I am a helluva clean coder?
4. do something completely different?

TobiMcNamobi
  • 1,171
  • 1
  • 10
  • 16
  • move it all to trash and start fresh – inf Dec 19 '12 at 20:56
  • 5
    Answer: Start small. Real small. Add comments. Rename variables. Move variable declarations as close as possible to their usage. Reduce the scope of variables and functions, if you can. Extract simple small blocks of code into functions. Start grouping related data together to form classes. Putting related data and functions together in classes will significantly reduce the number of parameters you need to pass. And... Never give up. Always make steps to improve things, even when the steps are small! – Jeff Grigg Dec 20 '12 at 06:29
  • Thanks Jeff. Your comment pointed me in the right direction, I think. And it helped keeping up the motivation ;-) –  Dec 20 '12 at 07:51
  • 1
    Get a copy of Working Effectively with Legacy Code (http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052), it will give you all the answers for your question above. – Doc Brown Dec 20 '12 at 18:12
  • @DocBrown I must admit: When I first read your comment to read "Working Effectively ..." I thought "How lame? I come here to ask a question and instead of getting an answer one says 'Read a book!' ". Now, I nearly finished reading it and it really, *really* is a good book. It perfectly suits my situation and very often it came to me that what Feathers describes is exactly my challenge *and then* he provides a solution for this. Thanks for the hint, Doc. – TobiMcNamobi Oct 31 '14 at 07:31
  • @TobiMcNamobi: thanks for your nice reply. Unfortunately, I had to deal several years with legacy code without knowing Feather's great book - I wish I had found it much earlier. – Doc Brown Oct 31 '14 at 08:56

2 Answers2

7

Refactoring is somewhat the whole books is written about. It's hard to retell all the methods and pitfalls.

  1. Before starting refactoring write the tests for a piece you want to refactor.
  2. Start refactoring with small and clean pieces of those big functions
  3. Find the piece that can be rewritten as a function and make it a function.
  4. Run tests to be sure you've done the things right and no side effects appears

After this you should have and idea what has to be done with all other (big one) pieces. When the whole thing becomes clearer then you can make a higher-level redesign.

P.S. it's just a brief sketch. About refactoring itself is better to read some books (like M. Fowler - Refactoring, etc.)

maverik
  • 194
  • 1
  • 3
3

Code Complete recommends creating an interface or a wrapper between the legacy code and new code that uses it. Keep the interface clean and as you need to make changes to the legacy code, refactor or rewrite the pieces that you need to and move them forward.

If you need to make enough changes, then eventually the classes will be brought up to par with new code.

CLandry
  • 187
  • 1
  • 2
  • this reminds of [Anticorruption Layer](http://c2.com/cgi/wiki?AnticorruptionLayer) "If your application needs to deal with a database or another application whose model is undesirable or inapplicable to the model you want within your own application..." – gnat Dec 20 '12 at 18:14