Question

I'm working on a project which has a codebase of about 3500 files probably a few hundred less than that actually. This project is made in PHP and is a quite messy, in this case it means that the documentation is hard to understand OOP and procedural programming is mixed, dependecies are unclear and the ones who made the system where beginner programmers with all that that entails.

To be honest what they have done is impressive, it is a working product and all that. But debugging and adding new features is a real chore.

Now to my question what are some good criteria for wheter we should refactor the whole project or do a complete rewrite. I should mention that rewriting parts of the system as we go is probably a no go because of everything being interdependet.

Was it helpful?

Solution 2

The basic criteria will probably be the time ( = money).
The estimation is always hard when something is big. Split it to the smallest chunks.

Mind the point of view. Your estimation will probably differ from the estimation of the previous programmers, as it will be biased by the style you prefer. Maybe a better choice would be to hire the previous programmers to "fix" the code.

Anyways, you should state the minimum requirements to call the app well written and easy to maintain, discuss this with the boss and the team, split to smallest possible sprints and estimate the time required for all of them.

If you are able to convince your boss that the rewrite will return in better ROI overall, do rewrite. Otherwise, learn the "new style". If you have the comfort to stop the business for the time of the rewrite, or a spare time to do it, you're lucky. You're even more lucky if the code is testable, or you have at least some unit tests. If this is not possible, you should implement functional tests at least to help with both cases (refactoring or rewrite).

Note, that each one of us will probably prefer to rewrite from scratch, than to refactor. Then rewrite this rewritten...

So refactoring is always a good choice, assuming you already have tests or you are able to easily implement the tests.

OTHER TIPS

If the application is big, successful, and does interesting things, an attempt to rewrite it by hand will likely fail. The rewrite will never have enough mass to displace the original, and you can't throw the original away until it does, meaning you will continue to have to enhance the original until the replacement is ready. Twice as much work, no additional value. You are much better off, likely, keeping the application and cleaning it up.

You might consider two ideas to help reduce the size of the code base:

  • Run test coverage on the code. Normally people use this to validate the application functionality, but if you run test coverage and simply run the application the code for an extended period of time, what it will tell you is what code isn't exerecised and therefore likely dead. If the system is mess, it likely has a lot of dead code, and this is an easy way to find candidates. There are test coverage tools that can collect data on your entire application with low overhead; you can use them on production code.

  • Run a clone detector. This will find duplicate code. Outright duplication is easy to remove. A good clone detector will find not just identical clones, but parametric clones, that is, code which has been copy/paste/edited but in a way that can be summarized with parameters. Parametric clones require more finesse to remove, but finding them tells you of likely missed abstractions, and removing them (by replacing with objects or methods) inserts those missing abstractions into your code, making further maintenance involving that idea easier. (It isn't a very well known fact, but clone removal also raises test coverage rates, if you are really using test coverage to verify functionality!)

If the application works already and there are no major malfunctions then I would leave most of it as it is since rewriting everything from scratch is always costly.

Perhaps you should consider reducing the amount files/lines wherever you see duplicated code making objects/functions to centralize everything as much as possible. This helps a lot for an eventual maintenance and doesn't require a large amount of time to do. Especially if it was written, as you say by beginners, you will easily find the "wrong approaches" to improve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top