From the course: Refactoring with GitHub Copilot

Chatting with Copilot - Github Copilot Tutorial

From the course: Refactoring with GitHub Copilot

Chatting with Copilot

Copilot Chat is a powerful feature for code explanation, generation, and insights. It allows you to harness Copilot in places external to the codebase. The first example I like to do is highlight a section of code and open the chat, which is this button right here, and I'll ask Copilot to write a comment for this function. Write a comment for the highlighted code. This output is probably more verbose than something I would write as a comment, and skimming, it seems completely accurate. I highly encourage you to double-check the work of Copilot everywhere. Also note, there's an option here to copy to your clipboard. Another prompt you can pass to Copilot as a request to explain. So I'll ask it to explain the highlighted code. I want to pause for a moment. This function was generated by Copilot. A moment ago, Copilot created the DocBlock and now it's explaining how this function works. I frequently search the internet for simple things, problems I've solved before, but I don't quite remember. With Copilot, I don't need to leave my IDE. For example, I can ask, what is the Git command to select a single commit from another branch? Now, Google may or may not have answered that for me, but not only does Copilot remind me that it's called cherry-picking, but it also provides the syntax for it. I'm going to use a lot of PHP in this course, so it would be logical to have to create a class in PHP, and pending the PHP version that can be somewhat verbose. So I'll ask Copilot to create a class that looks like a person. Create a PHP 8.0 class that has the following properties: age, integer, name, string, and email string. The output is valid, and it's PHP 8.0, which is what I asked for. But I can convert that to 8.3 with constructor property promotion by telling it, use PHP 8.3 constructor property promotion. Copilot knows I'm talking about the class that it just generated and gives me the updated version. I haven't called this class a person, but that's what Copilot decided it should be. Using that, I can ask it to create a person interface in TypeScript. Create a person. And this should unlock a new line of thinking. How would I rewrite the highlighted code in Python, or in Ruby, in Ada 83, in COBOL? I don't really know those last two, so I can't confirm. But it's fair to say that Copilot knows many languages, even pretty obscure stuff. I did a course about automated testing, so it stands to reason. I could ask Copilot to write a test for the highlighted code. Copilot Chat is powerful. The last part I want to call out are slash commands. I can type slash to see the available slash commands and a short description for each. I'm going to remove a semicolon in this, and I'll run /fix. As expected, Copilot finds the problem. Now, in trickier bugs, Copilot may or may not be able to identify it with the entire file. You might have to hone in on specific areas. The last slash command that I want to call out is optimize. In this case, there's not a lot to optimize, but something that has pretty difficult logic in conditional paths, Copilot can recommend faster ways and more readable code. So Copilot Chat is really useful, and because it's context-aware, it can really help move things forward quickly.

Contents