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