views:

457

answers:

7

I have recently gotten involved in a project where a web-based game uses an engine written in PHP. The game works fine but could be more robust and have some more features - and the code is both old and fairly ugly.

The issue with this code (and much PHP code in general I suspect) is that it has evolved to where it is and isn't structured very nicely, has a lot of almost-identical cut/pasted pieces of code, and makes heavy use of globals even where functions are involved. Not a single unit test anywhere, of course, and the code is hard coded to use a particular DB and relies on $_REQUEST strings from the web page that calls the code.

The right thing to do would be to pull out pieces of the code as sub-functions, tidy them up and add some unit tests to make sure it continues to work, adding a few mock objects along the way. However, doing this entirely by hand is tedious and error-prone and I wonder how you do this - is there an IDE or tool that can help, for example? I would like to have a tool that can help produce a function from a defined piece of code, suggest which variables should be parameters, by value or reference, automatically add $this-> references, etc.

Is this too much to ask, or is there a good solution?

Thanks!

A: 

I feel your pain. However, I've never heard of such a tool, at least not for PHP.

jperras
A: 

I don't think the language is even structured enough to permit any kind of semi-automatic refactoring. Anyway I don't know of any application that could do this, but on the bright side - doing it all by hand will make you intimately familiar with the source, it might help you later that you know your way around a lot better than you would with automated tools.

Nouveau
+5  A: 

Allan, I have never seen a refactoring tool that powerful. Even Eclipse refactorisation for Java is not that feature complete. However, there are some common hints on refactoring. I usually refactor applications in two steps.

1. Get things separated. I extract db and model related stuff and extract them away from main code. Some object representation would help. Check Active Record or DAO pattern for any ORM work. HTML code can be taken away from main code and moved to template files. Check out nice template engine with strong support for view logic processing - Smarty.

2. Put things into MVC framework. MVC is a robust architecture that suits web applications exceptionally well. Use one of PHP frameworks to tie loose PHP files into controller methods. Frameworks make easy managing URLs, HTTP requests, passing parameters, managing login information and usually provide some access control mechanisms.

But the most important is: do not break things beyond repair. It's refactoring, not redevelopment ;-)

Michał Rudnicki
+6  A: 

This article really highlights the dangers of refactoring/redeveloping old ugly code:

http://www.1729.com/blog/EconomicsOfTestingUglyCode.html

My approach is generally to tackle one small problem at a time, which starts with hiding much of the old code behind an interface where it can be dealt with "later" without affecting any new code. This way anything that I write can be well-designed and have a solid structure, but I don't have to waste too much time trying to refactor and then test weeks/months/years worth of bad code.

David McLaughlin
A: 

No tool is going to take fugly code and somehow make it nice again. The phrase "back-to-formula" is quite a scary prospect for a mature project, but bare with me...

I would suggest that you port it over to a decent MVC framework. In doing so, it might not be a bad time to do a structural rewrite so all your classes are optimised (something that rarely happens naturally with evolving code). It will take a long time and there will be pain when things don't work as expected.

It's a big step but that's what happens when code is left to rot.

Your other options:

  1. Do a partial restructure, where all new code follows a set regime and old code is slowly redeveloped.
  2. Just fix your bugs and do no restructuring.
Oli
A: 

I was googling this very thing and found an interesting blog by a customer of Accurev.

http://blog.accurev.com/2008/09/17/dr-strangecode-or-how-i-learned-to-stop-worrying-and-love-old-code/

Having the right version control tool in your toolbox can't hurt.

A: 

In my experience a complete rewrite is much more economical in this situation.

Rewriting pieces is fine, but a full rewrite on a lot of code (as people say) just uncovers the old edge case bugs again, so that the same special cases that the old code covered bubbles back up again.
Tchalvak