From the course: ChatGPT for Web Developers

Use ChatGPT to explain, debug, and optimize code - ChatGPT Tutorial

From the course: ChatGPT for Web Developers

Use ChatGPT to explain, debug, and optimize code

- [Instructor] ChatGPT was trained on millions of lines of code, so it is perfectly capable of processing, debugging, and explaining code to you. In this video, I'll show you how to use ChatGPT to explain code, which is written in (indistinct) JavaScript. And we start with a simple example with low complexity. So first I'm going to ask ChatGPT to explain this code, but actually the question is implied. Because without any prompts, ChatGPT is perfectly capable of figuring out which output is expected, in which programming language it is written, and which task to perform, meaning that it will return a line by line explanation of these code snippets. So here we can read that this function is even and it is a JavaScript function, which is used to check whether a given number is even or odd. So now let's try again, but with a prompt this time by providing instructions of the task to perform. We also tell ChatGPT which programming language to use, if this is necessary. But first let me explain to you what is a prompt. A prompt is a text input in natural language that you submit to the chat bot, to give instructions, context, and the information as to what you're looking for. So I'm going to write, can you explain? And I don't need to specify what because ChatGPT can remember your last input. So it's going to link this question to your last input, and I'm going to hit enter. So right away we're going to get a generated detailed response. First, you can read that the function is to check if a number is even, with a breakdown, and a line by line explanation of the function. So we can see that the function signature is even, it is named even, and it explains also the logic inside the body, which is to calculate the remainder of a number with the modulus operator. And it explains also that if the remainder is zero, this means that this is divisible by two. So this is an even number. Otherwise, this is an odd number. And on top of that, you're going to get explanations and also example as to how to use it, right below. So let's look at another example, with medium complexity this time. I'm going to write, can you explain, so this is my prompt. Can you explain this function? So this is fairly simple to explain for an experienced developer. But let's take this from the point of view of a complete beginner in web development. And we're going to get a line by line explanation of this function. So first we see that this is the function find even numbers, that takes an array, as a parameter. Then it's going to make an iteration, to check which number in this array is even. Then return in your array, including even numbers. It's going to also explain, so the condition which is used to check if a number is divisible by two by using the modulus operator. And after that, it's going to give us also a correction because ChatGPT is also capable of fixing the code for you. So I had purposely left a small mistake, a slight mistake in this code. And automatically it was corrected by ChatGPT. So that's great, I didn't need to ask. But I'm going to write also another prompt, which is to say, can you fix this code? Let's say that I know that there is a mistake but I'm not capable of finding where to fix it. So I'm going to do, can you fix this code? And it's going to tell us that we have two issues in this code. And for this example, so it's going to replace this statement, because I'm using the equal sign but it's not necessary in this example. Instead, we're just going to use the smaller than operator and that's going to allow the code to run more efficiently. And here we go. So here we just need to copy this code snippet, and take the corrected code and add to our projects. So asking ChatGPT to explain code may come in handy for beginner developers wanting to learn how to code and improve their coding skills. So now let's look at the most common frequently asked questions about ChatGPT. So what programming languages knows ChatGPT? So ChatGPT can understand and support many programming languages, like C++, PHP, COBOL, and also modern programming languages like Ruby, Python, Java, Kotlin, and Swift. Another common question is can ChatGPT find and fix bugs? We just saw a quick example. As a reminder, ChatGPT was trained on millions of open source lines of code. So it is very helpful for processing, debugging, and optimizing code. Finally, can ChatGPT write code? The response is yes, absolutely. With advanced natural language processing capabilities, combined with the support of many programming languages, ChatGPT can provide assistance with your next coding assignments and web projects. So we're going to see together plenty of real world examples, built with the help of ChatGPT. Now let's see next more of ChatGPT in action.

Contents