GitHub Copilot tutorial: We’ve tested it with Java and here's how you can do it too
Tags: ai, githubcopilot, java, intellij
Published: 2024-06-07 08:15:12

**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](https://dzone.com/articles/comparison-of-various-ai-code-generation-tools-ava) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/caiqolrlsrbz8vrli0yv.png)

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](https://docs.github.com/en/copilot/copilot-individual/about-github-copilot-individual#understanding-the-differences-between-copilot-individual-copilot-business-and-%20copilot-enterprise) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shjw3t49kf50bf07knzf.png)

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](https://github.com/). 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfoytxe46y4zr7kfywhk.png)

[Here](https://docs.github.com/en/copilot/configuring-github-copilot/configuring-github-copilot-settings-on-githubcom) 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](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot). 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](https://docs.github.com/en/copilot/using-github-copilot/using-github-copilot-code-suggestions-in-your-editor#installing-the-github-copilot-plugin-in-your-jetbrains-ide).

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b29mrifqujbvoqkzz1mr.png)

Our keymap settings for GitHub Copilot in IntelliJ IDEA:
![Our keymap settings for GitHub Copilot in IntelliJ IDEA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8cw4f2vlualhr8rtj3c.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3t7esmbmpva9ec0auqmy.png)

- By writing concise and precise comments in natural English

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m83xgis05wm2ojqapr1b.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9n6roayujze10n6aiiq.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtp8a0biicix5p238gxq.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3zmytxygspix0kq5ogb.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtlh8e1acnd96tubn46c.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xf0nubmrrc9dcy10doz.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0sbbzte8mtvm6w68ypp.png)

### 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](https://docs.github.com/en/copilot/github-copilot-in-the-cli/using-github-copilot-in-the-cli#installing-copilot-in-the-cli)
- Currently, the tool offers two commands, summarized in the table below


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/728bdk6qhzm2fuycms0e.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvr6kaem6c3fq2ub1ho5.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3q8sgoo8h4ebz66vj6f7.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1wowaxy8gutk2ttrp47.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k55ywi960u0u1l70qdys.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xnqoyansce7elalyxy0.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/108t2q2sue56aw1kgmp1.png)

The result:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gq4dbmhnc5yis70c4i6f.png)

## 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](https://pretius.com/blog/open-ai-code-review/)
2. [Biscuits+ChatGPT: Using AI to generate Oracle APEX Theme Roller Styles](https://pretius.com/blog/using-ai-to-generate-oracle-apex-themes/)
3. [AI in software testing: Can Pretius OpenAI Reviewer help you with test automation?](https://pretius.com/blog/ai-in-software-testing/)