This time we'll follow up on the first part of "How to get started with GitHub Copilot? #1", where we covered its main features, setup, and overall first steps to get started. If you missed that article, we recommend starting there.
Now we'll move on a bit and show you what else Copilot can help you with, specifically going over refactoring, optimization and test generation.
It is in these areas that Copilot can deliver very concrete results. Teams that use it to its fullest extent report significant reductions in code editing time, faster test generation, and overall higher quality output. Developers spend less time on routine tasks and can focus more on key parts of the application.
What exactly does Copilot bring to the table:
- Refactoring faster by up to by identifying and modifying complex functions.
- Saving up to ~50% of time in generating unit, integration and E2E tests.
- Improved readability, stability and reduced technical debt.
- Easier onboarding of developers thanks to automatic code explanation.
- Improving development metrics such as cycle time and deployment frequency.
Refactoring & Optimization
Copilot is useful not only for writing new code, but also for modifying existing code - improving readability, structure and performance.
Examples of use:
- Splitting long functions
You can mark a complex function and ask Copilot to break it down into logical, separate parts:
def process_data_and_generate_report(data):
# načítání, čištění, analýza, generování reportu …
pass
# Prompt v Copilot Chat:
# "/refactor Rozděl tuto funkci `process_data_and_generate_report` na menší části."
# Jeden z možných návrhů Copilota:
def load_data(data):
pass
def clean_data(data):
pass
def analyze_data(data):
pass
def generate_report(data):
passdef process_data_and_generate_report(data):
# načítání, čištění, analýza, generování reportu …
pass
# Prompt v Copilot Chat:
# "/refactor Rozděl tuto funkci `process_data_and_generate_report` na menší části."
# Jeden z možných návrhů Copilota:
def load_data(data):
pass
def clean_data(data):
pass
def analyze_data(data):
pass
def generate_report(data):
pass- Renaming variables/functions
Ask Copilot for more meaningful names:
tmp = 10
# Prompt:
# "Přejmenuj `tmp` na výraznější název."
# Copilot například navrhne:
customer_age = 10- Simplification of logic
For example, complex conditions can be made clearer:
if (user.isAdmin === true && user.isActive === true && user.hasPermission('edit')) {
// ...
}
// Prompt:
// "/refactor Zjednoduš tuto podmínku"
// Copilot může navrhnout:
if (user.isAdmin && user.isActive && user.hasPermission('edit')) {
// ...
}- Performance optimization
Copilot can design more efficient data structures or algorithms:
# Původní:
for i in range(len(my_list)):
if my_list[i] == value:
# …
# Prompt:
# "/optimize Tuto smyčku pro lepší výkon"
# Návrh Copilota:
if value in my_list:
# …Slash-commands in Copilot Chat for refactoring:
- /refactor - refactors the selected block according to the input
- /optimize - suggests performance improvements
- /explain - explains what the code block does, which helps you understand it better before editing. source:
Testing & Debugging
In this section, Copilot combines test generation with support for debugging and bug fixing to speed up development and improve code quality.
Test generation:
- Use /tests in Copilot Chat (or highlight the code in the editor) to have Copilot design unit tests according to your function.
- Copilot can create tests for methods, include edge cases, handle exceptions, etc.
- Experimentally, you can use /setupTests, which sets up a test environment for the whole project (select a framework, create a file, etc.).
Debugging and bug fixes:
- After marking the wrong code, you can use /fix - Copilot will offer the corrected version.
- If the test fails, /fixTestFailure can be used - Copilot analyzes the failure and suggests a fix.
- In experimental mode you can use /startDebugging to create a debugging configuration ("launch configuration") and start debugging directly from copilot chat.
Code sample + prompt:
def compute_factorial(n):
factorial = 1
for i in range(1, n + 1):
factorial *= i * factorial # chyba: násobí navíc factorial
return factorialPrompt for debugging:
/explain Proč tato funkce počítá faktorial špatně?After that:
/fix Oprav logiku tak, aby počítala faktoriál korektně.Copilot can suggest a corrected version:
def compute_factorial(n):
factorial = 1
for i in range(1, n + 1):
factorial *= i
return factorialDocumentation & Onboarding
This section is about how Copilot will make it easier to write documentation and get new developers involved in the project.
Documentation:
- You can use the /doc command to let Copilot generate docstrings, comments, or descriptions of functions and classes.
- For code that is complex or has not been documented, Copilot can explain and add the necessary comments.
Onboarding new developers:
- Newbies can use Copilot Chat for questions like "What does this part of the code do?" and Copilot will give them a clear explanation.
- When creating a new module or part of an application, developers can ask Copilot to generate the project skeleton: folders, tests, configuration files, etc.
- Documentation that is created automatically (docstrings, comments) lowers the barrier for new team members and speeds up their onboarding.
Conclusion
GitHub Copilot is more than just a tool for quickly writing new code. It's a powerful assistant that greatly helps with activities that developers often put off - like refactoring, optimizing, or generating tests. And it's in these areas that it can deliver immediate results: from speeding up development and reducing technical debt to better quality code and increased test coverage.
In this sequel, we'll summarize how to improve the quality of Copilot's outputs and go over advanced features and integrations.