Refactoring! the debt you have created.
We have all been through that, rapid PoC, fast prototype, deliver MVP to the board.
After all the dust had settled down, looked back and realized the technical debt is high now.
And then the fun part is refactoring the code.
So in a technical term,
Refactoring - the discipline of transforming bad code to good code without breaking it. It works best - and cost least - if you do it regularly. Five Lines of Code by Christian Clausen
What is bad code
Now we have a stable idea of what bad code can be. I am not going to describe how it will affect the codebase
Increase Software-Entropy AKA Software-ROT
This has symptoms, we call them CODE-SMELL,
Some example:
Many more, and I will try to outline them later on,
Let's focus on refactoring this in a utopian codebase where all variables, functions, methods, classes, and abstractions are doing their part and living happily together.
It's a long journey, planning is necessary and team engagement needs to be done successfully.
As an example, In my previous team, we decided to increase TypeScript coverage in a legacy JS repo, so we adopted the FIFO strategy.
When we touch a specific file/folder for a specific feature or bug fix, we will create necessary interface or re-use the existing one and convert that specific part to typescript. If the ticket is high priority or PROD bug then create a subsequent ticket to handle TS migration.
Here are some tools that will help
AST
AST is not a tool but a DataStructure, it's used by compilers on run time, and it is very widely used in almost all languages. To do better refactoring, understanding the code analysis is crucial, at least the concept and basics.
SonarQube
A very effective tool, not just for code refactoring but also for overall quality maintenance.
Easy setup with CI pipeline, also integrated with code review.
This has a couple of very advanced tools for refactoring help.
Jshint
Community-driven, open source, integrated with IDE. very well-established and adopted rule sets. Helpful for developers to write clean code and be lightweight. Almost all major IDE are supported.
JArchitect
Only for Java, comprehensive and detailed. Same as SonarQ. They do have all the important features like CI integration, code analysis, and a very powerful CQLinq query system.
领英推荐
Resharper
For C#, specifically an extension for .NET in Visual Studio. Powerful and effective. It supports a wide range of languages, including C#, VB.NET, XAML, ASP.NET, HTML, and XML.
PMD
Multi-language support, easy to install and use. Code analysis tool with rule set management. It uses .pom file for setup. Also integrated with CI pipelines.
Some Techniques
For refactoring, there are several ways to achieve the goal, here are some of my personal tested techniques.
1. Red-Green-Refactor
Sounds like a deployment method blue-green. Kinda the same in concept.
So, this works with TDD,
2 . Composing
This technique focuses on breaking down large classes, functions, and components into manageable chunks. I used a technique called Atomic-Design.
Break down into atoms, molecules, organisms, templates, and pages.
A must read for web developers
3. Preparatory Refactoring?
This works before major feature releases when the codebase will be touchdown in many parts.
As a savvy developer, we will touch all bad code and turn it into gold by restructuring code, removing API keys, simplifying complex logic, remove deprecated libraries and secrets.
Always be sure to scope them into manageable tasks.
Some other methods, I did not test...
4. Refactor by abstraction.
5. Moving feature.
6. Shift and lift.
Dont forget
Let's settle the debt, for now.