Legacy Code Fix versus Code Rewrite
Python Frameworks use the DRY Method — Don’t Repeat Yourself. That is a powerful mantra to follow when developing applications.
I am faced with a quandry. I have an application that consists of roughly 40000 lines. That code was written in php many years ago, with a handbuilt web framework implemented with smarty. There are a number of issues with the existing code, including inline functions duplicated through multiple files, poorly validated input and bad structure. Outsourcing some code development appeared to be cost effective. In the end, the code quality churned out by that vendor was sub-par. Maintenance of that codebase easily costs twice as much as it should.
This week, a few requirements cropped up which brought up an interesting question. Knowing that the code consists of 40000 lines of poorly written, difficult to maintain code, I debated whether fixing the existing code would be quicker than rewriting the relevent portion and coding the addendum. TurboGears, a python webapp framework would shrink the code considerably since it is thin middleware on top of a wsgi compliant server.
Where it took 45 lines of code to do a halfway decent job of validating a few input fields in php with smarty, the equivalent code in TurboGears consists of a model definition containing the validator segment and a few lines of code to define the page. Updating the database becomes one line of code, replacing 8-12 lines of code.
I had planned to convert the application over to TurboGears eventually, but, the scope of this current project gives me an opportunity to convert a piece of the application over while adding the new features, and leaving the legacy code running as is.
The features I need to add will take roughly 150 lines of Python/TurboGears code, or perhaps 1500-2000 lines of php to achieve the same functionality. I have debated using another PHP framework as a stopgap, but, I have yet to find a decent form library for it that works well.
If I had to pick a favorite, ad_form from openacs would top the list. TurboGears and Genshi with formencode come in as a close second.
I believe rewriting the portions of the app I need to write will probably take roughly the same amount of time as it would take to patch the existing code. The investment in time will put me closer to finishing the complete rewrite of the existing system.
An added advantage is that I can fix architectural issues with the existing package that couldn’t easily be reworked without considerable effort. If the code you are maintaining is over five years old, you owe it to yourself to check out some of the other frameworks out there. Prior to settling on TurboGears, I looked at Django, Catalyst, Mason and a number of other frameworks. I even searched the PHP frameworks but didn’t find anything with the strengths I saw with TurboGears.