📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Знакомство с GitHub Copilot

Pragmatic Programmer26:07

Transcription

[music] In this lesson, we will talk about such a hyped development tool called GitHub Copilot. This tool is developed by GitHub in collaboration with the well-known company OpenAI. That is, it involves the use of artificial intelligence during code development. And at the end, we will draw conclusions about what possibilities it gives us developers and whether artificial intelligence will replace us at work. Before we begin, a small disclaimer. I have been a C# developer for over 15 years, so I will demonstrate all the work in an environment called Microsoft Visual Studio. Or we will also look at Visual Studio Code later. That is, GitHub Copilot can be integrated into popular IDEs, no matter what language you write in. We will consider it on Microsoft products, and in the lesson notes, I will post instructions for setting up work in other IDEs for Python, Java, and other PHP languages. And one more small nuance. GitHub Copilot requires a VPN to work, but at the time of recording this video tutorial, advertising VPN tools in our country is prohibited, so this issue will not be discussed in the lesson. So, to demonstrate GitHub Copilot's work, let's launch Microsoft Visual Studio, a fresh version, and create some console project there as an example. Excellent. Next, we need to set up Copilot, regardless of which IDE you have or what language you write in. In general, the actions will be the same. We need to integrate with our GitHub account. To do this, in the studio, in the upper right corner, we see the GitHub Copilot button. We click on it and select the first option, "Sign in to use copilot." We are asked to log in to GitHub, which we will do now. Here, at the next step, after entering the login and password, we traditionally click the green button, in this case, "Authorize GitHub," and return to Visual Studio. Pants turn into elegant shorts. We've returned to the studio. Let's check. We click on the GitHub Copilot "Get Copilot for free" button again. And some interface already appears. We need to click on the last button here, "Sign up for Copilot free." And if everything goes well, as it is for us now, we will see several buttons. If we click on them, we will get additional information, as well as the working tab "GitHub Copilot Chat." This is the main Copilot tool. It works in all IDEs with which Copilot is integrated. It doesn't matter. Visual Studio, IntelliJ IDEA, PHP, WebStorm. Whatever you have, a similar window may be in different colors, but this is the main tool for working with Copilot. Excellent. What do we have at the moment? A standard "Hello World" console application in C# and a configured Copilot. For reliability, we can run it and make sure it outputs "Hello World." We click the green "Run" button. And after a while, the terminal launches, in which we see the text "Hello World." Everything works. Press any key to exit. Super. Now we want to develop our project. Let's say we want to create a "Person" class with some typical fields: name, surname, and so on. Typing all the code manually is time-consuming. We have a wonderful tool, Copilot, which we will use to speed up our work. So, in the Copilot window, we type "create," "create for us, please, a person class with typical fields." We've written such a prompt. We press Enter. And Copilot starts searching, thinking, to offer us something for this request. And we see some response. Let's expand this chat a bit more, please. Here, they have generated code for the "Person" class with some standard set of fields, a constructor. The "ToString" method is overridden. Now we can do the following with this. We have three buttons at the bottom. Copy this code, paste into a new file, or do a preview. We want to paste this code as is and create a new file in the process. We click the second button. Here, we have a new file, "Person.cs," code in C#. Now we need to save this file to our project. If we return to the "Solution Explorer" tab, we will see that we have the main file "Program.cs" and nothing else. We need to save "Person.cs" here as well. So, we press Ctrl+S, go into the folder with our application, and save the "Person" file here. It automatically appears in the "Solution Explorer" window. Super, created in a few seconds. By the way, I wrote the request to Copilot in English, but Copilot can understand most popular human natural languages. That is, you can also write tasks to it in Russian. So, if I had written: "Create a class person with a typical set of fields." Press Enter, and in general, we will get the same result. Let's see, indeed, perhaps Copilot took a couple of seconds longer because we didn't write in English, but we didn't even notice the time difference. Now we have instructions, explanations, comments all in Russian. Well, in general, we got approximately the same result. That is, you can write tasks to it in Russian as well. Since I don't program in 1C, I'm used to using English everywhere when working with code. Therefore, I will write everything in the Copilot chat in English and explain in Russian. You, in turn, can write in Russian, or in any language you like, in any human language. Now let's look at the "Person" class that Copilot generated for us. Here at the beginning, we see some set of fields. Then a constructor. Let's assume we didn't have a constructor. They didn't give it to us in advance. The class was in this form. Let's save it. Yes. And we see that we will have to manually create a constructor. Note that Copilot is already offering us something in the so-called "Ghost mode." Ghost mode is a phantom mode. You can see that some code is generated in gray font. This is precisely the work of Copilot. If I press Tab now, this proposed code will be inserted into this file. I didn't even have to write anything. GitHub Copilot understood my thoughts, practically read what I wanted to create a constructor. But if it hadn't done so now, I could have explicitly told it, for example, in this form: "Create constructor" Enter. And again, Copilot offers me standard code for a constructor of a similar class. I can press the Tab key, and this code will be inserted here. Ctrl+S to save. Now let's return to our main class "Program.cs," from which our "Hello World" greeting is launched. We will remove this "Hello World" line, we don't need it, and let's say we want to create an instance of the "Person" class here and display it on the screen. Excellent. We will now write such a task for Copilot. If you noticed, we have different ways of interacting with Copilot. The first is to write a task for it in the Copilot chat window. The second way is the so-called "Ghost mode." That is, we type two slashes and what we want to get. So, we type "//" and the task for Copilot. For example, "create instance of person class." Like this. That is, create an instance of the "Person" class. Enter. And Copilot. Look, in "Ghost mode," in this phantom mode, it's already offering us some option in gray font. I press the Tab key, and this line of code, where the instance of the "Person" class is created, appears for me. Super. Next, Copilot is already offering me the next line in "Ghost mode." "Print person details," that is, display the details of this instance on the screen. I can type Tab, Enter. The next line it offers us is "Console.WriteLine," that is, to output to the console in the terminal. I haven't typed a single line or word. Copilot is offering me this code because it's the most common scenario. When we create an instance of a class, we probably want to display it on the screen. I press Tab, Enter. Everything, the code is already here. It's even offering me the next line. I can follow these recommendations, which are displayed in gray. I can press Escape, and then they will disappear. Please, let's run our project again to make sure that instead of "Hello World," it now displays our person, information about them: name, surname, and some other information. In Visual Studio, to run a console project, we press Ctrl+F5. After the project is built, yes, we see information that our person is displayed on the screen, information about them, all their fields. Press any key to exit again. That is, we can work in the Copilot chat, write tasks for it, or we can work in "Ghost Mode." But that's not all. There is another option. We can write tasks for Copilot in the pop-up window of Visual Studio. There is a keyboard shortcut Alt+/ which also brings up a textbox where we can write something for Copilot. By the way, this video is just a small part of my training program on working with Git and GitHub. More than 3,000 students from all over the world have already enrolled in the course. It has received a hundred and fifty reviews from real users on the website. You can check for yourself what real people are writing about it. The course also includes over 6 hours of video materials, over 100 tests for checking and reinforcing knowledge, a friendly community, and many other interesting and useful things. And if you look at the course program, you will see that there are even gifts at the end. There's even a chance to get another large course for free, a life hack. All links are in the description or in the pinned comment. Well, where else? And of course, subscribe to the Pragmatic Programmer channel. The amount of IT content will grow. So, we want to create a new method that will return us a list of random people. To do this, we press Alt+/ in Visual Studio and write what we want from Copilot. "Create, returning a list of person instances." Enter. Let's see what it offers us. Aha, there is some compact method "getSamplePersons." It suits us in general. We press the "Accept" button. Super. Here is the method that will return us, in this case, three random people. And let's say we want not three, but ten. Let's try to make it change. We've highlighted three people. We press Alt+/ again and say, "return 10 instances of the Person class." Enter. Let's see and rejoice that now they are offering us much more. In fact, seven more new ones. Excellent, we like this. We press "Accept." And here is our beautiful method. Ctrl+Shift+S to save all changes in all files. Copilot can also do refactoring. For example, we look at the constructor and see that it takes name, surname, age, address. And we want it not to take the age field. That is, to remove this parameter and automatically remove all assignments inside, so that we don't get confused and break anything, we delegate this task to Copilot. We highlight our constructor, all the code with it, press Alt+/ and say, "remove age from parameters," that is, remove the assignment of this field here and passing it to the constructor. Enter. Let's see what Copilot will offer us. Aha, we see that the parameter and its assignment are being removed. We like this. Accept. Yes, there is no mention of the "Age" field in this constructor anymore. Ctrl+S. We fix the changes in the file. What else can Copilot do? It can generate documentation. Writing documentation for programmers is a routine task. Few people like to do it, but today this task can be delegated to Copilot. It will handle it in seconds. We highlight the "Person" class, press Alt+/ and say, "detailed docs for all class members." Create detailed documentation for all class members. Enter. What will Copilot offer us? Pants turn into elegant shorts. Look, a lot of comments for each field, for each method. This, in general, is the source code for creating documentation. Super. We press the "Accept" button. We accept these changes. Everything, our class is now very well, very qualitatively documented. I will press Ctrl+M, O keyboard shortcut so that our code looks more compact. Note that they have generated a few extra closing braces. That is, Copilot, of course, is not perfect. It can produce errors, but there is nothing wrong with that, because we have our brains, we have our programming skills, which will tell us that this code is invalid, that these two closing curly braces are extra. Let's remove them. Press F6 to compile the project, to check that everything is fine now, or in the menu "Build" -> "Build Solution," Ctrl+Shift+B, in this case, the keyboard shortcut to build the entire project. We see that an error has appeared somewhere. Let's click on it and see that in our main "Main" method of the "Program" class, where our program is launched, an instance of the "Person" class is created, to which all parameters are passed, including age. But we removed age from the constructor, right? This parameter with the number 30 should not be passed here. Let's say we don't immediately understand what our error is. We want Copilot to find the error itself and offer us to fix it. We highlight this line of code. Press Alt+/ and here again, type "/" and select the "fix" command. "fix" Enter. Enter again. Copilot will think, and we hope it will offer us a solution. Yes, indeed, it is precisely offering to remove the parameter 30. We accept these changes. There are no more errors. Let's try to build the project again. "Build Solution." Super. No more errors. We can run it. Ctrl+F5. Here it still displays the person. Now let's say we want to display the list of those ten random people that the "get10SamplePersons" method in the "Person" class returns. Okay. So that we don't make mistakes in writing the code, we delegate this task to Copilot. Let's comment out this code and give Copilot a new task. You can write in any mode: in the chat, use "Ghost Mode," or use Alt+/ launch, as you like. For example, we type "//" and the task. We write "call method Person.get10SamplePersons and output the result." Call the method that returns 10 people and output the result. Enter. What will it offer us? Let's check. We press Tab to accept this suggestion. Enter. Now in "Ghost mode," in this mode, it suggests iterating through these people, these instances of the "Person" class, this list of "Persons" in a "for" loop. We haven't typed any of this. Copilot is offering all of this. I just press Tab to accept. Super. Let's check Ctrl+F5. Let's run our project. And we see that it now displays 10 people. Everything is correct. Copilot can also work with third-party libraries. Let's assume we want to serialize these people into JSON format and save them to a file. Let's try to tell Copilot this. For example, Alt+/ and we say, "Serialize people to JSON and save to file." It's better not to make any typos. We want it to serialize the "people" variable into a JSON object and save it to a file. Enter. What will it offer us? We see some piece of code. Let's accept it. Accept. Errors appeared because Copilot suggests using the "JsonSerializer" library, and it's not connected yet. Let's click on it. In the studio, you can press Alt+Enter, for example, and choose a solution. Most likely, we will need to add a namespace to this file, and the error will disappear immediately. Let's run our project again. Ctrl+F5 and we see information that our 10 people have been serialized and saved to the file "persons.json." Let's try to find this file and open it, look at its content. If we go to the "Solution Explorer" tab in the studio, right-click on our project, we can select "Open Folder in File Explorer." This is a faster way to open the folder with our project. Now we need to go into the "bin" -> "Debug" -> "net8.0" folder. Here are all the necessary files and the executable file with our application, and everything else. Libraries, dependencies, and everything we need. And here we also see the "persons.json" file. Let's open it. You can open it in any application that can work with text files. Let's try it directly in the studio. And what do we see? Our 10 people, saved in JSON format. Exactly what we told Copilot. What else can Copilot do? It can generate tests. Let's try. Go to the "Person" class, next to the class definition. Type Alt+/ and write "generate tests for all class methods." Generate tests for all methods of this class. Enter. So, we see that Copilot is quite smart. Firstly, it offers us to save all this in a separate new file "Person.Tests.cs." Some large piece of code has already been generated. Let's accept it. Press "Accept." Ctrl+S. Save. Save the file as well. Save. Uh-huh. Dependencies may need to be added because tests are generated in third-party libraries. Let's try to compile this code. Menu "Build" -> "Build Solution." We see errors in this new file. Of course, they are related to the fact that the XUnit library is not connected. We understand that we need to add the XUnit library to our project. In general, this can be done with a specific command in the Package Manager Console, but let's assume we don't remember this command. Perhaps Copilot can remind us of it. Let's ask it. Let's go to the Copilot window and write: "How to add XUnit Library to Project via Package Manager Console." How to add the XUnit library for testing to our project using the Package Manager Console. Let's ask Copilot. It will probably suggest some command. Yes, here is the command "Install-Package XUnit." And it also says that first you need to open the Package Manager Console panel. Let's click on it. The search mechanism opens. Here we select "Package Manager Console." Here it is initializing, right? And now we paste this command. "Install-Package XUnit." Ctrl+C, Ctrl+V. We copied, pasted. Let the machine do everything for us. So. We are also offered to install a second auxiliary library. Let's use it too. Copy, paste. Enter. Something happened. Let's try to compile the code again. Menu "Build" -> "Build Solution." Now there are no errors. This means Copilot offered us the correct solution. Excellent. Tests have been generated for us. What else can Copilot do? It can generate code for various tasks. But in fact, it not only generates code, it helps us understand the written code. Here's a task. We have a "Person" class. Let's assume we don't quite understand what's going on there. We can tell the Copilot chat the command "//" and select the "explain" option. "Explain," that it even offers us "the code in Person.cs." Explain to us, please, the code in the "Person.cs" class. Enter. And please, they are describing literally every line. Since we wrote this task for Copilot in English, they are explaining it in English. But we can ask it to do it in Russian. Let's go back to the text, press the up arrow key to bring back the previous command for Copilot, and add it here in Russian. Like this. Enter. And what do we see in our beloved native Russian language? All explanations literally for every line of the "Person" class. Where the namespace is, where the class definition is, what properties, constructor, methods, and so on. Super, we've skimmed through it. If, perhaps, this was someone else's code, written sometime before us, we quickly understood it. This was working with GitHub Copilot in the Microsoft Visual Studio environment. Relevant for those who write in .NET languages. Typically, this is C#. But if you program in another language, perhaps you use an IDE called Visual Studio Code. Let's discuss how to set up Copilot in it. Let's launch VS Code. And here, first, you need to install the extension for Copilot. Go to the extensions tab and type "GitHub Copilot." We need to install the top two extensions. Select them, click the "Install" button, and do the same for the second one. If this didn't happen automatically, "GitHub Copilot" and "GitHub Copilot Chat." Let's install these two extensions, and you will also need to authorize through the GitHub website. After successful installation, a welcome window appears with some instructions. And now we see that the "Ask Copilot" tab has appeared automatically. This means we have already logged in. If it wasn't here, there would be a button similar to "Sign up via GitHub" in Visual Studio. If this "Copilot Chat" panel doesn't appear automatically on the right, for example, then it's okay, it can be invoked using specific hotkeys. Let's open the same console project here. For convenience, let's go back to Visual Studio, go to the "Solution Explorer" panel, select our Solution, click on it with the right mouse button and copy the path to the project "Copy full path." And let's open the project at this path in VS Code. Click the "Open Folder" button. Paste the copied path. Excellent. "Select Folder." And here is our project in general. Let's click the checkmark, indicating that we trust it. Everything is fine. The same files that we just wrote in Visual Studio, everything works. So, if the "GitHub Copilot Chat" window is not displayed in VS Code, we can press the keyboard shortcut Ctrl+Alt+I here, and the window appears. That is, in different IDEs, there are different hotkeys, which is, of course, natural. If we want "inline chat," that is, a textbox directly in the code window, then the key here will be Ctrl+I. And in general, everything is the same. Let's come up with a task for Copilot. Let's say it takes this "Person.cs" code and translates it into Python. Will it cope with such a task? You can write in Copilot chat something like "Translate all in file Person.cs to Python language." The main thing is not to make any typos. Enter. So, apparently, we haven't logged into GitHub yet, so it's offering us to do it. Click the "Sign in" button. It will also redirect us to the GitHub website in the browser. We click the green button, in this case, "Continue," "Authorize," return to VS Code. And now everything should be fine. What do we see? That a fairly large amount of code is being generated. If you are familiar with Python, it should be understandable to you. I don't know Python, so I can only trust Copilot. In VS Code, the interface is slightly different. In particular, the action buttons for Copilot's results are displayed in the upper right corner. In particular, the first one: "Apply to Person," send this code to a new file. The second option is to insert this code where the cursor is currently blinking, or copy it, or there are some other options. Let's choose the first one. Create a file "Person.py" and send the code there. Click. It offers us what to do at the top. "New File Person.py." Select. Everything, super, everything is ready. 48 lines of code, as if we had already written it. It's interesting how long it would take us to write all this manually. It turns out it takes only a minute or so. Save. Ctrl+S or Ctrl+Shift+S. Save all files. Yes, super. We also see an icon next to this file. That means we haven't fully applied these changes yet. At the top, there is a checkmark to apply. Everything. Now the task is completed. With one request to Copilot chat, we asked to translate the code from C# to Python, and this task was solved quite quickly. Well, now let's draw some conclusions about this wonderful tool, GitHub Copilot. Firstly, let's dispel the popular myth that these artificial intelligence tools will soon replace all programmers. Of course not. They work with errors. You cannot trust these tools to make decisions independently, but they perfectly handle the task of accelerating the solution of typical problems, generating large chunks of code, which we review, so to speak, review this code, and accept or modify it as needed. We, of course, have done a relatively brief overview of Copilot. This topic is large, capacious, possibly worthy of a separate course, but nevertheless, it is an important, trendy, popular tool. In our industry, it is always important to keep up with the times, master new tools, follow development trends to stay at the forefront of what is happening in the labor market. And then, for sure, no machines will replace people. [music]