Refactoring!  the debt you have created.

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

  1. Code that is less readable, I mean readable by humans, not machines. ??
  2. Code that is not maintainable, when a small change affects more than needed. which cost time.
  3. Legacy code without maintenance.
  4. Lots of abstraction and scope leaks.
  5. Misuse/bad use of the conditional operator, not enough effort to reduce security flaws, and unused 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

https://en.wikipedia.org/wiki/Software_rot

This has symptoms, we call them CODE-SMELL,

Some example:

  • Bloater
  • OO abuser
  • Change Preventer
  • Coupler

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.

AST for JS


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.

https://jshint.com/docs/


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.

https://pmd.github.io/



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,

  • Write tests and then write features.
  • Refactoring star only when all tests in a file/module are greens.
  • The goal of the refactor is to make the code better and keep the tests green.

Red-green-refactor


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.

https://bradfrost.com/blog/post/atomic-web-design/

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

  1. Test, Test, Test This is the only way you can track the progress and integrity. Collaborate with tester in UAT and Regression label.
  2. Always automate the code sanity check with CI pipeline and publish them with code review.
  3. Do not use bug-fix tickets for refactoring, they can backfire in many ways. Treat them in separate manners. Refactoring itself can create bugs.



Let's settle the debt, for now.


要查看或添加评论,请登录

社区洞察

其他会员也浏览了