Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legacy Code Nightmare [closed]

Tags:

I've inherited a project where the class diagrams closely resemble a spider web on a plate of spaghetti. I've written about 300 unit tests in the past two months to give myself a safety net covering the main executable.

I have my library of agile development books within reach at any given moment:

  • Working Effectively with Legacy Code
  • Refactoring
  • Code Complete
  • Agile Principles Patterns and Practices in C#
  • etc.

The problem is everything I touch seems to break something else. The UI classes have business logic and database code mixed in. There are mutual dependencies between a number of classes. There's a couple of god classes that break every time I change any of the other classes. There's also a mutant singleton/utility class with about half instance methods and half static methods (though ironically the static methods rely on the instance and the instance methods don't).

My predecessors even thought it would be clever to use all the datasets backwards. Every database update is sent directly to the db server as parameters in a stored procedure, then the datasets are manually refreshed so the UI will display the most recent changes.

I'm sometimes tempted to think they used some form of weak obfuscation for either job security or as a last farewell before handing the code over.

Is there any good resources for detangling this mess? The books I have are helpful but only seem to cover half the scenarios I'm running into.

like image 708
Kenneth Cochran Avatar asked Mar 17 '09 16:03

Kenneth Cochran


People also ask

What is considered legacy code?

This is simply code that was written in the past. In most cases, legacy code do not follow new/current practices and is often considered archaic.

When should I stop refactoring?

One should not start refactoring unless he has a clear purpose in mind. Once the purpose has been accomplished, one is done. There is probably not an explicit 10 point check list to tell you when you are done, but most people can determine if they are being productive or just playing.

Why does legacy code exist?

Sometimes legacy code exists for a reason. It may point to a team with a crazy high turnover, making little effort in their documentation. Or a team of more experienced developers who are comfortable with older languages. A company may keep legacy code due to competitive advantages over other businesses.

What is legacy code in PHP?

For our purposes, a legacy application in PHP is one that matches two or more of the following descriptions: It uses page scripts placed directly in the document root of the web server. It has special index files in some directories to prevent access to those directories.


1 Answers

It sounds like you're tackling it in the right way.

  • Test
  • Refactor
  • Test again

Unfortunately, this can be a slow and tedious process. There's really no substitute for digging in and understanding what the code is trying to accomplish.

One book that I can recommend (if you don't already have it filed under "etc.") is Refactoring to Patterns. It's geared towards people who are in your exact situation.

like image 199
Bill the Lizard Avatar answered Sep 29 '22 21:09

Bill the Lizard