Tools2023-05-22

8 Ways To Write Better Code With GitHub Copilot

GitHub Copilot is a revolutionary AI-powered code completion tool developed by OpenAI and GitHub. Know how to use GitHub Copilot for writing perfect code.
8 things you didn’t know you could do with GitHub Copilot

GitHub Copilot is a revolutionary AI-powered code completion tool developed by Microsoft-owned OpenAI and GitHub that has changed the way software developers write code. It offers a wide range of features, from generating functions to writing tests and even helping with git commands. In this article, we will explore various tips and tricks to make the most out of GitHub Copilot, ultimately improving your productivity as a developer.

GitHub Copilot draws context from comments and code, providing suggestions for individual lines and entire functions. Powered by OpenAI Codex, Copilot is available as a technical preview extension for Visual Studio Code, Neovim, and JetBrains IDEs. Offering individual plans at $10/month and business plans at $19/user/month, it provides seamless integration with your editor, code generation from natural language prompts, multiline function suggestions, test generation acceleration, and additional features for businesses, such as policy management and corporate proxy support.

[ Read Related: Use GitHub Copilot for Pull Request ]

8 Ways To Write Better Code With GitHub Copilot

1. Structuring Your Code for Better Assistance

To improve the suggestions provided by Copilot, it's crucial to give it a clear context and structure to work with. For example, if you want Copilot to use a helper function, start by building the helper function first. This allows Copilot to recognize it and integrate it into the following code.

function helperFunction(a, b) {
    return a * b;
}

Additionally, avoid using hard-coded constant values. Instead, declare well-named variables above the generated code to make it easier for Copilot to incorporate them.

const apiEndpoint = 'https://api.example.com/data';

2. Utilizing Copilot for Git Commands

If you struggle to remember git commands, GitHub Copilot can assist you. Type your command request in an empty tab, and Copilot will generate the order for you. Although it might not always be perfect, it provides enough information to point you in the right direction.

// How to remove a file from git
git rm <filename>

3. Writing Tests with Copilot

Writing tests for complex features can be time-consuming, but Copilot excels in this area. By providing context and requirements, Copilot can generate accurate tests for your application, streamlining the testing process and ensuring your code is reliable.

// Test if the sum function adds two numbers correctly
describe('sum', () => {
    it('should return the sum of two numbers', () => {
        const result = sum(2, 3);
        expect(result).toBe(5);
    });
});

4. Generating Comments and Documentation

While providing comments can help Copilot understand your code better, the reverse is also true. If you have existing code that needs documentation, Copilot can generate comments for you. This can save time and ensure consistency in your documentation.

function square(number) {
    return number * number;
}

// Copilot-generated comment:
// Calculates the square of a given number.

5. Explaining Code with Copilot Labs

The Copilot Labs extension offers a side panel where you can highlight a block of text, and Copilot will attempt to explain it to you in plain English. This can be especially helpful if you come across cryptic or unfamiliar code in a project.

6. Learning New Programming Languages

GitHub Copilot can help you learn new programming languages as well. By focusing on writing precise, accurate comments, you can rely on Copilot to suggest lines of code in the new language. This allows you to pick up on patterns and syntax quickly and efficiently.

7. Assisting Non-Native English Speakers

Copilot supports multiple languages, making it a valuable tool for developers worldwide. It can interpret comments in various languages and provide code suggestions accordingly. Moreover, Copilot can also help translate words between English and other languages.

8. Creating Dictionaries and Lookup Data

GitHub Copilot is excellent at generating dictionaries of lookup data. Provide it with a comment and the initial lines of code, and Copilot can generate a dictionary based on your requirements.

# Create a dictionary of 3 country codes and country names
country_codes = {
    "US": "United States",
    "CA": "Canada",
    "MX": "Mexico",
}

Want to integrate GitHub status notifications? Know how to set up GitHub status notifications to keep track of your codebase's health and deployments.

Conclusion

GitHub Copilot is a game-changing AI-powered pair programmer that promises to revolutionize the way developers work, enabling them to write code more efficiently, streamline test writing, improve comment quality, and enhance the pair programming experience.

As you begin to integrate GitHub Copilot into your development workflow, remember to stay open to its suggestions and be willing to adapt your coding habits. Additionally, keep in mind that Copilot is still in the technical preview stage, so be prepared for potential limitations and updates as the technology continues to evolve.

In conclusion, embracing GitHub Copilot is a strategic move for developers and organizations alike, as it offers an invaluable opportunity to optimize their workflows, reduce time spent on tedious tasks, and ensure code quality. By harnessing the power of this AI-driven pair programmer, you can transform your software development practices and drive your organization toward success in an increasingly competitive market.

Subscribe to the Hatica blog today to read more about unblocking developers, and boosting productivity with engineering analytics.

Subscribe to Hatica's blog

Get bi-weekly insights straight to your inbox

Share this article:
Table of Contents
  • 8 Ways To Write Better Code With GitHub Copilot
  • 1. Structuring Your Code for Better Assistance
  • 2. Utilizing Copilot for Git Commands
  • 3. Writing Tests with Copilot
  • 4. Generating Comments and Documentation
  • 5. Explaining Code with Copilot Labs
  • 6. Learning New Programming Languages
  • 7. Assisting Non-Native English Speakers
  • 8. Creating Dictionaries and Lookup Data
  • Conclusion

Ready to dive in? Start your free trial today

Overview dashboard from Hatica