This article first appeared on my personal website https://luigimorel.co/blog
Refactoring is a chore that any software engineer encounters as they progress through the career ladder. It is an important process that's geared towards improving code quality, scalability and maintainability of a code base.
A couple of days ago, I got an Upwork to refactor a Next.js application with the end goal of making the improving the re-usability of the components, improving global state management using Redux and cleaning up dead code.
Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. - Martin Fowler
A codebase is like a garden. Without care and attention, it can become overgrown and tangled. Refactoring is like pruning the garden - it helps the codebase grow healthy and strong. - Unknown
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. - Martin Golding
As a lone developer with no engineering manager or team lead to ask why you made the decision to refactor the code the way you did, the above quotes always guides my refactoring process. The idea of a psycopath hunting me down because of an entangled code base is a pretty scary one :)
The Dos
1. Do not change behaviour
While refacoring code, do not change the behaviour of the bits and pieces you're striving to improve.
Changing behavior is not only risky (allowing bugs to slip through since refatoring and behaviour changes overlap), but also make bug attribution harder. Bug attribution is a mental process you'd go through while trying to understand whatt caused the bug ("attribute").
Behavior changes also lead to making code reviews harder and longer since the reviewer has to spend more time looking at not only the refactored code but also at the new behavior.
2. Avoid introducing unnecessary complexity and abstractions
Software complexity can not be avoided but deciding whether the complexity/abstractions you are about to introduce into the codebase are essential or accidental is a call you have to make at some point.
Sometimes, repeating a piece of logic takes precedence over DRY as a way of avoiding accidental complexity.
3. Write tests for the refactored code
Refactoring sessions can introduce new bugs and errors. It's therefore important to have a comprehensive test suite in place that can catch any regressions. Make sure you test the codebase thoroughly before and after making any changes.
4. Don't ignore performance
Refactoring can sometimes have a negative impact on performance. For example, changing to a piece of code from O(n) to O(n2) run time on large inputs.
Make sure you keep performance in mind when making changes, and measure the performance impact of your changes.
5. Keep eyes on the end goal
When refactoring, you should have a clear end goal for example, improving maintainability or scalability. Furthermore, have metrics to measure whether the code chosen end goal has been achieved. Make effort to ensure that you stay focused on it and avoid getting sidetracked by other issues in the codebase you are refactoring
Conclusions
Refactoring is an essential process in software development that helps to improve code quality, maintainability, and scalability. By identifying and eliminating technical debt, refactoring can make it easier to add new features, fix bugs, and optimize performance. It is also an effective way to reduce the risk of software failure and improve the overall user experience.
While refactoring can be time-consuming and require significant effort, it is an investment in the long-term health of your codebase. By regularly reviewing and refactoring your code, you can ensure that your software remains agile, adaptable, and efficient over time. Whether you are working on a small personal project or a large enterprise application, refactoring should be an essential part of your development process.
Top comments (0)