Use Copilot to Write and Translate a Binary Search Algorithm

I learn best from reading, watching, and doing, which means I need various resources to create a robust mental model. When I’ve studied for interviews, colleges exams, and coding bootcamp, I’ll watch videos, complete LeetCode exercises, and write an explanation of the code. Copilot Labs serves as an additional resource giving me more context on why and how specific algorithms work.

I will use Copilot and Copilot Labs to write and understand a binary search algorithm in this blog post.

Prerequisites:

Ensure that you’ve installed the GitHub Copilot extension AND the GitHub Copilot Labs extension. Please note: GitHub Copilot is in technical preview, and thus not all users will be able to access this feature. Please join the waitlist if you’d like to sign up for the technical preview.

Image description

Writing Code with Copilot

Step 1: Open your IDE. For this example, I’m using Visual Studio Code via Codespaces. Type a comment that says, “Write a binary search algorithm.”

Image description

Step 2: Type the keyword “const” on a new line. This should trigger Copilot to provide suggested code to write a binary search algorithm. To accept the suggested code, press the TAB key.

Image description

Image description

Image description

Explaining Code with Copilot Labs

Step 3: Now, we can better understand what the code is accomplishing by converting the code into plain language. Let’s open the Copilot Labs sidebar.

Image description

Step 4: Highlight the code snippet, and on the top left-hand corner, choose “Explain.” Press the “Ask Copilot” button to generate a step-by-step explanation of what’s happening in the code snippet.

Image description

Image description

Translating Code to a Different Programming Language with Copilot Labs

Step 5: We can also convert this code snippet into Python (or any programming language of your choice). Navigate to the Language Translation of the Copilot Labs sidebar and choose the target language. Press the “Ask Copilot” button to view a version of the code snippet in a different language.

Image description

Image description

Follow GitHub and me on DEV for more updates on GitHub’s newest features!

If you have any feedback you want to share with the team at GitHub, use the Discussion board.