There is a module in our telecoms equipment which is written in C. I think the code in this module has a bad smell because it has a number of symptoms:
When new features are added to this module, some original features go wrong. And some weird problems were finally orientated to the buffer overflow of this module.
It is difficult to add new code to the module. Although the unit tests and integration tests covers new code, errors and flaw concealed in the original code come to the surface.
By means of code review, we found the quality of the code is extremely bad. But the module was tested for many rounds, the errors concealed in the surface of the code were found by the QA engineers. Other problems hidden deeper were difficult to find. as well as a virus in the body.(easy to bring misunderstanding, delete it)
We summarize the status quo lay in these reasons:
The original code has corrupted because of the loose process monitoring, in fact, many self testings are ignored or partial executed.
The first programmer of this module was lack of the programming ability and the team leader didn't find the problem and risk in time.
Many improvement approaches stay in the plan phase due to the team leader's suspicion.
The project came to a new version. Some team leaders want to change this situation and we discussed the procedure many times. Everyone has agreed to revise the module but we have to decide between:
- rewriting the module. It means throwing all the code of the module to the trash can and rewriting the code without changing its interface.
- refactoring the module. This means one step at a time. we need retain all the code at the beginning and build a solid set of tests for the code. Then we dig into the code and revise a small piece of it, and relying on the tests to tell us whether we introduce a bug. This process is repeated until the refactoring complete.
I agree with rewriting because we already have some rewriting experiences in our systems which were proven to be the best choices. The project manager wishes to refactor the module on account of the limited time available required to complete the detailed design, coding, code review, unit tests and integration testing which is three months. He thinks the time scales are too tight to do a full rewrite.
Should this code be rewritten or refactored?
As Joel said in Things You Should Never Do, Part I
The idea that new code is better than old is patently absurd.
I agree with this viewpoint. But I have some puzzles that some rewriting cases in our project proved to be wise choice. Maybe it relies on the new programmer who is more outstanding than the previous programmer or the testing is more sufficient.