What triggers code refactoring?
It was a huge monolithic code laying around for quite some time. It took five sprints to get its current shape and no one bothered until one needs to modify a logic there. This "broken window" (Thanks to my colleague who told me the story!) presents in almost every project. Based on the sensitivity of the team it might take a different time to get it fixed, refactored. I have observed a team with seasoned developers who just ignored such cracky corners but a junior team member asking for help to get those cleaned up before he cloud pound on it. I wonder, then what actually triggers the urge of code refactoring? Is this purely technical or psychological or a mix? This post is all about my feelings about what makes a developer start feeling about code refactoring.
The sense of beauty
Have you ever noticed how we react to a beautiful piece of sculpture or paintings or any form of art? Do we think we all have the technical capabilities to appreciate that artwork? No. We have a sense of beauty embedded in us, mostly nurtured by our culture initially and later if at all by acquiring skills. Developers all have the skills to write code but do we have the culture of appreciating beautiful lines of code? To appreciate a 'clean code' you need to develop sense of fulfillment in writing the same and then articulate the same to your peer and build that culture in the team. This enables team to findout the scope of improvements which may triggers a code refactor.
Crystal clear vision
Before touching the code there should be a clear-cut reasonable expectation about the final shape after refactoring. These expectations may come from experience or knowledge but the proposal must sounds feasible, realistic and bring real values to the project and finally to the team. You need to convince yourself first about the journey you are going start. Having a clear vision about the goal of refactoring helps you to make your mind.
Recommended by LinkedIn
Sense of amusement
Refactoring is not just a routine task for me. Reducing the lines of code, making it more readable and modular give me pleasure. I always find opportunity to get myself involved in refactoring task and eagerly wait for the end result to compare side by side. Unless you are enjoying, refactoring won't bring much value to you neither to the project. It would just be another task randomly assigned to you. Read books, listen and follow famous developers and find out your personal 'Wow' to refactor.
The summary of all is in the tone of Martin Fowler-
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Refactoring is very close to my heart. I still cherish the refactoring sessions I had with Avik. When I want to write some code, which can be done more elegantly if the code was written in a different way, triggers my refactoring. And also the trait of cleaning things up, helps you to enjoy the refactoring. Refactoring is like a poetry that unfolds on every small steps, with passing unit tests.