GitHub Copilot Tutorial – DZone – Uplaza

This text describes the GitHub Copilot software and the primary pointers and assumptions relating to its use in software program improvement initiatives. The rules concern each the software’s configuration and its software in on a regular basis work and assume the reader will use GitHub Copilot with IntelliJ IDEA (through a devoted plugin).

GitHub Copilot: What Is It?

GitHub Copilot is an AI developer assistant that makes use of a generative AI mannequin skilled for all programming languages accessible in GitHub repositories. The complete description and documentation of the software is offered right here. 

There are different related instruments in the marketplace, equivalent to OpenAI Codex, JetBrains AI Assistant, or Tabnine, however GitHub Copilot stands out because of the following options:

  • The biggest and most numerous assortment for coaching an AI mannequin – GitHub repositories
  • Estimated utilization share – presently approx. 40-50% (in line with Abhay Mishra’s article primarily based on undisclosed trade insights), however the market may be very dynamic
  • Help for standard applied sciences – we’ve examined it with the Java programming language, Scala, Kotlin, Groovy, SQL, Spring, Dockerfile, OpenShift, Bash
  • Excellent integration with the JetBrains IntelliJ IDEA IDE
  • Low entry-level as a consequence of fast and simple configuration, common ease of use, clear documentation, and lots of utilization examples on the web
  • A variety of functionalities, together with:
    • Recommendations whereas writing code
    • Producing code primarily based on feedback in pure language
    • Taking current code into consideration when producing a brand new code snippet
    • Creating unit exams
    • Chat – lets you ask questions relating to code, language, and expertise, in addition to suggests corrections for simplifying the code
    • CLI – help for working within the console and creating bash scripts

Our Targets

Our essential aim for utilizing GitHub Copilot was to enhance the effectivity of writing code and its high quality. As well as, we meant it to help and help us in work through which programmers lack data and expertise. Listed below are the precise objectives that we wished our improvement group to attain through the use of GitHub Copilot:

1. Accelerating Improvement

  • Producing code fragments
  • Producing SQL queries
  • Hints for creating and modifying OpenShift and Dockerfile configuration recordsdata
  • Sooner seek for options utilizing the chat operate, e.g., rationalization of standard expressions, operation of libraries or framework mechanisms

2. Enhancing Code High quality

  • Producing unit exams with edge circumstances – each in Java and Groovy languages
  • Suggesting corrections and simplifications in our personal code

3. Working With Much less Continuously Used Applied sciences

  • Explaining and producing code (together with unit exams) in Scala and Kotlin
  • Help whereas utilizing “legacy” options like Activiti, and many others. 
  • Help in creating and understanding configuration recordsdata

4. Extra Environment friendly Administrative Work within the Console Utilizing CLI Capabilities

Device Limitations Pointers

Since GitHub Copilot is predicated on generative AI, you need to all the time do not forget that it might generate incorrect code or responses. Subsequently, when utilizing the software, you need to concentrate on potential limitations and apply the precept of restricted belief and verification. The principle limitations are offered within the desk beneath.

Limitation Description
Restricted scope of data The software is predicated on code present in GitHub repositories. Some issues, or complicated constructions, languages or knowledge notations, have poor illustration within the coaching units
Dynamic improvement and options within the beta part The software is growing very dynamically. Patches and updates seem each week or each a number of weeks, which signifies that many components of the software will not be working correctly. Some functionalities, equivalent to GitHub Copilot CLI, are nonetheless in beta
Inaccurate code The software supplier informs that the generated code could not meet the person’s expectations, could not resolve the precise downside, and should comprise errors
Inaccurate chat responses When utilizing chat, the accuracy of the reply relies upon largely on the query or command formulated. The documentation says that “Copilot Chat is not designed to answer non-coding questions”, so there are attainable solutions, particularly in areas not strictly associated to the code (design, and many others.), that won’t be applicable and even smart
Harmful code The coaching set (repositories) can also comprise code components that violate safety guidelines, each in the safety and security sense, equivalent to API keys, community scanning, IP addresses, code that overloads sources or causes reminiscence leaks, and many others.

To reduce the unfavorable affect of the recognized GitHub Copilot limitations, you must all the time:

  • Verify different recommendations (utilizing Ctrl+[ and Ctrl+], and many others.) and select those that finest swimsuit a given state of affairs
  • Learn and analyze the correctness of the generated code
  • Check and run code in pre-production environments – primarily domestically and within the improvement surroundings
  • Submit the generated code to code overview

Necessary: By no means deploy the code generated by GitHub Copilot to manufacturing environments with out performing the above checks.

Configuration Pointers

On this part, we’ll current the fundamental data relating to the pricing plans (with benefits and downsides for every possibility, as seen from the attitude of our meant objectives) and private account configuration (for each GitHub Copilot and the IntelliJ IDEA plugin). 

Pricing Plans

GitHub Copilot gives three subscription plans with completely different scopes of supplied performance and price. In our case, two plans have been price contemplating: Copilot Particular person or Copilot Enterprise. The Copilot Enterprise plan moreover gives entry to talk through the github.com web site and producing summaries for pull requests, which was unimportant for our assumed objectives (however it might be completely different in your case). Each plans’ essential benefits and downsides are offered within the desk beneath.

Plan Benefits Disadvantages
GitHub Copilot Particular person Decrease price at $10/month/person

Gives the important thing performance required to attain the meant objectives

Lack of management over software configuration and person entry by the group
GitHub Copilot Enterprise Gives the important thing performance required to attain the meant objectives

Management over software configuration and person entry by the group

Larger price at $19/month/person

