Github Copilot tutorial for Java Developers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up friends dan vega here back with another tutorial and today we're talking about github copilot for java developers so github copilot was released back in june but it was a technical preview and it had very limited language support at the time and very limited id support it was only for visual studio code so what it does it really helps to suggest some code whether it's one line or entire functions so as you start to type out a method signature it will start to make suggestions to you you can even get a little bit more complex and write out comments to try and describe what method you're trying to create and it will make suggestions to you there as well so what we're going to do today we'll take a look at the copilot website we'll talk about what it is and what it can do and then we'll dive into intellij because that's the reason we're here today so again at github universe which was recently 2021 they announced support for intellij because it was just for uh visual studio code to begin with so we're here today because we can do this in intellij so we're going to fire up intellij we'll create some basic java examples and then we'll take a look at a spring boot example too of github copilot in action writing some code for us so i think this is gonna be a fun one i hope you're excited about it and what are we waiting for let's get into it [Music] all right so here we are on copilot.github.com and this is your ai pair programmer so the big thing here is this is still in technical preview they're still collecting data trying to improve the product but with github copilot you get suggestions for whole lines or entire functions right inside your editor so you can sign up here to get on the preview there's some examples here we'll get into these examples we're going to use java today obviously but we'll get into kind of some examples and different ways that you can ask github co-pilot to kind of kick off some suggestions the reason we're here today though is github co-pilot is now available as an extension for neo-vim jet brains and visual studio code so originally when this came out in june i was just available in visual studio code now it's available in jetbrains so we are able to jump into intellij today and take a look at some examples in both java and spring boot and that's exactly what we're going to do so i'll give you a link to this below go ahead and search through this site there's a bunch more information on what it can do what languages it supports etc so with that let's jump into intellij all right so here we are in intellij and the first thing i want to do is talk about the plugin so you're going to need a plugin for this so if you head over to plugins whether you're here on the home screen or you're in intellij you can go into plugins you're going to want to search the marketplace for github copilot and there was actually already an update for this today as i'm recording this so they're already continuing to listen to feedback and approve this again there's a link here that you can learn more that's the link that we just looked at and just some more information so there's a usage to get started with github co-pilot visit the getting started guide so there's some good information here and in the documentation for the repo but once this is installed you are ready to go so we're going to create a new project and what i'm going to do is i'm going to create a spring boot project today now you can just create a java project if you want but i'm going to show off some both java and spring support for what we're doing here with github copilot so the first thing that i'm going to do is i'm gonna go into youtube tutorials so that i can go ahead and put this on uh github uh so if we jump down here on to dev youtube and so what i want to do is i'll put this in let's just do this in java and we'll call this github co-pilot and that should be good okay so now we are going to use javan java maven and let's go ahead and dab dan vega we'll use java 17 you can use whatever version of java you want and we're just going to create a simple web project here not doing anything crazy okay so the first thing that i'm going to do is actually just create a simple test class and um what we're going to do is just go into this class and right away we should get some suggestion here so it's thinking hey you just created a base class you may want a public static void main and inside of that main method you probably want to do something like print out hello world so that's kind of the first suggestion that we're going to get there so i'm going to save that and actually just close that out so the next thing i'm going to do is go ahead and i'm going to create a new package we'll call this the classic utils package where we may have some different utility classes in here uh this will give me a good place to kind of just start so i'm going to start with a matthew tills so let's say matthew tills now this is going to be nothing interesting but the first way well you can see that we're already starting to get some suggestions here uh public static double round so if you want to round something but the first thing we that you the second way that we can get a suggestion here is we could start to type out some method signatures so i'm going to say in sum and as you can see here it's starting to suggest that we can use the arguments a and b and they're both integers because we're returning an inch right so if i go ahead and auto complete that and i auto completed that in the previous one using the tab key so i tabbed out now i auto completed that and it's saying hey if you want to if you want to get the sum from two arguments a and b the best way to do that is to return a plus b so that sounds good um also i'm going to go ahead and start typing out subtract and you can see that hey we're starting to see a pattern here it knows what we're trying to do there's multiply so i'm going to tab through that and let's try divide and that looks good so it took some context there from the method signatures or the method names in that instance and i figured out what we wanted to do so that's pretty cool so the next one that i'm going to do is we'll do some stringy tails everybody has a class like this somewhere in their code base and what i'm going to do is let's say public string reverse so i want to reverse a string and so it's going to accept a an argument of string it's going to create a new instance of the string builder and it's going to loop through those so it's going to take the length so it's going to actually work backwards and and build that new string so this is one way to do it now i'm going to try and hold down the option key and bracket and you'll see that it gives us another way to do it now this is a little bug that i think that i'm getting when i do that i get these quotes i'm not sure why but this is another way that you can do it so there's one or more suggestions and it's going to give those to you so let's just back up here and one thing i want to do is you can also hit option enter and open up github copilot and now you're going to see all of these suggestions here and i happen to like this one this is going to this is the more concise one you can check for null again you're kind of iterating over those um the one that we saw before we're kind of looping over those backwards so there's all these solutions and i'm just going to go ahead and accept solution one so great way to look at some different options there and then maybe we want to public string upper case yes we're going to take a string and we're just going to call to uppercase on it so there's some pretty cool things let's keep diving a little bit further so i'm going to create a new java class called calendar utils so the reason i'm doing this one is because you know some people may not be familiar with a lot of the new um date apis and so this may be a way to kind of get some information about like what is the best way to do this now i will say this isn't the suggestions aren't always the best way to do something or the most efficient way what they're doing is really crawling github or packing you know different repositories and looking for solutions that kind of match that so we're using ai and machine learning to kind of build up this predictive model right so let's start with i want to be able to calculate days between dates right so um i don't want a string i want actually a local date so i'm going to say local date and now there's a start date let's go ahead and import that so now it's saying okay you want local dates instead of string so i'll have a start date and an end date so how would i do that so you can use the start date dot until end date dot two days so that was able to um fix that for us well although uh start date dot until let's go ahead and see what actually want the chrono unit i don't know why that one was coming up hmm so let's say that what i wanted to do was int i want chrono unit dot days okay so there's what we're looking for so between so it's weird that in preparation for this that was the one i got suggested and now i'm getting a different suggestion so again we're in technical previews still kind of running through this trying to understand you know how useful this is so what if i wanted to find what if i wanted to get the day from a date and i wasn't quite sure how to do that so let's go ahead and do this and it says return date dot get day of month that's exactly what i'm looking for great what if i wanted to get a year from a date uh how would i do that return date.getyear that looks good so just some more examples there and one more i want to look at here is the let's go ahead and create a java class called actually i'm going to create a service this is more of a service let's go ahead and create a new package called service and in here i'm going to go ahead and create a whiteboard oops java we're going to create a whiteboard service and in here i want to do a couple things so if you are looking at the repo for this i put a link into um let's actually pop back over to the browser so this was i found some interview questions uh maybe some whiteboard questions you may come across um so if you want to go through here these are some some things you may have to like build out so i wanted to do a whiteboard service that kind of helped us solve some of those problems so the one way that we've been looking at um kind of asking copilot for some suggestion is by giving a name and kind of giving some arguments to a method uh sort of the signature of a method right so now we're going to do instead of doing that we're going to kind of explain to copilot what we're trying to do so i'm going to do that by using a comment so i'm going to simply say that this method i want to try and figure out if it's a palindrome so if it's a palindrome it's going to go ahead and do some suggestions here for us so again public static bullying we actually don't need it to be a static but we'll fix that so i'm going to go into copilot and look at the different suggestions so there are some suggestions here um you know it doesn't matter which one we use for now i'm going to go ahead and accept this one and now that works uh now but now does it work right so we'll take a look at that in a second so another one on my db is just a little bit more complex we'll say method will remove any given sub string from a string so that's a little bit more complex and co-pilot is able to look at that and go okay you probably want a method called a remove substring that will take in a string as an argument and a sub as the second argument and then it will look for that and try to remove that and so i'll go ahead and save that um and finally if you wanted to do something another one of those examples were count the number of times a substring of occurs in a string and there we go another example so that's pretty cool um but now i think one of the things we're going to ask ourselves is what um you know something else i i pointed out i don't think i'm going to get into it now but if you are on my newsletter so and yeah so the reason i'm talking about this in my latest newsletter i talked a lot about github co-pilot for intellij and the reason i did that is because uh github universe just happened they had a bunch of things kind of come out so one of the things i talked about here was github copilot we're doing a lot of the things that i i talked about in here but you know does that does that work so i actually went in and created a test for that is palindrome and we were able to see it kind of work and so let's go ahead and do that because it actually helps with the test too so let's generate a test so i'm going to say let's generate we want to generate a test i just want to test this one out real quick and if i go in here you'll see that it's going to help us hey let's go ahead and create a instance or we don't need an instance of whiteboard service because that was actually a static method so is this a palindrome and if you're not familiar with what a palindrome is it means that the characters are the same forwards and backwards so as you can see r-a-c-e-r-a-c-e-c-a-r so this should pass so if we go ahead and run this it passes so not only is it writing code for us but it's writing effective code code that works right so that's pretty cool um so i think the next thing i want to do is let's go ahead and create a new we'll get into some spring boot specific stuff so i'm going to create a new model folder i'm going to create a new class called employee now this is where i found that generating this kind of stuff is a little bit easier as you see as we go into here it goes okay well you probably want a string right yeah that sounds good you probably want an email i also want an id so it's private string or let's do an int actually so this is my model now i would expect it to like really help me kind of generate all the different constructors and getters and setters but like i said i think to be honest it's just easier to generate these from the ide now this is also a really good place where a record class would come in so you know i've been using records a lot more lately and you could see this did help with the two strings so we're going to go ahead and save that so again if you want to cut down on this boiler plate and you're running a newer version of java take a take a look at records that will kind of cut all this down okay so now that we have a employee we probably want to create an employee service so let's say employee service and at the top of employee service i'm going to create a list of employees we'll call them employees and actually let's all right um and then we'll say new arraylist and i'm just gonna initialize that so the next thing i need is a instructor so it's kind of suggesting suggesting some stuff here but i'm just going to go ahead and create a employee service and we're going to create we're just going to add a new employee so it's not going to help with everything but it will do some things for us so that's the first thing so now in an in a service where we're kind of working we're working with an in-memory representation of some data here the first thing i might want to do is get all my employees and look at that public list of employee get all employees return employees that is right but for whatever reason i did not spell that right employees okay so the next thing we might want to do is oh get an employee and this git employee is going to take an id as an argument it's going to turn the employees list into a stream and then use the filter function which takes a lambda expression and it's going to say hey is that id equal to the id that was passed in it's going to find that first one because we just want one or it's going to return null so that actually looks like a method i want now i want to add an employee look at that now i want to update employee look at that now i want to delete an employee look at that so it was able to kind of derive that from the things that we were doing in that class so that was really cool uh again i don't know how useful this is going to be in my day to day i haven't really been using this in day to day stuff yet but uh it's pretty cool to see this stuff being generated for me all right so i think the last thing that we're gonna do here is create a controller and test this out so let's create a new package we will call this the employee oh sorry we are in a package let's create a package create a new class called employee controller this is going to be a rest controller with a request mapping of let's see if it helps us out it did in one of my tests but it doesn't seem to be doing so now it actually suggested employee and i want to do employees so the first thing we're going to need is our employee service so i'm going to say private final employee service employee service and we'll add a constructor parameter for that that is stuff that i can auto generate so the first thing that it's suggesting here is a request mapping of slash hello uh string hello employee service doc employee that doesn't really make sense let's go ahead and jump into co-pilot and see if there's any other suggestion so here's some we have a default request mapping here to return just a string we have a request mapping so this is basically a git mapping and we're going to return employee service dot employees this is actually what i'm looking for i want to get all employees um actually is that employees no i think we have get employees and actually i didn't write that so let's jump over there so we have a get all employees so that is right um no this is right so employee service dot get no that isn't right this one's right okay so let's go ahead and accept this uh that should work um what is it complaining about oh so we do have a problem here let's go ahead and mark this as a service so that spring knows about it and do we not have get all employees again when you don't write your method this is uh okay so this is returning a list of employees and this is returned in a string so close enough now let's actually turn this upside down so those were the suggestions let's try get all employees so slash all so this returns a list of this looks like more what we want so that worked out well so sometimes you know you can derive it from a comment you can derive it from a metric method signature um again this is all kind of new to me so i'm still just kind of playing around with this uh so what if we wanted to get a specific employee get employee by id um so slash id so this is gonna get us close but i'm gonna change this so what i would normally do is a get mapping there we go that's what i want and then i want this to be a path variable um okay so there's no get ploy by d there's a get employee so you can see it's kind of getting there not doing the greatest job but let's go ahead and start this application up and i'm going to go ahead and run this and all i want to do is test this out so what i'm going to do is run a quick test here in [Music] so we're just going to get all employees let's go ahead and run that um that probably isn't what we want okay um 404 not found slash employees i'm sure i just did something wrong here slash employees this is a request mapping um please so hmm [Music] all right there we go there's our one employee and then if we wanted to we can say slash one and we should just get that single employee and we do so i think that's really where i'm going to end this again this is a technical preview copilot to me is a really cool project you can also use it you know in the repo that you pull down i'll probably have some stuff in the resources folder you can use it for other languages like javascript so if you're working on a front end and a back end you'll get some kind of code assistance there as well but really it's a this is something new um just the beginning you know if this is just the alpha of it and and it's not even released to world you know this is beyond call um just the things that i can start to do so again i haven't been able to use this in my day-to-day workflow yet this is really me just kind of checking out this project so i'll try to incorporate this more into some day-to-day workflows and report back to you and let you know really um you know what it's doing for me if it's helping out in any places and and we'll see where it goes but i thought this is pretty cool and i hope you found this info uh useful if you did please leave me a comment below hit that subscribe button thumbs up whatever you can do to tell me that you enjoyed this content i'd be very very grateful so with that i think we'll call this a day and as always friends happy [Music] we coding
Info
Channel: Dan Vega
Views: 32,792
Rating: undefined out of 5
Keywords: github copilot, github copilot examples, github copilot intellij, github copilot java, github copilot java intellij, github copilot review, github copilot tutorial, how to use github copilot, java
Id: 97C3fQqzj-I
Channel Id: undefined
Length: 27min 27sec (1647 seconds)
Published: Thu Nov 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.