Can ChatGPT write code? How to use it for software development

Edgar Cervantes / Android Authority

If you’ve ever tried programming from scratch, you probably already know that it involves logical thinking, repetitive typing, and fixing bugs. For these reasons, it’s often easier to work with a programming partner who can help you come up with ideas and fix unintended bugs. Luckily, ChatGPT can do just that for free. Whether you’re writing code or need to review an existing code base, ChatGPT has become an invaluable tool for software development almost overnight.

So in this article, let’s take a closer look at ChatGPT’s encoding features and how you can use them for your own projects.

Which programming languages ​​does ChatGPT know?

ChatGPT’s underlying large language model has been trained on billions of text samples, most of which come from the web. With so many websites dedicated to writing code these days, it’s safe to say that ChatGPT knows a thing or two about major programming languages. For a more data-backed answer, see the following list of popular programming languages ​​pulled from public GitHub repositories:

Python Java JavaScript C / C++ / C#

Of course, this is by no means a complete list. In general, you’ll find that ChatGPT delivers competent code in older, more established languages ​​since it has more training to support it. That’s not to say you can’t use it on a newer language like Kotlin, just that the results might not be as impressive.

One way to potentially improve ChatGPT’s ability to write code is to use the latest GPT-4 model. It’s an optional feature for now, but it greatly enhances the chatbot’s capabilities. Unfortunately, accessing GPT-4 requires a subscription to ChatGPT Plus, which costs $20 a month. To keep things simple, in this article we’ll be using the free version of ChatGPT; Just know that you can expect even better results with GPT-4.

How to use ChatGPT to write code

Calvin Wankhede/Android Authority

There are several ways you can use ChatGPT to write code. Regardless of the approach, however, remember that the usefulness of the code depends entirely on your command prompt. In other words, you need to be specific about what you want out of the end product—the more detailed your prompt, the better.

Another thing to keep in mind is that while you can ask ChatGPT to write entire apps or projects, it’s probably not the best strategy. You see, the chatbot is prone to errors, so it’s better to generate short snippets and check if they work. And with that in mind, it’s also worth noting that ChatGPT’s training data is almost two years old, so you might run into outdated conventions and links. We will highlight one such example soon.

READ :  The rumor mill is unveiling several new features reportedly coming to the Waze app

Below are a few ways ChatGPT can help you write code.

Write code with ChatGPT

Let’s start with a simple example that involves some JavaScript, HTML, and CSS. I asked ChatGPT to create a website that would show the current trading price of a specific stock. Or in this case the S&P 500 Index.

On his first try, ChatGPT generated a bit of code that looked like it would work, but I wasn’t very happy with his approach. In particular, I didn’t like the fact that the code called jQuery, an external JavaScript library. So I asked ChatGPT if it could get rid of this requirement and use vanilla JavaScript instead. The chatbot committed and generated code without jQuery as you can see in the second screenshot.

ChatGPT can also refine the generated code, just mention your personal preferences.

Did ChatGPT’s code work for the site? Yes, as long as I followed his instructions. You see, in order to get the live price for a specific stock, a connection to a third-party website is required. ChatGPT used Alpha Vantage’s API, so I had to sign up for my own access key and add it to the code. With that, I had a working web page showing the current price of the S&P 500 Index.

Calvin Wankhede/Android Authority

From that point on I can modify the website to my own needs or ask ChatGPT to further improve the code. For example, I can ask it to include a list of stocks instead of just showing the S&P 500 and have the page refresh automatically every few minutes. This way I always have the latest price without having to manually update it every time.

Use ChatGPT to explain a feature

Have you ever encountered code that you don’t fully understand? ChatGPT can help with this. You can just copy and paste the function into a command prompt and ask for an explanation like I did in the screenshot above. I wrote the Python code as part of a Telegram chatbot that sends a random XKCD comic whenever the user requests one.

READ :  Oppo Find X5 Pro review: An Android powerhouse fit for business

As you can see, it summed up the function’s context and even wrote out a line-by-line breakdown. ChatGPT also specified that the code uses the Python Telegram bot library, although I didn’t specifically mention it.

Improve or simplify code

The example above showed how ChatGPT understands features, but can it improve on something you’ve already written? Yes, and it does a really good job too. I continued with the above function, which retrieves a random XKCD comic, and asked ChatGPT if there were any ways to improve my code.

Much to my surprise, ChatGPT managed to provide really helpful suggestions and even rewrote the feature with all the fixes included. In a direct comparison, the updated function is clearly the better one. It includes proper error handling, follows Python’s variable conventions, and even includes comments.

Convert code from one language to another

If you’ve worked with multiple programming languages, you know that one isn’t always better than the other. But what if you have an existing code snippet that needs to be converted to another language? ChatGPT can also help with this.

Again, we’ll continue using my random XKCD comic function and ask ChatGPT to rewrite it in JavaScript. Remember that I originally wrote it in Python, which is a completely different language.

ChatGPT can translate programming languages, but cannot always find a perfect replacement.

ChatGPT converted syntax from one language to another as expected. However, the lines where a telegram message was sent back to the user were incorrect. Although I’ve been asked to import another library for JavaScript, I haven’t found one that supports using ChatGPT. This leads me to believe that ChatGPT was either hallucinating or inventing a JavaScript function that looks convincing but isn’t really connected to Telegram.

What did we learn from this exercise? Asking ChatGPT to translate code works for smaller chunks, but it can stumble in some areas. Accuracy largely depends on how heavily your code relies on external libraries and services. I’m sure ChatGPT would have performed better if I had fed it some documentation on a JavaScript Telegram bot library.

Find bugs in the code

Debugging can be a time-consuming process, but ChatGPT can take care of it when you paste the code into your first command prompt. For this example, I asked it to generate a piece of buggy Python code and point out its errors.

READ :  iOS 17: release date, features, iPhone compatibility, more

Why isn’t my ChatGPT code working?

Edgar Cervantes / Android Authority

As we have seen in the examples above, ChatGPT does not always generate ready-to-use results. In fact, it can even make big mistakes that prevent the code from running at all. Here are some reasons why this might happen and how to fix them:

Syntax Error: If the code doesn’t work or doesn’t compile at all, most likely ChatGPT made a glaring mistake. Just copy the error and ask for advice on how to fix it in a follow-up prompt. With enough context, the chatbot will likely understand what it did wrong the first time and offer a revised version that actually works. Missing dependencies: ChatGPT may generate code when calling or referencing an external library or module. In programming, a library refers to a collection of code that you can import into your project. These may need to be installed before you can run the code. So ask ChatGPT if the code relies on dependencies. Outdated Information: As mentioned earlier, ChatGPT does not know anything that happened after 2021. So there’s a chance you’re using newer versions of libraries, languages, and other tools that ChatGPT just can’t accommodate. There’s no easy fix for this, but you could try to coach it through a series of prompts with more recent information. Alternatively, you can try your luck with Bing Chat as it has the ability to search the web. Character Limit: If you are trying to create a complex program, chances are it will stop abruptly at some point. This is due to ChatGPT’s hidden character limit, which is currently around 4,000 characters. To get around this limitation, just ask for code chunks and piece them together in a code editor.

If these limitations seem like a deal-breaker to you, remember that there are other AI-powered options. Some of them are also more competent. For example, GitHub Co-Pilot tops our recommended list of ChatGPT encoding alternatives. That’s because it’s explicitly trained on large codebases, making it far more accurate than ChatGPT when writing code.

Comments