Refactoring code with GitHub Copilot
Leverage Copilot artificial intelligence to help you refactor your code quickly and effectively.
In this article
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: PressCommand+i (Mac) orCtrl+i (Windows/Linux).
- In Visual Studio: PressAlt+/.
- In JetBrains IDEs: PressControl+Shift+i (Mac) orCtrl+Shift+g (Windows/Linux).
In the input box for inline chat, type a forward slash (
/
).In the dropdown list, select/explain and pressEnter.
If the explanation that Copilot returns is more than a few lines, clickView 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:
#!/bin/bash# Find all .txt files and count lines in eachfor filein $(find . -type f -name"*.txt");dowc -l"$file"done
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 theInsert At Cursor icon.
In Visual Studio: ClickPreview then, in the comparison view, clickAccept.
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: PressCommand+i (Mac) orCtrl+i (Windows/Linux).
- In Visual Studio: PressAlt+/.
- In JetBrains IDEs: PressControl+Shift+i (Mac) orCtrl+Shift+g (Windows/Linux).
Type
optimize
and pressEnter.Copilot suggests revised code. For example:
find . -type f -name"*.txt" -execwc -l {} +
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: ClickAccept.
- 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.
let totalSales =0;let applePrice =3;let applesSold =100;totalSales += applePrice * applesSold;let orangePrice =5;let orangesSold =50;totalSales += orangePrice * orangesSold;console.log(`Total:${totalSales}`);
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: PressCommand+i (Mac) orCtrl+i (Windows/Linux).
- In Visual Studio: PressAlt+/.
- In JetBrains IDEs: PressControl+Shift+i (Mac) orCtrl+Shift+g (Windows/Linux).
Type:
move repeated calculations into functions
and pressEnter.Copilot suggests revised code. For example:
functioncalculateSales(price, quantity) {return price * quantity;}let totalSales =0;let applePrice =3;let applesSold =100;totalSales +=calculateSales(applePrice, applesSold);let orangePrice =5;let orangesSold =50;totalSales +=calculateSales(orangePrice, orangesSold);console.log(`Total:${totalSales}`);
Assess Copilot's suggestion and, if you agree with the change:
- In VS Code and Visual Studio: ClickAccept.
- 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:
defcalculate_area_of_rectangle(length, width): area = length * widthreturn areadefcalculate_area_of_circle(radius):import math area = math.pi * (radius **2)return arealength_of_rectangle =10width_of_rectangle =5area_of_rectangle = calculate_area_of_rectangle(length_of_rectangle, width_of_rectangle)print(f"Area of rectangle:{area_of_rectangle}")radius_of_circle =7area_of_circle = calculate_area_of_circle(radius_of_circle)print(f"Area of circle:{area_of_circle}")
Select the entire contents of the file.
Open inline chat:
- In VS Code: PressCommand+i (Mac) orCtrl+i (Windows/Linux).
- In Visual Studio: PressAlt+/.
- In JetBrains IDEs: PressControl+Shift+i (Mac) orCtrl+Shift+g (Windows/Linux).
Type:
make this more concise
and pressEnter.Copilot suggests revised code. For example:
import mathdefcalculate_area_of_rectangle(length, width):return length * widthdefcalculate_area_of_circle(radius):return math.pi * (radius **2)print(f"Area of rectangle:{calculate_area_of_rectangle(10,5)}")print(f"Area of circle:{calculate_area_of_circle(7)}")
Assess Copilot's suggestion and, if you agree with the change:
- In VS Code and Visual Studio: ClickAccept.
- 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.
import pandasas pdfrom pandas.io.formats.styleimport Stylerdefprocess_data(item, price):# Cleanse data item = item.strip()# Strip whitespace from item price = price.strip()# Strip whitespace from price price =float(price)# Convert price to a float# More cleansing operations here# Create and print a DataFrame data = {'Item': [item],'Price': [price]} df = pd.DataFrame(data)print(df.to_string(index=False))# Example usageitem =" Apple "price =" 1.25"process_data(item, price)
To split up theprocess_data
function:
Put the cursor in the function name.
Open inline chat:
- In VS Code: PressCommand+i (Mac) orCtrl+i (Windows/Linux).
- In Visual Studio: PressAlt+/.
- In JetBrains IDEs: PressControl+Shift+i (Mac) orCtrl+Shift+g (Windows/Linux).
Type:
split into 2 separate functions: one for cleansing data, the other for printing
and pressEnter.Copilot suggests revised code. For example:
defcleanse_data(item, price):# Cleanse data item = item.strip()# Strip whitespace from item price = price.strip()# Strip whitespace from price price =float(price)# Convert price to a floatreturn item, pricedefprint_data(item, price):# Create and print a DataFrame data = {'Item': [item],'Price': [price]} df = pd.DataFrame(data)print(df.to_string(index=False))defprocess_data(item, price): item, price = cleanse_data(item, price) print_data(item, price)
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: ClickAccept.
- 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 ofif
andelse if
statements to determine which operation to perform:
public StringgetSound(String animal) {if (animal ==null) { System.out.println("Oops! A null animal?"); }elseif (animal.equalsIgnoreCase("Dog")) {return"Bark"; }elseif ( animal.equalsIgnoreCase("Cat")) {return"Meow"; }elseif ( animal.equalsIgnoreCase("Bird")) {return"Tweet"; }return"Unknown";}
Aswitch
statement might be a better way of applying the same logic.
Put the cursor in the method name.
Open inline chat:
- In VS Code: PressCommand+i (Mac) orCtrl+i (Windows/Linux).
- In Visual Studio: PressAlt+/.
- In JetBrains IDEs: PressControl+Shift+i (Mac) orCtrl+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.
PressEnter.
Copilot suggests revised code. For example:
/** * Retrieves the sound made by a given animal. * *@param animal The name of the animal. *@return The sound made by the animal, or "Unknown" if the animal is not recognized. */public StringgetAnimalSound(String animal) {returnswitch (animal) {casenull -> { System.out.println("Oops! A null animal?");yield"Unknown"; }case String awhen a.equalsIgnoreCase("Dog") ->"Bark";case String awhen a.equalsIgnoreCase("Cat") ->"Meow";case String awhen a.equalsIgnoreCase("Bird") ->"Tweet";default ->"Unknown"; };}
Assess Copilot's suggestion and, if you agree with the change:
- In VS Code and Visual Studio: ClickAccept.
- 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 #"-1">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.
PressF2.
Visual Studio only: PressCtrl+Space.
Copilot suggests alternative names.
In the dropdown list, select one of the suggested names.
The name is changed throughout the project.