DEV Community

Cover image for GitHub Copilot tutorial: We’ve tested it with Java and here's how you can do it too
Karol Świder
Karol Świder

Posted on • Originally published at pretius.com

GitHub Copilot tutorial: We’ve tested it with Java and here's how you can do it too

This article describes the GitHub Copilot tool and the main guidelines and assumptions regarding its use in software development projects. The guidelines concern both the tool's configuration and its application in everyday work and assume the reader will use GitHub Copilot with IntelliJ IDEA (via a dedicated plugin).

GitHub Copilot – What is it?

GitHub Copilot is an AI developer assistant that uses a generative AI model trained for all programming languages available in GitHub repositories. The full description and documentation of the tool is available here. 

There are other similar tools on the market, such as OpenAI Codex, JetBrains AI Assistant or Tabnine, but GitHub Copilot stands out due to the following features:

  1. The largest and most diverse collection for training an AI model – GitHub repositories
  2. Estimated usage share – currently approx. 40-50% (according to Abhay Mishra’s article based on undisclosed industry insights), but the market is very dynamic
  3. Support for popular technologies – we’ve tested it with the Java programming language, Scala, Kotlin, Groovy, SQL, Spring, Dockerfile, OpenShift, Bash
  4. Very good integration with the JetBrains IntelliJ IDEA IDE
  5. Low entry-level due to quick and easy configuration, general ease of use, clear documentation, and many usage examples on the internet
  6. A wide range of functionalities, including:
  • Suggestions while writing code
  • Generating code based on comments in natural language
  • Taking existing code into account when generating a new code snippet
  • Creating unit tests
  • Chat – allows you to ask questions regarding code, language, and technology, as well as suggests corrections for simplifying the code
  • CLI – support for working in the console and creating bash scripts

Our goals

Our main goal for using GitHub Copilot was to improve the efficiency of writing code and its quality. In addition, we intended it to support and assist us in work in which programmers lack knowledge and experience. Here are the specific goals that we wanted our development team to achieve by using GitHub Copilot:

1. Accelerating development:

  • Generating code fragments
  • Generating SQL queries
  • Hints for creating and modifying OpenShift and Dockerfile configuration files
  • Faster search for solutions using the chat function, e.g., explanation of regular expressions, operation of libraries or framework mechanisms

2. Improving code quality:

  • Generating unit tests with edge cases – both in Java and Groovy languages
  • Suggesting corrections and simplifications in our own code

3. Working with less frequently used technologies:

  • Explaining and generating code (including unit tests) in Scala and Kotlin
  • Support while using “legacy” solutions like Activiti, etc. 
  • Support in creating and understanding configuration files

4. More efficient administrative work in the console using CLI functions

Tool limitations guidelines

Since GitHub Copilot is based on generative AI, you must always remember that it may generate incorrect code or responses. Therefore, when using the tool, you must be aware of potential limitations and apply the principle of limited trust and verification. The main limitations are presented in the table below.

Image description

To minimize the negative impact of the identified GitHub Copilot limitations, you should always:

  • Check alternative suggestions (using Ctrl+[ and Ctrl+], etc.) and choose the ones that best suit a given situation
  • Read and analyze the correctness of the generated code
  • Test and run code in pre-production environments – primarily locally and in the development environment
  • Submit the generated code to code review

Important: Never deploy the code generated by GitHub Copilot to production environments without performing the above checks.

Configuration guidelines

In this section, we’ll present the basic information regarding the pricing plans (with advantages and disadvantages for each option, as seen from the perspective of our intended goals) and personal account configuration (for both GitHub Copilot and the IntelliJ IDEA plugin). 

Pricing plans

GitHub Copilot offers three subscription plans with different scopes of offered functionality and cost. In our case, two plans were worth considering: Copilot Individual or Copilot Business. The Copilot Enterprise plan additionally offers access to chat via the github.com website and generating summaries for pull requests, which was unimportant for our assumed goals (but it may be different in your case). Both plans' main advantages and disadvantages are presented in the table below.

Image description

In our case, Copilot Business was the better option, especially because it allows full control over the configuration and access to the tool for developers in the team. If you’re working on your own, the Copilot Individual plan might be enough.

Account configuration

You can configure GitHub Copilot when purchasing a subscription plan, and the settings can also be changed after activating the account in the organization's account settings on GitHub. At the account level, there were two key parameters for our use case to configure in GitHub Copilot, described in the table below.

Image description

Here is a detailed description and instructions for changing configuration options in your GitHub account. 

IntelliJ IDEA plugin configuration

To enable GitHub Copilot in the IntelliJ IDEA IDE, you must install GitHub Copilot extension from the Visual Studio Code marketplace. Installation is done via the IDE in the plugin settings. After installation, log in to your GitHub account with your device code. You can find detailed instructions for installing and updating the plugin here.

The GitHub Copilot plugin for the IntelliJ IDEA IDE offers the ability to configure the following things:

  • Automatic submission of suggestions
  • The way suggestions are displayed
  • Automatic plugin updates
  • Supported languages
  • Keyboard shortcuts

In our case, using the default plugin settings was recommended because they ensure good working comfort and are compatible with the existing tool documentation. Any changes to the configuration can be made by each user according to their own preferences.

Our GitHub Copilot plugin settings in IntelliJ IDEA:
Our GitHub Copilot plugin settings in Intellij IDEA

