Refactor code

Last edited time
Feb 3, 2024 5:57 PM
Reading list
Status
1 case
tags
coding

Case 1: Refactor financial transaction handlers

The original transaction handlers were stored as long files (200-300 lines of code) and contained complex logic. We decided to refactor them to be more abstract and separate lower-level operations from the main logic.

Used GitHub Copilot

Lessons learned

Follow by example

Our team primarily refactored the first transaction handler to establish the structure. We manually divided it into logical components and implemented the Strategy pattern, distributing different strategies across various files. For the remaining files, we instructed the chatbot to follow this example and explicitly attached files to the context. This approach worked exceptionally well.

Breakdown the task on subtasks

Ask the assistant to analyze the task and come up with a step-by-step plan. Then, review and modify the plan to be more accurate and save it as part of the prompt.

Prompt (exemplary): Your task is to refactor file X. Use the approach applied in the already refactored files Y, Q, W, and E as a model. Aim to closely replicate the same model and coding style. Before starting the actual refactoring, your first task is to develop a detailed step-by-step plan for this process. Analyze the structure, design patterns, and modularization used in the example files and create a clear, actionable plan for file X. Ensure the plan aligns with the established coding practices, architecture, and maintains the integrity of the overall system design.

Then, ask the assistant to execute step #N. You can iterate over steps in the same chat window, but remember that the context window is narrowing down with every request.

Lost context

At some point, the assistant might forget the original prompt. By the 5th step (out of 10), the context seemed to be overloaded, leading to Copilot deviating from the original plan. We addressed this by starting a new dialogue.

Prompt (exemplary): Here is the detailed plan for refactoring file X. The first four steps have already been implemented. Your task is to continue from step 5 of this plan. Please follow the established strategies and code style used in the previous steps. Ensure that the refactoring from step 5 onwards is consistent with the work already done, integrating seamlessly with the changes and maintaining the overall design and coding principles. Provide the necessary code and modifications starting from step 5 and continue through the subsequent steps of the plan.

Ask to create bash scripts if necessary

In our case, we were splitting one large file into many smaller ones.

You can save additional time by asking the assistant to wrap the new code into bash scripts that create new files and paste the code into them. This is very handy.

Leave TODOs in your code

You can leave tags for the AI in the code indicating how you want to refactor certain parts. Then, you can add a line to your in-chat prompt: "Refactor this file in accordance with TODOs.”

Pitfalls

The assistant does not think abstractly

After a while after refactoring, it turned out that the code could be improved even better. If all the classes were refactored by a human, they would most likely have noticed repeating patterns and immediately improved those places. The machine, although it did well, followed the instructions, but for a human, the task of refactoring is difficult in that the next step might not be visible until you make the previous one.

If you ask the AI Assistant how it would refactor this or that code, you are likely to get some low-level recommendations at the level of if-else and loops, not high-level architectural improvements.

A hypothesis emerged that Diagram of classes helps Assistant understand code base more abstractly

Not enough context for further refactoring

After the first refactoring iteration, the total volume of code increased by approximately 15% and the number of files increased from 1 to 4. In the second iteration, the number of files was supposed to increase from 4 to 8.

Subsequent attempts to refactor further using the method described above were not successful. The assistant clearly lacked context, and forcibly connecting files to the context did not produce any results. The assistant suggested creating files that already exist and fell into low-level recommendations.

It is difficult to judge what influenced the result - the increase in the volume of code, or the number of files.