How to Use GitHub Copilot: An AI Coding Assistant (Demo)
In this article, we will guide you through the features and advantages of GitHub Copilot, an innovative AI-powered coding assistant, by showcasing a coding demo. As users of GitHub Enterprise and GitHub Advanced Security, we have firsthand experience utilizing this AI coding assistant, which has prompted us to share our insights and expertise.
What is GitHub Copilot
GitHub Copilot acts as your AI pair programmer, offering intelligent suggestions and support while you code. It monitors your current actions and past code, providing valuable context-based suggestions that actively support your daily coding work. Powered by OpenAI's Codex AI implementation, it possesses extensive knowledge across multiple programming languages and technologies.
AI Coding Assistant: Supported IDEs and Technologies
This AI coding assistant seamlessly integrates with popular IDEs such as Visual Studio, Visual Studio Code, JetBrains IDEs (like IntelliJ and Goland), and Neovim. It supports many programming languages, including Go, Java, JavaScript, TypeScript, and more. Moreover, GitHub Copilot extends its expertise to technologies like Terraform, Docker, Kubernetes, GitHub Actions, and customized files.
Start by writing a comment.
You can start writing a comment. As an example, I write a comment like "return the list of persons sorted by the given field," and when I hit enter, this AI coding assistant generates a suggestion for the method signature in Go, my favourite programming language. It proposes implementing a function called "SortPersons" that takes a list of persons and a field as a string to control the sorting. By pressing the tab, I accept the suggestion, and when I hit tab again, it provides the complete code that precisely implements what I wanted. This way, it effortlessly returns the sorted list of persons based on the specified field. That's how GitHub Copilot works in action.
You can watch the video to follow the process.
Powerful in creating tests
AI GitHub Copilot is not only great at generating code but it's also incredibly powerful when it comes to creating tests. For instance, when you write the comment "test the sort persons function," it quickly suggests a method called "TestSortPersons," and almost everything else you see on the screen is generated by GitHub Copilot. All I have to do is review the code to ensure it is aligned with my intentions and coding style. This way, I can describe what I want, and GitHub Copilot actively supports me in generating the necessary test code.
All-in-One AI-Powered Assistant for Coding, Infrastructure, and Documentation
It's worth mentioning that AI GitHub Copilot operates independently of GitHub itself. You can store your code using hosting platforms like Azure DevOps or GitLab. However, if you do use GitHub, you'll likely find additional benefits. For example, when working with GitHub Actions pipelines, which are typically described in a YAML file, GitHub Copilot's knowledge extends beyond programming languages to include technologies like GitHub Actions. So whether you're coding, configuring GitHub Actions, or working with Docker files, this AI coding assistant provides valuable suggestions and support in various areas of development.
When dealing with Terraform, a popular infrastructure description language, simply by writing a comment like "create a public IP," Copilot can offer resource suggestions tailored to your needs, like a public IP, efficiently organizing it within the appropriate resource group like in this example "Camino." This demonstrates Copilot's ability to assist in diverse technologies, including writing documentation and infrastructure provisioning with Terraform.
GitHub Actions
GitHub Copilot's support extends beyond programming languages and includes a wide range of technologies used in modern development. Its coverage is extensive, from GitHub Actions and Dependabot to HTML, CSS, and other front-end technologies. At PRODYNA, we have successfully implemented Copilot and are actively leveraging its capabilities.
Let's dive into even more exciting parts of this AI coding assistant in our demo.
Building a small REST service in Go from scratch
Using Intellij as my IDE, I created a new project named "demo." As I start writing code in the main.go file, GitHub Copilot provides intelligent suggestions, such as the package name and a simple main function that prints "Hello." The code is generated with just a few tabs and enter key presses. I run the application and successfully see the "Hello world" message. It's incredible how GitHub Copilot accelerates coding and ensures accurate code generation.
Create a REST endpoint that exposes information about persons
Alright, let's dive into a more complex demo. In this case, I want to create a REST endpoint that exposes information about persons.
First, I need to define the structure of a person, including ID, first name, and last name. GitHub Copilot suggests a structure called "person" and even adds JSON annotations for serialization. Perfect. Next, I require a function to create a person, and once again, an AI coding assistant offers a convenient solution with the "NewPerson" function. I also need a structure to hold multiple persons, which I name "Persons." Amazingly, AI GitHub Copilot anticipates my needs and generates the code accordingly, even including the necessary annotations. Moving on, I want to create a function that generates five random persons. It quickly provides the initial code, and I can further refine it by selecting alternative suggestions if needed. Lastly, I want a function that retrieves the IDs of all persons. GitHub Copilot promptly suggests the "GetIDs" function, which returns an array of strings. Running the test confirms its success.
Now, let's focus on creating the REST service. I need a HTTP router, and GitHub Copilot recognizes my need for the Chii router. It suggests the router implementation and generates the code when I confirm it. However, I encounter an issue since GitHub Copilot doesn't automatically import the necessary packages. I add the missing import, download the Chi library, and the issue is resolved. An AI coding assistant continues to assist me by suggesting routes, such as "/persons," which returns the IDs of all persons. It creates the route with the correct logic based on the existing functions I've defined. With the server running on port 8080, I test the endpoint using curl, and it successfully returns a list of persons.
To improve the response format, I modify the code to marshal the IDs into JSON format. GitHub Copilot provides suggestions for error handling and using logging frameworks. I opt for Logrus, import it, and incorporate it into the code. Now, when I run the server and make a curl request, I receive a JSON response.
Continuing with the implementation, I want to retrieve persons by their ID. GitHub Copilot intelligently suggests adding a route for the "person/{id}" endpoint, which returns the person with the given ID. It generates the code, including the necessary logging. To refine the implementation, I need to create a dedicated function, "FindPersonByID," to handle the lookup. AI GitHub Copilot recognizes the new function and adapts the code accordingly. After resolving a minor issue, I’m restarting the server, testing it, and successfully retrieving a specific person using their ID.
To ensure code quality, I must also create a test for the "FindPersonByID" function. GitHub Copilot suggests the test function, and I can run it to verify the desired behaviour.
In summary, it has been instrumental in guiding the development process throughout this demo. It not only generates working code but also helps with documentation and adheres to established coding practices.
Packaging the Application as a Docker Container with Multistage Build
In the final part of the demo, we focus on packaging the application as a Docker container. To achieve this, we create a Dockerfile using a multistage build approach. GitHub Copilot assists in generating the Dockerfile, simplifying the process of building and running the application.
The Dockerfile begins with a description of a multistage build for the Go application. An AI coding assistant suggests the stages, and we specify the builder stage as Golang 1.20. We install Git as a dependency for downloading project dependencies. The current working directory is set to the "app" directory, and the Go module and sum files are copied. Dependencies are then downloaded using "go mod download".
Next, the source files from the current directory are copied to the working directory. The last step in the builder stage is building the application. Moving on to the second stage, we choose an Alpine image as the runtime environment. Port 8080 is exposed, which GitHub Copilot intelligently recognizes. Finally, the command to run the executable is specified as "main".
With the Dockerfile complete, we proceed to build the Docker image. After connecting to Docker, we execute the build command, which pulls the necessary images and builds the container. Once the build is complete, we can see the newly created image, named "demo", in the list of Docker images.
To test the container, we run it using the Docker image we just built. Upon successful execution, the application is up and running. Although the demo was prepared in advance, it is important to note that starting from scratch allowed us to create the Dockerfile and build the container effortlessly with the help of GitHub Copilot.
As a software engineer, I’m amazed by the power of GitHub Copilot. I can create a fully functional REST service with minimal coding effort by simply describing what I want. GitHub Copilot intelligently provides suggestions and support throughout the process, turning my descriptions into working code. It's incredible how this AI-powered coding assistant revolutionizes development and accelerates productivity.
What GitHub Copilot cannot do? Will I lose my job as a coder?
As a software engineer, it's essential to understand that while GitHub Copilot is an incredibly powerful tool, it does have its limitations. While it can assist in writing and implementing code based on descriptions and suggestions, it cannot handle strategic aspects such as project structure or file organization. These are tasks that still require human understanding and decision-making.
A common concern is whether an AI coding assistant will make coders obsolete and result in job loss. The answer is no. While GitHub Copilot significantly enhances productivity and provides valuable support, it does not replace the role of a coder. Coder's expertise in understanding the overall requirements, making critical decisions, and solving complex problems is still crucial. GitHub Copilot is a valuable assistant, but it does not replace the need for skilled coders who can effectively translate ideas into functional and efficient code.
Benefits of using AI GitHub Copilot
In just a few minutes, I wrote 175 lines of code from scratch, including the main application, tests, and a Dockerfile. GitHub Copilot's assistance was invaluable, enabling me to go from zero to hero effortlessly. It's truly amazing how quickly and effectively it helps streamline the coding process.
Coding speed: GitHub Copilot significantly enhances coding speed by providing intelligent code suggestions and auto-completion. It analyzes the context and offers relevant code snippets, saving time and effort in typing repetitive or boilerplate code. This allows you to write code more swiftly and focus on higher-level problem-solving.
Helps with repetitive tasks: It excels at automating repetitive coding tasks. It can generate code for common patterns, create function stubs, and suggest solutions for everyday programming challenges. By reducing the need to write repetitive code from scratch, Copilot increases efficiency and saves developers from tedious and time-consuming tasks.
Documentation: An AI coding assistant aids in creating documentation and comments within the code. As you write code, Copilot can suggest relevant comments and documentation for functions, classes, and other code components. This helps improve code readability, facilitates future maintenance, and ensures that the codebase remains understandable even after a significant amount of time has passed.
Reduce monotonous work: It reduces the burden of monotonous and mundane coding tasks. It takes care of boilerplate code generation, routine implementations, and repetitive code optimizations, freeing up developers' time and mental energy. This allows you to focus on more creative and challenging aspects of the project, leading to increased job satisfaction and engagement.
Tests: GitHub Copilot can also assist in writing tests. It can suggest test case scenarios, test function setups, and assertions based on the code context. Automating parts of the testing process helps ensure code reliability and promotes a test-driven development approach. It speeds up the creation of unit tests, making it easier to maintain code quality and identify potential issues.
Data Privacy
At PRODYNA, we prioritize your data privacy when using GitHub Copilot. Your code and snippets are securely processed on GitHub, with adherence to our documented privacy practices. After generating recommendations, your code is promptly discarded. Our data processing procedures are fully outlined in the GitHub Copilot for Business Privacy Statement, accessible via QR code or online search.
We will engage with you to explain the benefits of GitHub Copilot, ensuring transparency and offering faster coding and quicker results. If you disagree with the privacy terms, we can disable the AI coding assistant, while agreeing allows us to continue providing transparent code snippet transfer and processing.
—---
In conclusion, GitHub Copilot is an AI-powered coding assistant that accelerates coding speed, streamlines repetitive tasks, improves documentation, reduces mundane work, and respects data privacy, enhancing the overall development process for software engineers.