Our keymap settings for GitHub Copilot in IntelliJ IDEA:
Our keymap settings for GitHub Copilot in IntelliJ IDEA

How to use GitHub Copilot in IntelliJ

Here are some guidelines for using key functionalities that will help you use the GitHub Copilot tool optimally.

Generating application code

When to use:

  • Creating classes
  • Creating fields, methods, constructors
  • Writing code snippets inside methods

How to use:

  • By writing code and using automatic suggestions – it’s always worth checking other suggestions using the Ctrl+] / Ctrl+[ keys

Image description

  • By writing concise and precise comments in natural English

Image description

  • Using the chat function – the chat can generate a fragment of code in response to a query (see examples in the section “Using the GitHub Copilot Chat” below) and allows you to quickly generate code using the Copy Code Block or Insert Code Block at Cursor buttons that appear in the section with code in the chat window

Image description

Writing unit tests

When to use:

  • Creating new classes and methods that we want to cover with unit tests
  • Coverage of existing classes and methods with unit tests

How to use:

  • By writing a comment in the test class. For example, if you write // Unit test in JUnit for CurrencyService, you will get the following result:

Image description

  • It is possible to generate individual test methods by entering in the comment the test case that the method is to test. Similarly, you can generate mocks in the test class.
  • Using the chat – you can select the GitHub Copilot > Generate Test option from the context menu, enter the /tests command, or write an instruction in a natural language, e.g., Generate unit test for class CurrencyService. In response, you will receive a descriptive explanation of the test structure and the code of the entire test class:

Image description

Generating SQL queries and stored procedures

When to use:

  • When writing DDL, DML and DQL queries that will be used in the application
  • During data analysis and errors related to data in the database
  • When writing scripts and stored procedures

How to use:

  • IMPORTANT: you must have a database connection configured in IntelliJ IDEA or DataGrip
  • By writing queries and using automatic suggestions
  • By writing a comment, e.g. if you write - - get party data for account, you will get the following result:

Image description

Creating OpenShift configuration or other configuration files

When to use:

  • Creating or modifying configuration files
  • Analysis of directives, their options and values, and configuration mechanisms

How to use:

  • By writing directives and using automatic suggestions

Image description

  • Using the chat – you can select the directive and choose GitHub Copilot > Explain This from the context menu, enter the /explain command, or write a query in natural language about a given configuration element

Image description

Using the BASH console

When to use:

  • When trying to use obscure console commands
  • For an explanation of command operation and its options
  • To find the right command to perform a task
  • When writing BASH scripts

How to use:

  • IMPORTANT: to use the CLI tool, install GitHub CLI with the gh-copilot extension according to the instructions
  • Currently, the tool offers two commands, summarized in the table below

Image description

How to use GitHub Copilot Chat

We’ve written a separate chapter for the GitHub Copilot Chat – as there are several use cases worth talking about. Let’s go through them individually and discuss specific guidelines for each case.

Creating new functionalities

When to use:

  • When you are looking for a solution to a problem, such as creating a website, a method that performs a specific task, error handling for a given block of code/method/class, etc.

How to use:

  • Enter a query in natural English regarding the functionality you are looking for. It should concern topics related to programming – code, frameworks/libraries, services, architecture, etc. Below is an example for the query: How to get currency exchange data?

Image description

Using regular expressions

When to use:

  • When you need to create and verify a regular expression

How to use:

  • Enter a query in natural English regarding the pattern you are looking for. The example below shows a generated method with an incorrect pattern, a query, and a response with an explanation and corrected code

Image description

Finding errors in the code

When to use:

  • When you create new classes or methods
  • When analyzing a class or method that causes errors

How to use:

  • You can select the code and choose GitHub Copilot > Fix This from the context menu, enter the /fix command, or write an instruction in natural English, e.g., Find possible errors in this class. You can specify a command to a method name or error type. For example, for a simple class, explanations of potential errors were obtained, and the chat generated code to handle these errors:

Image description

Image description

Explanation of existing code

When to use:

  • When you don't understand what exactly a module, class, method, piece of code, regular expression, etc., does When you don’t know the framework or library mechanism used

How to use:

  • In a class or method, you can select GitHub Copilot > Explain this from the context menu, type the /explain command, or write a query in natural English about the problematic code element, e.g., Explain what is this class doing. The example below presents an explanation of the class and its methods. This applies to the class generated in the bug-finding example

Image description

Simplify existing code

When to use: 

  • When the code is complicated and difficult to understand or unnecessarily extensive
  • When refactoring the code

How to use:

  • In a class or selected method or code fragment, you can select GitHub Copilot > Simplify This from the context menu, type the /simplify command, or write a query in natural English. An example of a simple method refactoring for a class is below:

Image description

The result:

Image description

Summary: A powerful tool, as long as you’re cautious

As you can see, GitHub Copilot can be a powerful tool in a software developer’s arsenal. It can speed up and simplify various processes and day-to-day tasks. However, as with all things related to generative AI, you can never fully trust this tool – therefore, the crucial rule is to always read, review, and test what it creates. 

If you’re interested in AI, we advise you to check out a couple of other articles on our blog regarding the possibilities offered by this technology:

  1. AI code review – We've tried OpenAI at our company, and here's what we've learned
  2. Biscuits+ChatGPT: Using AI to generate Oracle APEX Theme Roller Styles
  3. AI in software testing: Can Pretius OpenAI Reviewer help you with test automation?

Top comments (0)