Click here to Skip to main content
15,884,298 members
Articles / All Topics

What Do You Do When Your Code Has Cancer?

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
14 Jan 2015CPOL3 min read 12K   4   8
What to do when your code has cancer

One truth about business software applications is that they evolve together with business requirements. From different reasons, sometimes from lack of experience of developers and sometimes from not having enough time to devise good solutions, the code often tends to create some sort of cancer inside, which progressively grows to unmanageable pieces of software until it breaks the whole system.

What do we do if we find such phenomenon in your code?

In my experience, I have seen that dealing with such code is almost inevitable in brown field projects. Ignoring them makes things only worse. You have to face it and treat it as it is a disease which needs to be cured. Whenever I face such a situation, I follow these steps:

1. Isolate

Changing the code without breaking the whole system, or refactoring as is called in software development terminology, is not an easy task. Every change must be validated and tested well to ensure that changes do not break or alter the current functionality. In order to stop the bad code growing, we need to isolate it first. Isolation could be done by aiming abstraction through loose coupling the part which needs to be isolated. The abstraction is done by creating interfaces for every class that needs to be isolated and make dependent classes call to interfaces instead of real classes. This may require introduction of inversion of control (IoC) / Dependency Injection (DI) container. Each isolated part must be supported by quality unit tests which ensure that the abstraction maintains the code logic which was in place. Unit tests will also help us in the second phase.

2. Repair

This phase is quite simple. When you arrive here, you have isolated the bad code and protect the functionality with unit tests. Now you can safely start to refactor and change the bad code. As you change, you should continuously run the unit tests to check if changes have any unexpected effects. If the unit test coverage is OK and they are written well, then you can safely proceed as unit tests will spot if you have broken something unintentionally. I would highly recommend Test Driven Development in this phase.

3. Integrate

Integrating the isolated code is the last phase, and most probably the easiest. Now the bad code should be gone and the functionality is unchanged. Often you shall find this phase unnecessary as the integration has been done during the phase 2. However, sometimes you will find some kind of double abstractions resulting from a lot of refactorings and you will see the opportunity of cutting these unnecessary code.

Conclusions

Perhaps this strategy looks very simple, but in my experience, it has been a very effective one. We are not always lucky to land in green field projects and define good code from the beginning. Besides, it is not always easy to maintain the code in good shape when the team has many developers and there is not a good development process in place. Sometimes, you deliberately allow the code to go in a bad direction as a compromise to speed or resources.

The post What do you do when your code has cancer? appeared first on arian-celina.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Albania Albania
I am a Software Architect, a web developer, and a Computer Science lecturer. I enjoy solving business problems by providing software solutions to them. My favorite technologies and fields of interest include ASP.NET, C# programming language, Java programming language, Javascript, jQuery, AngularJS, Web Services, REST, and mobile application development

Comments and Discussions

 
QuestionPerfect recipe but unit test is the real enabler here Pin
Nji, Klaus15-Jan-15 11:33
Nji, Klaus15-Jan-15 11:33 
AnswerRe: Perfect recipe but unit test is the real enabler here Pin
Arian Celina15-Jan-15 11:38
Arian Celina15-Jan-15 11:38 
QuestionPrevent the cancer in the first place Pin
pulford15-Jan-15 6:37
pulford15-Jan-15 6:37 
AnswerRe: Prevent the cancer in the first place Pin
Arian Celina15-Jan-15 11:13
Arian Celina15-Jan-15 11:13 
AnswerRe: Prevent the cancer in the first place Pin
Nji, Klaus15-Jan-15 11:30
Nji, Klaus15-Jan-15 11:30 
GeneralRe: Prevent the cancer in the first place Pin
Arian Celina15-Jan-15 11:36
Arian Celina15-Jan-15 11:36 
I agree with you Nji

Conscious effort to refactor the code and maintain the code quality is essential.

That is why I pointed that you need a team with a flow, and in that case we can achieve the prevention.

p.s. Inheritance of a mess is almost a guarantee for such a scenario
GeneralMy vote of 5 Pin
Klaus Luedenscheidt14-Jan-15 19:16
Klaus Luedenscheidt14-Jan-15 19:16 
GeneralRe: My vote of 5 Pin
Arian Celina15-Jan-15 5:36
Arian Celina15-Jan-15 5:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.