Github Copilot logo

Let’s learn how to write better Github Copilot prompt so that we always get the desired result

What is Github Copilot?

GitHub Copilot is an AI tool designed specifically for code generation and completion right in your IDE.

It is trained on a large corpus of high-quality code and can provide suggestions and completions based on the code you are writing. It aims to enhance your productivity by automating repetitive coding tasks and providing accurate suggestions tailored to the context.

While there are other AI-driven tools that can be helpful for developers, Copilot is specifically designed to assist with code-related tasks, providing more accurate and contextually relevant suggestions.

It is trained specifically on code and understands programming concepts and patterns, making it more suitable for generating code snippets and completing code blocks.

Using Copilot is really as convenient as possible because it is integrated in your IDE where it has access to your code without having you copy and paste it back and forth.

Firstly, let’s make sure you have it installed.

Copilot Installation

Github Copilot is at the moment support by. the following IDEs and text editors:

Supported editors

If you use one of them — just look for “Github Copilot” extension and install it.

There is a 30-days trial period and a paid membership option ($10/month for individuals). You will need to register with your Github Account and then sign in with the same account in your IDE.


How to write Github Copilot prompt

Prompt by writing

This way of prompting is pretty obvious and natural. you just write the code as you do usually and Copilot analyses your writing, the surrounding code, the file itself and the other files nearby. After the analysis it provides you 1 or more suggestions.

Github copilot prompt by writing

As you can see Copilot guessed what we wanted to write by the function name.

TIP: Try to provide as much precise variables, functions and classes names as you can.

This will result in having:

  1. Much more readable code for humans that you work with.
  2. Let Copilot generate much more accurate suggestions for you.

One more thing to mention is that you can also see all the suggestions that Copilot currently has by hovering the mouse onto the suggestion and switching between them:

Switching between suggestions

Prompting with comments

Another way to prompt Copilot is by comments. And in my opinion this way doing this is much-much more powerful.

To start — you need to write a comment with a prompt similar to what you would write for ChatGPT. Let’s say we want to generate an HTML5 file with the links to tailwind css, main.css and JQuery included.

Let’s start with the following comment

<!--
Generate HTML5 file
it should have a link to tailwind.css CDN in the head
it should have a link to main.css in the head
it should have a link to JQuery CDN in the body
-->

And let’s see what happens next:

Github Copilot prompt to generate HTML file
Copilot generating HTML file

Wow! Isn’t it a magic? 😍

The same can be done with other types of code. Let’s write a TypeScript function that will accept a number n and will return n first Fibonacci numbers.

I’ll start with the following comment prompt:

// Generate a function called getFirstFiboNumbers that takes a number n and returns an array of the first n Fibonacci numbers.
// The Fibonacci sequence is a sequence of numbers where every value is the sum of the previous 2 values.
// The first two values are 0 and 1.

Let’s see the result:

Copilot generating HTML file
Copilot generating JS function

Despite I’m using this tool each day I still cannot believe it works so well 😄

Tips about writing the better comments Github Copilot prompt:

  1. Be as precise as you can.
  2. Give an context of what you goal is.
  3. Provide examples of inputs and outputs that you expect.
  4. If the generated code is not wht you wanted — adjust the comment prompt to avoid the mistake that Copilot made.
  5. Remember that the time spent on writing the detailed comment prompt is returned at least 2x back because writing the code itself is always taking much longer.

Conclusion

  • Github Copilot is a powerful tool to generate the code
  • You can delegate the routine tasks to it while focusing on what needs to be done instead of how
  • You can create Github Copilot prompt by writing and by comments
  • Do not spare time on writing the precise prompts — it will always return you back an amazing result

👋🏻 Thank you for your reading. I hope this article inspired you to improve your code review process. If you implement at least one of the above advices in your development cycle you will notice its pleasant effect immediately.

✅ Click here to follow me so you don’t miss out the new articles!

💬 Your thoughts and ideas are very appreciated in the comments below

One Comment

Leave a Reply

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