Using GitHub Copilot Effectively

Posted on Jun 14, 2023 • 11 min read

Rajesh Jadav

Senior Software Engineer (Android)

Using GitHub Copilot Effectively

What is GitHub Copilot?

Github Copilot is an AI pair programmer that offers auto-complete style suggestions as you code. It helps you to write your code faster with less work. The term “AI pair programmer” implies that it can’t work without you. When a developer is writing code, GitHub Copilot analyses the context, such as variable names, function signatures, and surrounding code, and generates relevant suggestions in real-time.

GitHub Copilot is available as an extension in Visual Studio Code, Visual Studio, Neovim, and the JetBrains suite of IDEs. For more detailed information, please refer to  “Getting Started with GitHub Copilot.”

How does it work?

GitHub Copilot is based on Open AI Codex which is a machine learning model that translates natural language into code. Codex is a version of GPT-3 (Generative Pre-trained Transformer 3) which is fine-tuned for programming tasks.

GitHub Copilot is trained on natural language text and the vast amount of public code available on GitHub. It also processes all of the files open in a developer’s IDE instead of just the single one the developer is working on. By opening all files relevant to their project, developers can assist to generate more accurate suggestions.

For more detailed information, please refer to Working with the LLMs behind GitHub Copilot.

What can GitHub Copilot do?

Let’s look at different examples of how you can use GitHub Copilot. These examples will help you to better understand the tool’s functionality and versatility.

1. Convert comments to code

GitHub Copilot provides accurate suggestions when you write comments with clear instructions. To receive the desired output, it is important to be specific and intentional in your inputs.

test

Similarly, if you are utilizing different models for your data layer and UI layer, GitHub Copilot can assist in creating mapping functions. For instance, let's consider the scenario where you have an ArticleApiModel retrieved from an API, which contains both article information and additional data and metadata that are not intended for display in the UI. In such cases, you can create another Article model specifically tailored for the UI, including only the necessary properties.

GitHub Copilot can generate a mapping function to convert ArticleApiModel to Article in this scenario. For example, if you wish to present the formatted date in the UI, you can provide additional context such as the "dateDisplayFormat" variable, and GitHub Copilot will utilize this information to convert the date accordingly.

test

2. Autofill repetitive code

GitHub Copilot excels at handling repetitive code, making it convenient to reuse previously written code in other files once repetitive patterns have been established. Allow me to elaborate on what I mean by repetitive patterns.

To illustrate, let's consider an example within an Android project involving two screens. However, similar outcomes can be achieved in projects across various platforms. In Android, screens are typically created using Fragments, which act as UI controllers responsible for displaying the UI layout defined in an XML file. There is certain boilerplate code required to bind the UI layout with the Fragment. Initially, GitHub Copilot may not provide suggestions to fill this boilerplate code since no pattern has been established yet, requiring manual input. 

Below is an example of what the code for a Fragment, such as LoginFragment with a layout named FragmentLoginBinding, might look like

class LoginFragment: Fragment() {

   private var _binding: FragmentLoginBinding? = null
   private val binding get() = _binding!!

   override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
       _binding = FragmentLoginBinding.inflate(inflater, container, false)
       return binding.root
   }

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
       super.onViewCreated(view, savedInstanceState)
       setupListeners()
   }

   private fun setupListeners() {
       binding.loginButton.setOnClickListener {
           // TODO: Implement login
       }
   }

   override fun onDestroyView() {
       super.onDestroyView()
       _binding = null
   }
}

When you create another Fragment for the Register screen, GitHub Copilot will suggest a similar code for the RegisterFragment. This feature is akin to copy-pasting, but with GitHub Copilot the advantage is that you don’t need to manually refactor the layout name and view IDs, as GitHub Copilot will handle it for you.

test

This example is just one instance of the capability of handling repetitive patterns. GitHub Copilot performs effectively in existing projects. However, in new projects, you'll need to initially write some boilerplate code that GitHub Copilot can reference later as context.

3. Learn new programming languages and frameworks

GitHub Copilot is compatible with most programming languages, allowing you to explore and familiarise yourself with new programming languages. Additionally, it can be a helpful resource when learning new frameworks or navigating unfamiliar codebases.

