Giving the right prompt!
In this short article I want to show how GitHub Copilot can help you refactor your code.?
Let’s start with this code:
We have a method which is slightly difficult to read and understand, it shown below:
We would like to break it up into smaller methods.
If we just select the method and say “Refactor this code”, GitHub Copilot does nothing.? So, it is important to give the right prompt, do some hit and try if are you are not getting what you want.? The prompt which got me what I wanted is given below
Prompt: Break this method down to smaller methods
Here is the resulting code recommended by GitHub Copilot, you can see that now we have 3 easy to read methods instead of 1 large method.
You can find the new code file here:
A lot of times we can reduce technical debt by doing such small refactoring.? Having unit tests or at-least some integration tests help build confidence that this code will run as expected in production.
Do share how you are using GitHub Copilot to improve your productivity?