In our case, Copilot Enterprise was the higher possibility, particularly as a result of it permits full management over the configuration and entry to the software for builders within the group. In case you’re working by yourself, the Copilot Particular person plan could be sufficient.

Account Configuration

You possibly can configure GitHub Copilot when buying a subscription plan, and the settings will also be modified after activating the account within the group’s account settings on GitHub. On the account stage, there have been two key parameters for our use case to configure in GitHub Copilot, described within the desk beneath.

Choice identify Description Beneficial settings
Recommendations matching public code Accessible choices:

Allowed and  Blocked

Determines whether or not to indicate or to dam code recommendations that overlap round 150 traces with public code

Blocked

This selection reduces the danger of duplicating code from public repositories, thus decreasing the uncertainty in regards to the copyright possession of the code

Enable GitHub to make use of my code snippets for product enhancements Accessible choices: Sure and No

Determines whether or not GitHub, its associates, and third events could use person code snippets to discover and enhance GitHub Copilot recommendations, associated product fashions, and options

No

In case you plan to make use of GitHub Copilot for industrial functions, GitHub and its related entities shouldn’t use person code as a consequence of copyright issues

Here’s a detailed description and directions for altering configuration choices in your GitHub account. 

IntelliJ IDEA Plugin Configuration

To allow GitHub Copilot within the IntelliJ IDEA IDE, you need to set up the GitHub Copilot extension from the Visible Studio Code market. Set up is completed through the IDE within the plugin settings. After set up, log in to your GitHub account along with your system code. You could find detailed directions for putting in and updating the plugin right here.

The GitHub Copilot plugin for the IntelliJ IDEA IDE gives the flexibility to configure the next issues:

  • Automated submission of recommendations
  • The way in which recommendations are displayed
  • Automated plugin updates
  • Supported languages
  • Keyboard shortcuts

In our case, utilizing the default plugin settings was beneficial as a result of they guarantee good working consolation and are appropriate with the present software documentation. Any modifications to the configuration will be made by every person in line with their very own preferences.

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

How To Use GitHub Copilot in IntelliJ

Listed below are some pointers for utilizing key functionalities that may show you how to use the GitHub Copilot software optimally.

Producing Software Code

When To Use

  • Creating lessons
  • Creating fields, strategies, constructors
  • Writing code snippets inside strategies

How To Use

  • By writing code and utilizing automated recommendations – it’s all the time price checking different recommendations utilizing the Ctrl+] / Ctrl+[ keys

  • By writing concise and precise comments in natural English

  • 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

A screenshot showing the example.

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:

  • 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:

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:

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

  • 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

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
Command Example Result
gh copilot suggest  # gh copilot suggest “find IP number in text file” grep -E -o ‘([0-9]{1,3}.){3}[0-9]{1,3}’  
gh copilot clarify  # gh copilot clarify “curl -k” curl is used to concern net requests, e.g., obtain net pages                                                                         

ok or –insecure permits curl to carry out insecure SSL connections and transfers

How To Use GitHub Copilot Chat

We’ve written a separate chapter for the GitHub Copilot Chat – as there are a number of use circumstances price speaking about. Let’s undergo them individually and talk about particular pointers for every case.

Creating New Functionalities

When To Use

  • If you end up on the lookout for an answer to an issue, equivalent to creating an internet site, a way that performs a selected activity, error dealing with for a given block of code/methodology/class, and many others.

How To Use

  • Enter a question in pure English relating to the performance you’re on the lookout for. It ought to concern subjects associated to programming – code, frameworks/libraries, providers, structure, and many others. Beneath is an instance for the question: The right way to get foreign money change knowledge?

Utilizing Common Expressions

When To Use

  • When you want to create and confirm a daily expression

How To Use

  • Enter a question in pure English relating to the sample you’re on the lookout for. The instance beneath exhibits a generated methodology with an incorrect sample, a question, and a response with an evidence and corrected code

Discovering Errors within the Code

When To Use

  • Whenever you create new lessons or strategies
  • When analyzing a category or methodology that causes errors

How To Use

  • You possibly can choose the code and select GitHub Copilot > Repair This from the context menu, enter the /repair command, or write an instruction in pure English, e.g., Discover attainable errors on this class. You possibly can specify a command to a way identify or error kind. For instance, for a easy class, explanations of potential errors have been obtained, and the chat generated code to deal with these errors:

Clarification of Present Code

When To Use

  • Whenever you don’t perceive what precisely a module, class, methodology, piece of code, common expression, and many others., does
  • Whenever you don’t know the framework or library mechanism used

How To Use

  • In a category or methodology, you possibly can choose GitHub Copilot > Clarify this from the context menu, kind the /clarify command, or write a question in pure English in regards to the problematic code ingredient, e.g., Clarify what is that this class doing. The instance beneath presents an evidence of the category and its strategies. This is applicable to the category generated within the bug-finding instance

Simplify Present Code

When To Use

  • When the code is sophisticated and obscure or unnecessarily in depth
  • When refactoring the code

How To Use

  • In a category or chosen methodology or code fragment, you possibly can choose GitHub Copilot > Simplify This from the context menu, kind the /simplify command, or write a question in pure English. An instance of a easy methodology of refactoring for a category is beneath:

The outcome:

Abstract: A Highly effective Device, as Lengthy as You’re Cautious

As you possibly can see, GitHub Copilot could be a highly effective software in a software program developer’s arsenal. It may velocity up and simplify numerous processes and day-to-day duties. Nevertheless, as with all issues associated to generative AI, you possibly can by no means totally belief this software – subsequently, the essential rule is to all the time learn, overview, and check what it creates. 

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version