To illustrate, When I began learning Flutter, I had limited knowledge of the Dart programming language. I used GitHub Copilot to learn about the language syntax of creating classes, variables, functions with required/optional parameters, etc.

test

Moreover, when working with an unfamiliar codebase, you can utilise Copilot Labs to understand the piece of code in plain language. 

4. Writing unit tests

You can use GitHub Copilot to write unit tests for your classes. For instance, let's consider a class called EmailValidator, which is responsible for email validation.

class EmailValidator {

   private val emailRegex = Regex("^[A-Za-z](.*)([@]{1})(.{1,})(\\.)(.{1,})")

   fun isValid(email: String?): Boolean {
       return emailRegex.matches(email ?: "")
       }
   }

To create unit tests for this class, you can generate a test class named EmailValidatorTest. GitHub Copilot will provide you with suggestions for various test cases that you can include in this test class.

test

5. Writing test doubles

In certain cases, it becomes necessary to utilize test doubles for your unit/UI tests. Instead of interacting with actual components like a REST API or a local database, you can create a fake implementation that mimics the behavior of the real components.

In the context of Android development, it’s common to create Repository classes that handle interaction with REST API or local database. Another common design pattern involves using interfaces instead of concrete implementations, enabling you to swap the real implementation with a fake one for testing purposes. 

To illustrate this concept, let's consider a Contact model and a ContactRepository interface. It's important to note that we are not discussing the real implementation in this scenario, as the real implementation would typically involve interacting with either a contacts REST API or a database.

GitHub Copilot can provide suggestions for creating a local fake implementation using an in-memory mutable list. This fake implementation can be utilized in your tests.

test

6. Test-driven development (TDD)

GitHub Copilot can indeed be a valuable asset in test-driven development (TDD) by suggesting the code necessary to make all the tests pass. Let's consider an example where you have the following unit tests for a DateHelper class, which aims to convert dates into a displayable format. 

class DateHelperTest {

   private val dateHelper = DateHelper()

   @Test
   fun getDisplayDateTest() {
       val date = "2021-08-01T00:00:00.000Z"
       val displayDate = dateHelper.getDisplayDate(date)
       assert(displayDate == "Aug 1, 2021")
   }

   @Test
   fun getDisplayDateNullTest() {
       val date = null
       val displayDate = dateHelper.getDisplayDate(date)
       assert(displayDate == "")
   }

   @Test
   fun getDisplayDateEmptyTest() {
       val date = ""
       val displayDate = dateHelper.getDisplayDate(date)
       assert(displayDate == "")
   }

   @Test
   fun getDisplayDateInvalidTest() {
       val date = "2021-08-01"
       val displayDate = dateHelper.getDisplayDate(date)
       assert(displayDate == "")
   }

   @Test
   fun getApiDateTest() {
       val date = "Aug 1, 2021"
       val apiDate = dateHelper.fromDisplayDateToApiDate(date)
       assert(apiDate == "2021-08-01T00:00:00.000Z")
   }
}

It's important to note that these tests are written prior to creating the DateHelper class, as per the TDD approach. GitHub Copilot can assist you in generating the required code for two utility functions that will fulfil the expectations of the existing tests.

test

7. Writing commands

You can use GitHub Copilot for finding the command you’re looking for. You can use code comments to find the command. Also as you begin typing the initial part of the command, GitHub Copilot analyzes the context and generates relevant suggestions, and completes the command for you. For instance, This feature proves particularly helpful when you’re learning or writing Git commands.

test

Furthermore, GitHub Copilot's capabilities extend beyond Git commands. It can also offer code suggestions for system commands or any other commands based on the context you provide.

test

8. Writing regular expressions

Working with regular expressions can be challenging and confusing at times. However, GitHub Copilot can help in simplifying the process of writing regular expressions. it is crucial to adjust and test the generated regular expressions according to your specific requirements and the programming language you are utilising.

test

9. Writing SQL queries

GitHub Copilot understands the syntax and context of SQL code. These suggested completions encompass a wide array of SQL elements, such as keywords, table names, column names, functions, and more.

test

10. Internationalisation

GitHub Copilot can be utilized for language translation tasks. For instance, consider a scenario where you have a strings.xml that contains text definitions for UI elements in an Android application. 

