DEV Community

Cover image for Use Copilot to Write and Translate a Binary Search Algorithm
Rizèl Scarlett for GitHub

Posted on • Updated on

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

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.

Top comments (25)

Collapse
 
adamity profile image
Adamity

Just sharing, I have this rule whenever learn new framework/language I will not use GitHub Copilot, I will do first, try and error hoping the thing I learn stick inside my head, if I can't do it by myself, then I use GitHub Copilot

But if I do my fulltime job, I'll fully utilized GitHub Copilot.

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

That’s a really good method!

Collapse
 
blankscreenexe profile image
Muhammad Hammad Hassan

Yeah, I also let my mistakes burn in my head before I could use any auto complete feature.

With that said, co pilot is too close to being a code buddy so I like that.

Collapse
 
rishabh570 profile image
Rishabh Rawat

I really liked the part where you can generate the explaination for the code.

I would not fully rely on Copilot when I'm learning something brand new, but the explaination aspect is really interesting. Nice article!

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

I agree! I don’t mean to solely rely on it, but just having it for additional context

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Thank you btw!

Collapse
 
dhravya profile image
Dhravya

Damnnn github copilot is soo cool!

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

yes, it is!!!

Collapse
 
juliannicholls profile image
Julian Nicholls

Overall, I like CoPilot, and it has created some incredible code for me.

However, you need to (a) look for code you didn't realise that it had created when you took your eye off the ball for a moment, and (b) look at every line it has created because sometimes its intuition is either a little off or way off.

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

It’s still in beta mode! Definitely give feedback to help us improve. Also I wrote about use cases in this article, and I acknowledge that it’s not all the way there yet: dev.to/github/why-use-github-copil...

Collapse
 
juliannicholls profile image
Julian Nicholls

This wasn't meant as criticism, far from it.

It was merely a warning to always look at the created code, particularly when you didn't realise that it had created some :-)

Thread Thread
 
blackgirlbytes profile image
Rizèl Scarlett • Edited

Oh, I didn't take it as such. I was just saying when mistakes do happen or copilot does something expected..there's a feedback area in copilot where you can provide feedback and help the feature improve, so we could reduce chances of that happening.

Collapse
 
zanothis profile image
zanothis

Copilot Labs looks like it still has some work to do. If I didn't understand how that algorithm worked, that explanation would be almost useless. It seems to only explain the syntax without addressing the semantic meaning of the code.

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett • Edited

It’s still in beta mode, so yes true! We are open to feedback to improve

Collapse
 
evgeniigerasin profile image
Evgenii Gerasin

Juniors will not be needed soon? I thinks - yes

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett • Edited

I think juniors will still be needed, but it will help juniors to code more efficiently. I wrote another blog post about possible use cases for Copilot. You can check it out here: dev.to/github/why-use-github-copil...

Collapse
 
evgeniigerasin profile image
Evgenii Gerasin

I think junior may will be stay junior forever by this tools. Because, its tools stopping our thinks about code

Thread Thread
 
blackgirlbytes profile image
Rizèl Scarlett

I get what you're saying.

However..
Copilot can't write code without knowledge behind it. Copilot is more like stack overflow than someone who is thinking for us. You still have to think about code and understand what you're writing and why to use Copilot. Copilot just provides added context and suggestions. Also, Copilot is not ALWAYS accurate.

Thread Thread
 
evgeniigerasin profile image
Evgenii Gerasin

and junior cant understand correct code or not

Collapse
 
zirkelc profile image
Chris Cook • Edited

I've been using Copilot for a few weeks now and I'm amazed how good it is. Sometimes, even the words and comments generated by Copilot are better than mine.

Collapse
 
evgeniigerasin profile image
Evgenii Gerasin

its true

Collapse
 
alexeagleson profile image
Alex Eagleson

That's a really cool use case. Thanks for the tip.

Collapse
 
naruaika profile image
Naufan Rusyda Faikar

Ha-ha. I've tried it myself, that's sick! Maybe one day we'll no longer need to share knowledge with people who used to be involved in writing code, where the code wasn't commented clearly enough.

Collapse
 
evgeniigerasin profile image
Evgenii Gerasin

What to do with legacy code, if we need change it?

Collapse
 
mdejaz_16 profile image
MD EJAZ

great to know that copilot can make our coding life easy