Refactoring code with GitHub Copilot
Leverage Copilot artificial intelligence to help you refactor your code quickly and effectively.
Introduction
Refactoring code is the process of restructuring existing code without changing its behavior. The benefits of refactoring include improving code readability, reducing complexity, making the code easier to maintain, and allowing new features to be added more easily.
This article gives you some ideas for using Copilot to refactor code in your IDE.
Note
Example responses are included in this article. GitHub Copilot Chat may give you different responses from the ones shown here.
Understanding code
Before you modify existing code you should make sure you understand its purpose and how it currently works. Copilot can help you with this.
Select the relevant code in your IDE's editor.
Open inline chat:
In VS Code: Press Command+i (Mac) or Ctrl+i (Windows/Linux).
In Visual Studio: Press Alt+/.
In JetBrains IDEs: Press Control+Shift+i (Mac) or Ctrl+Shift+g (Windows/Linux).
In the input box for inline chat, type a forward slash (
/
).In the dropdown list, select /explain and press Enter.
If the explanation that Copilot returns is more than a few lines, click View in Chat to allow you to read the explanation more easily.
Optimizing inefficient code
Copilot can help you to optimize code - for example, to make the code run more quickly.
Example code
In the two sections below, we'll use the following example bash script to demonstrate how to optimize inefficient code:
Use the Copilot Chat panel
Copilot can tell you whether code, like the example bash script, can be optimized.
Select either the
for
loop or the entire contents of the file.Open Copilot Chat by clicking the chat icon in the activity bar or by using the keyboard shortcut:
VS Code and Visual Studio: Control+Command+i (Mac) / Ctrl+Alt+i (Windows/Linux)
JetBrains: Control+Shift+c
In the input box at the bottom of the chat panel, type:
Can this script be improved?
Copilot replies with a suggestion that will make the code more efficient.
To apply the suggested change:
In VS Code and JetBrains: Hover over the suggestion in the chat panel and click the Insert At Cursor icon.
In Visual Studio: Click Preview then, in the comparison view, click Accept.
Use Copilot inline chat
Alternatively, if you already know that existing code, like the example bash script, is inefficient:
Select either the
for
loop or the entire contents of the file.Open inline chat:
In VS Code: Press Command+i (Mac) or Ctrl+i (Windows/Linux).
In Visual Studio: Press Alt+/.
In JetBrains IDEs: Press Control+Shift+i (Mac) or Ctrl+Shift+g (Windows/Linux).
Type
optimize
and press Enter.Copilot suggests revised code. For example:
This is more efficient than the original code, shown earlier in this article, because using
-exec ... +
allowsfind
to pass multiple files towc
at once rather than callingwc
once for each*.txt
file that's found.Assess Copilot's suggestion and, if you agree with the change:
In VS Code and Visual Studio: Click Accept.
In JetBrains: Click the Preview icon (double arrows), then click the Apply All Diffs icon (double angle brackets).
As with all Copilot suggestions, you should always check that the revised code runs without errors and produces the correct result.
Cleaning up repeated code
Avoiding repetition will make your code easier to revise and debug. For example, if the same calculation is performed more than once at different places in a file, you could move the calculation to a function.
In the following very simple JavaScript example, the same calculation (item price multiplied by number of items sold) is performed in two places.
You can ask Copilot to move the repeated calculation into a function.
Select the entire contents of the file.
Open inline chat:
In VS Code: Press Command+i (Mac) or Ctrl+i (Windows/Linux).
In Visual Studio: Press Alt+/.
In JetBrains IDEs: Press Control+Shift+i (Mac) or Ctrl+Shift+g (Windows/Linux).
Type:
move repeated calculations into functions
and press Enter.Copilot suggests revised code. For example:
Assess Copilot's suggestion and, if you agree with the change:
In VS Code and Visual Studio: Click Accept.
In JetBrains: Click the Preview icon (double arrows), then click the Apply All Diffs icon (double angle brackets).
As with all Copilot suggestions, you should always check that the revised code runs without errors and produces the correct result.
Making code more concise
If code is unnecessarily verbose it can be difficult to read and maintain. Copilot can suggest a more concise version of selected code.
In the following example, this Python code outputs the area of a rectangle and a circle, but could be written more concisely:
Select the entire contents of the file.
Open inline chat:
In VS Code: Press Command+i (Mac) or Ctrl+i (Windows/Linux).
In Visual Studio: Press Alt+/.
In JetBrains IDEs: Press Control+Shift+i (Mac) or Ctrl+Shift+g (Windows/Linux).
Type:
make this more concise
and press Enter.Copilot suggests revised code. For example:
Assess Copilot's suggestion and, if you agree with the change:
In VS Code and Visual Studio: Click Accept.
In JetBrains: Click the Preview icon (double arrows), then click the Apply All Diffs icon (double angle brackets).
As with all Copilot suggestions, you should always check that the revised code runs without errors and produces the correct result.
Splitting up complex units of code
Large methods or functions that perform multiple operations are likely to offer fewer opportunities for reuse than smaller, simpler functions that are focused on performing a particular operation. They may also be more difficult to understand and debug.
Copilot can help you to split up complex blocks of code into smaller units that are more suitable for reuse.
The following Python code is a very simple example, but it shows the principle of splitting up a single function into two functions that perform particular operations.
To split up the process_data
function:
Put the cursor in the function name.
Open inline chat:
In VS Code: Press Command+i (Mac) or Ctrl+i (Windows/Linux).
In Visual Studio: Press Alt+/.
In JetBrains IDEs: Press Control+Shift+i (Mac) or Ctrl+Shift+g (Windows/Linux).
Type:
split into 2 separate functions: one for cleansing data, the other for printing
and press Enter.Copilot suggests revised code. For example:
Note
The example suggestion shown above replaces the existing function with three functions. The third function simply calls the other two functions, and allows for existing code that calls the
process_data
function.Assess Copilot's suggestion and, if you agree with the change:
In VS Code and Visual Studio: Click Accept.
In JetBrains: Click the Preview icon (double arrows), then click the Apply All Diffs icon (double angle brackets).
As with all Copilot suggestions, you should always check that the revised code runs without errors and produces the correct result.
Rewrite conditional code for better readability
There are often several ways to write code that does, or does not, get executed depending on various conditions. Some conditional structures are better suited than others to particular use cases, and choosing an alternative conditional structure can sometimes make the code easier to read.
This Java method uses a series of if
and else if
statements to determine which operation to perform:
A switch
statement might be a better way of applying the same logic.
Put the cursor in the method name.
Open inline chat:
In VS Code: Press Command+i (Mac) or Ctrl+i (Windows/Linux).
In Visual Studio: Press Alt+/.
In JetBrains IDEs: Press Control+Shift+i (Mac) or Ctrl+Shift+g (Windows/Linux).
Type:
rewrite the condition to use a switch and use Java 21 syntax with null case, but also add documentation and provide a better function name
.This demonstrates how you can use a single prompt to ask Copilot to make multiple changes.
Press Enter.
Copilot suggests revised code. For example:
Assess Copilot's suggestion and, if you agree with the change:
In VS Code and Visual Studio: Click Accept.
In JetBrains: Click the Preview icon (double arrows), then click the Apply All Diffs icon (double angle brackets).
As with all Copilot suggestions, you should always check that the revised code runs without errors and produces the correct result.
Reformat code to use a different structure
Suppose you have this function in JavaScript:
If your coding standards require you to use the arrow notation for functions, and descriptive names for parameters, you can use Copilot to help you make these changes.
Put the cursor in the function name.
Open inline chat:
In VS Code: Press Command+i (Mac) or Ctrl+i (Windows/Linux).
In Visual Studio: Press Alt+/.
In JetBrains IDEs: Press Control+Shift+i (Mac) or Ctrl+Shift+g (Windows/Linux).
Type:
use arrow notation and better parameter names
and press Enter.Copilot suggests revised code. For example:
Improving the name of a symbol
Note
VS Code and Visual Studio only.
Support for this feature depends on having the appropriate language extension installed in your IDE for the language you are using. Not all language extensions support this feature.
Well chosen names can help to make code easier to maintain. Copilot in VS Code and Visual Studio can suggest alternative names for symbols such as variables or functions.
Put the cursor in the symbol name.
Press F2.
Visual Studio only: Press Ctrl+Space.
Copilot suggests alternative names.
In the dropdown list, select one of the suggested names.
The name is changed throughout the project.