<resources>
   <string name="app_name">Github Copilot Sample</string>
   <string name="hint_email">Email</string>
   <string name="hint_password">Password</string>
   <string name="button_login">Login</string>
   <string name="label_login">Login</string>
   <string name="button_register">Register</string>
   <string name="label_register">Register</string>
   <string name="button_add_contact">Add Contact</string>
   <string name="hint_first_name">First Name</string>
   <string name="hint_last_name">Last Name</string>
   <string name="hint_job">Job</string>
   <string name="label_contact_list">Contacts</string>
   <string name="add_contact">Add Contact</string>
   <string name="action_logout">Logout</string>
   <string name="label_add_edit_contact">Add/Edit Contact</string>
</resources>

To add support for the French language, all you need to do is just provide initial context by adding app_name entry with corresponding French text. GitHub Copilot will then generate the remaining values with translated texts for the French language. Similarly, you can follow the same approach to add translations for other languages by providing appropriate initial context.

test

Furthermore, GitHub Copilot has the capability to generate suggestions from code comments written in non-English languages.

11. Generating sample data

GitHub Copilot can assist in generating sample data for diverse use cases. For instance, suppose you require sample data to be displayed on the UI during the initial stages of the project. In such scenarios, GitHub Copilot can help in generating this sample data efficiently. You can do the same thing in JSON files as well.

test

However, it may suggest repetitive values for subsequent items. To address this, you can add comments to indicate the need for unique values. 

12. Ask Questions

If you have a specific question that you'd like to address to GitHub Copilot, you can make your query more explicit by utilizing code comments. By adding a comment preceding your question, such as "// Q: How can I iterate over an array in Java?", you provide clear context to GitHub Copilot regarding the nature of your question.

test

Tips

  • Structure your code into small functions and use meaningful names for classes, variables, functions, and parameters. Additionally, write informative comments.

  • GitHub Copilot doesn’t generate perfect code. You need to review and edit the generated code to ensure the code is of good quality for your specific requirements.

  • Instead of relying on GitHub Copilot to write entire functions, start by writing functions yourself and utilize Copilot for autocompletion assistance.

  • Open relevant files to provide context to GitHub Copilot, enabling it to generate more accurate suggestions by contextualizing code from other opened tabs.

  • Autocomplete suggestions are most accurate in specific contexts. For example, when working on login input validation, if you define variables for email or password regex in the code, GitHub Copilot will incorporate regex validation into the same function, rather than simple validation.

  • Always use GitHub Copilot with good testing and code review practices.

Limitations and Downsides

  • Auto-complete suggestions in GitHub Copilot are currently limited to the current files and opened tabs. The GitHub Team is actively working on expanding this functionality to cover the entire codebase. Learn more about this ongoing development here.

  • GitHub Copilot may suggest code that is unrelated to the code you're editing, requiring your assistance. Suggested code may not compile correctly or may rely on outdated or deprecated libraries. 

  • Ensure to thoroughly test your code as Copilot's suggestions may include unoptimized, inefficient, or potentially insecure code.

  • GitHub Copilot can be a source of distraction during the coding process and it can lead to bad coding habits among new programmers. 

  • Developers become too dependent on Copilot, which can impede their personal growth and development. Additionally, There are valid concerns regarding code copyright and proper attribution.

What’s next for GitHub Copilot

The GitHub team is actively working on "GitHub Copilot X" a next-generation toolset that aims to expand the scope of AI beyond the IDE. This advancement will bring AI capabilities to various components of the platform, including chat, CLI, documentation, and pull requests. 

For more detailed information, please refer to "GitHub Copilot X"

Conclusion

GitHub Copilot is an impressive AI-powered tool that enhances coding experience and productivity. Embrace it as a companion, while also leveraging your own judgment and expertise. This tool provides an exciting glimpse into the potential of AI in coding, enabling you to become a more efficient and proficient developer.

AWS Certified Team

Tech Holding Team is a AWS Certified & validates cloud expertise to help professionals highlight in-demand skills and organizations build effective, innovative teams for cloud initiatives using AWS.

By using this site, you agree to thePrivacy Policy.