How to Use ChatGPT as a Powerful Tool for Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there how's it going everybody in this video we're going to be looking at chat gbt and how we can use this tool as programmers now I'm sure a lot of you have already heard of chat GPT at this point but if you haven't it's basically a new artificial intelligence technology that we can interact with almost like we're speaking to another person so it's used for a lot of different things outside of programming but the programming aspect is what we're going to be focusing on in this video so what we're going to be looking at is how we can use this to write code for us how to optimize code how to explain code Snippets convert between different languages how we can use it to get ideas for starting projects and also help us with you know some of the more boring tasks that we don't like to do but need to do like writing unit test and commenting code so there has been a little bit of a debate as to whether programmers should use tools like this since it doesn't always get things right but I think after this video we're going to see why this can be so useful for us to learn and it's only going to get better in the future so in my personal opinion it's going to be important for us to know how to use tools like this going forward you know just like some people say that you know how you Google things can be a certain skill in itself uh knowing how to interact with this new wave of AI tools is also going to become a skill that is going to boost our code and productivity so I do think it's going to be important so with that said let's go ahead and get started so first off let's just see how we can use chat GPT to write some code for us now I currently have a chat GPD open here in my browser and if you've never used this before it's extremely simple you just go to their website and create an account and then you're good to go I'll go ahead and leave a link to their page in the description section below where you can use this now you can use it for free but they also have a paid version that gives a few benefits that you can read about on their page now I currently have the paid version which gives me access to I think more up time and I can also use chat gpt4 which is the latest version but I also tested this tutorial using chat gpd3 and I didn't notice much of a difference in its output um now I'm going to be using chat gpt4 in this video uh simply because this should be available for everyone to use soon so I want to be sure that I'm using the version that most people are going to be using before too long so when speaking to chat gbt we can basically speak like we're speaking to another person there's no specific queries or anything like that involved so for example if I wanted to do something extremely simple like looping between 1 to 10 and printing out those numbers I could simply just come down here and say we'll just simply tell it to please write a python script that Loops from one to ten and prints out each number and so whenever I run this we can see that it takes a while to think here and then prints it out and it says sure here's a simple python script that does exactly that we can see that it gave us our for Loop and our print here and it even explains a couple of details here and I think this is a great tool for learning because uh not only does it give us the code that we can copy just by clicking up here in the top right but it also explains exactly what's going on in case somebody is learning something for the first time so it says why it use the range function and then it also says here note that range 111 actually generates numbers from 1 to 10 not 1 to 11 because the stop value provided to the range function is exclusive so that's pretty cool that we can just tell it exactly what we want in human words and that it understands us to print out the code but like I said this is an extremely simple example we can actually ask ask it to do much more advanced code than this so for example let's say that I wanted to write a script that accepted a password input from a user and then hashes that password using assault now this might be something that some of you have never done before that might take a little bit of research before you're able to actually write it out in code but let's see if we can get chat gbt to write something like this for us so let me type this out here and then I will explain the prompt okay so the prompt that I wrote here I just said write a python script that accepts user input this input will be a password I then want this password to be hashed using a salt then print the hash password to the user so let's run this and see what it gives us okay so let's scroll back up here to the top and see exactly what it gave us for this prompt so we can see it says the python standard Library includes the hashlib module and then we can see that it gave us the script here and attaching this password using several algorithms it is also using a salt like we wanted before and it's generating a salt using the OS dot U random module and then after it hashes that then it prints out that hash password and everything also here is commented pretty well so that we know exactly what's going on and then at the bottom where it gives an explanation It also says in a real application you would also need to store the salt for each user and so on and so forth now one thing that I really love about chat GPT is that it actually remembers previous prompts and requests that we've made so if something is wrong or not how we like it then we can simply ask it to change the code and it's specific way again kind of like we're talking to a co-worker so with the current code that it gave us we can see here that when they entered their password it's just using input here but when we use the input function as the user types it out it actually shows the characters that they're typing to the screen but passwords are usually hidden on the screen so that anyone else who can you know see over their shoulder just can't read the password off the screen so we can just simply ask chat gbt to redo this if it knows how so let me just type out my prompt here and then I'll explain what we're asking it so I'm just saying since the user input is a password can you make it so that when they type in their input it doesn't show the character up the characters that they are typing out to the screen so let's go ahead and run this and it says sure for handling password inputs password python has a built-in module called get pass so if we go down and look at our new code now we can see that it's using this built-in get pass and using that for them to enter their password and that will take care of that user input showing up on the screen so that's one thing that I really like about this kind of a tool is this conversation style where you can go back and forth because if you're Googling something and you come to a roadblock then you're kind of out of luck you just got to keep searching but with this you can give it errors or give it extra input and it'll redo the code and regenerate that response for you so I just think that that's a really good touch and it doesn't put us in these roadblocks that we normally hit okay so now that we've seen a couple of examples of where we can use chat CP to write code from scratch let's look at how we can use it to optimize existing code and I'm going to be using a simple example here just so that we can see exactly what's going on and I'm just going to grab this from some code Snippets here and copy this bring this back over and I'm going to go ahead and paste this in this code is very simple we just have a list of names here and we're doing a for Loop here where We're looping over the names but we also want to access the index which we don't get from a normal for Loop so one way especially coming from another language somebody might want to do this just by saying index equals 0 outside of the for Loop and then looping through or incrementing that by one each time we go through that for Loop to keep track of the index now this code actually does work but you're kind of setting yourself up with possible errors and debugging if the values of the index go out of bounds or anything like that so a better way to write this code would be to use the built-in enumerate function now I know that personally but if someone is learning this for the first time then let's see if chat gbt can pick up on this and optimize this code for them so I have the code pasted here and and I'm just going to put that down one line whoops now what's funny there is I didn't actually mean to submit um that code I was going to put in a prompt that said can you optimize this code for me um but I just put in the code and it says that it kind of knows already that we wanted to optimize it it says the code that you provided Loops over this names list but python provides the built-in function enumerate which makes this task easier and more pythonic and we can see here that at the bottom so it does rewrite that code for us using enumerate and it knows that we want to access the index and the name and then down here in the extra details as usual it says in this code enumerate generates a sequence of tuples where the first element of each stop was the index and the second element is the corresponding item from names and again like I said before this is an extremely simple example just so we can see exactly what's going on but you can paste in much larger and much more complicated section of code to see see how a tool like this will optimize it and one thing I'll mention several times throughout the video is that you shouldn't take everything chat GPT tells you as gospel or as the solid truth I mean definitely be sure to look over the changes that it makes and make sure that your desired functionality is still there and also read over these explanations that it provides to make sure that you agree with the changes that it made again it's just a tool and should be used like a tool we still need to understand the code that it's giving us back but seeing how it will optimize your code may lead you to learning more and discovering new built-in functions or ways of Performing tasks that we didn't know before okay so that's how we can use this to optimize code now let's take some existing code and have it explain it to us if it doesn't make sense to us so for this example I'm going to use regular Expressions since those are fairly notorious for being complicated and hard to figure out so let's say that I have a regular expression in my code that I don't understand and it's not properly commented so we have no idea what it's doing so I'm going to grab this from my Snippets here and let me copy this and let's go back and let's just ask it to explain this to us so I can just say can you explain what this regular expression does paste in the regular expression and run that prompt and we can see that it says that this regular expression matches most valid email addresses it breaks down as the follows and then it gives you a step-by-step basis here of what each part of this regular expression does so if we scroll back up to the top here we can see that it breaks down each part of this regular expression one at a time and tells us exactly what each of those is matching and it also gives a suggestion here that says that this regular expression is somewhat permissive and will also match some invalid email addresses and then explains to us those email addresses that would make it past this regular expression that we currently have so actually think that the learning aspect of something like chat GPT might be one of the biggest potentials for a technology like this now I'm not saying that it's a hundred percent there but I can definitely see a future where we're no longer searching through you know stack Overflow to see if our questions have been answered but instead just asking something like chat GPT to explain an error or an expression or a section of code and have it guide us through step by step like it's doing here now another thing that we can use this tool for is to convert code from one language to another other so this regular expression that I asked chat GPT about is just a normal regular expression it's not currently written in Python so let's ask chat GPT to convert that to a python statement for us so I'll just come down here to the bottom and just write this out so all I asked here was can you write a python script using that regular expression to match email addresses in a string of text so let's see what it gives us here okay so if we scroll up here and look at what we got we can see that it wrote this code out for us and it also gives us an example of how this can be used so it wrote out some test emails here or some test text here with some emails in it and then shows us how we can use that pattern that it created here to match all those using the re dot find all method and then it prints out all those matches looping through those now in a lot of development jobs a lot of us aren't using only one programming language so you know maybe you've been using python for a while now and then you want to take this script and convert it over to JavaScript but it's been a while since you've used JavaScript and you don't really remember exactly how well with something like this we can just ask it to convert this code to JavaScript so I'll just say can you convert this script to JavaScript and then we will run that and see what it gives us so if we go back up here then we can see that it wrote all of this out for us in JavaScript comments the code so we know step by step exactly what it's doing and again it also gives us this test text here so that we can test to make sure that this is working the way that we plan on it working now speaking of testing our code that's actually another thing that we can use a tool like chat gbt to help us out with now I'm guilty of this I'm sure a lot of people are guilty of this I don't always write the best unit tests for my code sometimes on personal projects I don't even write unit tests for my code but when I work for large companies I really did see the benefit because it does save you a lot of time in the long run if you're writing very good unit tests on a large code base so that you know that any small changes that you make don't break everything in other places in the code and having good test coverage within your code base using unit us can definitely help you with that discovery of errors and prevention of Errors so that you're not releasing something to the public that a unit test would have probably found if you had written them well but like I said on a lot of private projects I don't even do this because I just find writing unit tests boring and I'm sure a lot of other people do too but we can use chat gbt to help us out with this so let's ask chat gbt if it can write some unit tests for this python version of this regular expression code that we gave it before so I'm just going to come down to the bottom here and I'm just going to say write unit tests for the python version of this code and like I said it's going to remember previous prompts it doesn't even have to be the one that you just got a response to it can be something up here and it's going to remember what the python version of this code is and it can write unit tests for it so I'll go ahead and run this okay and let's see what we got here so let's see so it rewrote this to where we are using the unit test Library here and it is uh creating fine emails and its own function now and we can see that it created a unit test test case here and we have one function within here that's testing a few different things now this should probably be split up but let's see what it's got for us here so it's saying that this one should catch a couple of emails here this one should have no emails and this one should just have one email and that's another thing about it helping us with testing is that it might even come up with some examples uh that will we wouldn't have thought to cover ourselves you know sometimes people can't forget these edge cases where they forget to add the education of you know not having results or something like that and that can help us out here and we can see here that it actually says this at the bottom that this is just a simple test may not cover all possible edge cases in a real application you'd want to test the function with a wide range of inputs and colliding including edge cases invalid inputs to make sure it handles all scenarios correctly now if we wanted to continue this example people then I could actually you know just copy and paste this and say hey can you change this to where it does handle a wider range of inputs and more invalid inputs and things like that and it would take care of that for us one thing I'm going to see if it can do here is these are usually broken up into different different functions I believe let's see if I can have this split it up for us foreign okay so I asked it to split those up for us and we can see that it kind of uh realizes its mistakes as it goes it says absolutely each test should ideally be testing only one thing here's how you can split up these tests now we can see that we still have our find emails function but now in our testing class now we have multiple functions here and each function tells exactly what it's supposed to do so this test finding multiple emails this is testing find no emails this test finds a single email and then when you run your test and something breaks then you can narrow it down exactly to the specific function the test function that broke your code so this is something that I'm definitely going to be using chat GPT for more even for my own personal projects because like I said I just find testing boring and when it's a large project yes I understand that it's needed but when I'm working on a private project I just want to get to the phone functionality of what I want my code to do and the testing you know just I never get around to it but using something like chat GPT is going to make that so much easier to where you know I have a little bit of some reassurance in my tests in the background now another area of coding where I could definitely personally improve is my documentation and commenting like I said I usually just like to get down into the functionality and I don't comment as much as I should or document as much as I should now that's another one of those things that I always plan to do later and usually just don't get around to but this is another thing that we can pass off to chat GPT if we paste in our code and see if it can help us out so I'm just going to use the same code that we've been using here for these regular expressions and we can see we don't have any comments in here so I'm going to ask it to comment and document this code for us can you add comments and documentation for that code so let's run this and see what it comes up with okay so now it has finished generating that code let's see what it gave us here okay so we can see that it gave us a very detailed documentation and comments for this code it's using a mix of Doc strings and comments to show us exactly what each function does so that if we come back through this code in the future then we'll more quickly be able to see exactly what everything does and for anybody who's ever gone back and looked at code from years ago that doesn't have comments it's definitely a lifesaver to have good documentation that can tell us exactly what's going on so for this one it explains exactly what the function does in this first part here then it gives us the arguments and then the return values and it's also documenting our classes and functions down here as well okay so the next thing that we're going to cover is probably what I use chat GPD for more than anything else now I don't know about you all but when I first start a new project and I I'm literally staring at a blank page with no code it's difficult for me to get started a blank canvas is difficult for a lot of people and that's not just programmers that's you know people talk about this if whether they're an author or an artist a programmer a musician it's that starting point that's usually the most difficult part because you just have nothing to go off of so I think chat gbt is fantastic for getting that starting point of our project as something that we can build off of so let's say that I have a project idea that I've been wanting to do for a while so for example I play the game League of Legends and for a while I've been wanting to check out their API just to do a hobby project in my off time but I've always put it off because it would just be a hobby project and in the back of my head I know that I'll have to dive into their API documentation go through a ton of trial and error and it's just never worked its way up my my priority list but with chat gbt it makes starting these projects so much easier and just getting some code into your IDE so without even looking at the documentation let's see if we can get a basic script that uses this League of Legends API so I'm just going to write out a prompt here so I'm just asking can you write a python script using the League of Legends API that shows me the percentage of wins for a specific player so let's go ahead and run this and let it think about it and generate a response okay so we can see that it spits out a good bit of code here and some explanations of what everything does so right here at the top it tells us exactly what we need to do it says in order to use this API we first need to sign up for an API key at their developer portal that it's going to use the requests library to interact with the API and then exactly what these parameters are here that we need our API key our region and the Summoner name which is the player name and then we can see exactly how it's using this API in these functions now like I said before I've not actually used their API yet so I don't know how accurate this is this is something that I can't really fact check on until I dive into their documentation but this the point is that we now have a starting point where we don't have a blank page anymore we can see that we have diff different ways of interacting with this API and this would allow us to go out and get an API key plug that into our script and start doing some testing and running some queries to see if it does what we want it to do and it also gives us a pretty good idea of how we would interact with this API to fetch different things like we can see here that a way to get a player's information how to get a match history how to get match results and then calculate the win percentage and things like that so that's probably what I use this technology for the most because I like to use this for the starting points of a new project because it allows you to just Dive Right In without so much trial and error and once you get to the point where chat GPT doesn't have an answer for you or you're not getting the results that you want then it's no big deal because then you're just back to where you were previously where you're going to have to dig through the documentation yourself anyway and do it from scratch so I always like to ask what it comes up you know ask it the project that I want see what it comes up with and if I need to make changes I can make changes and if it's not what I want then I'm just back to where I was to where you know I was going to have to start that project from scratch anyway so I think it's a good way to to use this tool okay so we are just about finished up here but one more thing that I wanted to talk about after seeing all of these benefits of chat gbt is to talk a little bit about one of the main concerns and questions that people have with this technology and that's whether or not AI models like this will replace Developer jobs a lot of people have been wondering this I've seen several videos already of hey you know if it writes code this well is it going to replace us as developers and I understand that concern because we see a lot of examples of its capability in this video but I don't think we have to worry about this quite yet like I said earlier this is just another tool that we'll see programmers using regularly but like I've been saying sometimes it doesn't get everything right especially when working on more complicated projects so we're going to need an actual human who can read and understand the code to determine whether or not we are getting back from these tools what we actually want and whether that code works correctly and we saw that a couple of times in this video as well like when I asked it to rewrite those unit tests that could have been done in a better way and also understanding the code and the better of a developer that you are it's also going to allow you to write better prompts and use these tools more efficiently because if you're someone who doesn't understand that you know unit tests should be split up then you're not going to even know to tell it that prompt of hey that she should be split up so the better developer that you are the better that you're going to be able to use these tools to write better code so when it comes to actually replacing developers I think we're a long way off from that I heard this in another YouTube video uh and I would give them credit if I remembered exactly where I heard it but I think that their response to this question was spot on so when it came to whether or not AI tools like this would replace programmers that person said not exactly it's not going to be AI replacing programmers it's going to be programmers who use AI replacing programmers who don't use Ai and I completely agree with what he said here I think that this is going to be an important tool that we use but not something that takes our job it's going to be something that makes us more productive than anything okay so before we go here I'm going to do one final test with chat CPT and see if it gives me correct answers or not let's see exactly how smart this thing is so if I was to say what's the best resource for learning python on YouTube then it better get this one right let's see what we got okay so let's see what these uh YouTube resources are there we go so I take back everything I said earlier uh about it being wrong sometimes because this is obviously super intelligent and we need to take everything that it says at its word okay obviously I'm joking there uh I think one of the main reasons I'm at the top of this list is because it's a bit behind on its data I think that it collects it's collected up to like September of 2001 or something like that because as you guys know I haven't been as active on this channel as I want to be lately and I'm doing my best to change that and put out more content if I were to put a list of resources together uh I would definitely also include some of these uh channels on here uh syntax he's awesome but some of the others that have come up in the last couple years I don't see on here uh I would definitely add you know channels like Tech with Tim I would have one here and I've heard great things about coding with Harry's Channel although I haven't been able to check out his stuff his stuff in depth by myself yet um but I've heard really good things about his channel as well that's actually a video that I want to do in the near future is I want to release a video of my top resources for learning coding and my favorite YouTube channels where I get my information I think you guys would find that that useful I did one a very very long time ago that's outdated at this point so it's definitely time for an update but with that said I think that's going to do it for this video uh hopefully now you have a good idea for how you can use chat GPT as a tool to help you out with your daily programming task and it can help you be more productive these Technologies are moving extremely quickly so I'm curious to see how these tools evolve in the next few years you know hopefully it's not going to look like a Skynet situation or something like that but we never know it's moving fast but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest way is to Simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon or YouTube and there are links to those pages in the description section below be sure to subscribe for future videos and thank you all for watching
Info
Channel: Corey Schafer
Views: 180,149
Rating: undefined out of 5
Keywords: ChatGPT, chatgpt for programmers, AI for developers, Programming tools, Virtual programming assistant, Coding workflow, AI in software development, Boosting coding skills, Programming tutorial, Python, Javascript, Corey Schafer, Python tutorial
Id: jRAAaDll34Q
Channel Id: undefined
Length: 31min 7sec (1867 seconds)
Published: Sun May 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.