Search results for

All search results
Best daily deals

Affiliate links on Android Authority may earn us a commission. Learn more.

Can ChatGPT write code? Here's how to use it for software development

Can you use an AI chatbot to automate software development? Yes, but some conditions apply.
By

Published onJune 18, 2023

ChatGPT stock photo 7
Edgar Cervantes / Android Authority

If you’ve ever tried to code from scratch, you probably already know that it includes logical reasoning, repetitive typing, and fixing bugs. For these reasons, it’s often easier to work with a coding partner that can help you come up with ideas and resolve unintended errors. Luckily, ChatGPT can do just that for free. Whether you need to write code or review an existing codebase, ChatGPT has become an invaluable tool for software development virtually overnight.

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

QUICK ANSWER

Yes, you can use ChatGPT to write code in various programming languages ranging from C++ to Python. The chatbot can also find bugs in your code, suggest improvements, and help you convert from one language to another. Keep reading to learn more, including examples of how to use ChatGPT for software development.


JUMP TO KEY SECTIONS

What programming languages does ChatGPT know?

ChatGPT’s underlying large language model was trained on billions of text samples, most of which originated from the internet. 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, consider the following list of popular programming languages, sourced from public GitHub repositories:

  • Python
  • Java
  • JavaScript
  • C / C++ / C#
  • Go
  • PHP
  • Ruby
  • Shell

Of course, this is by no means an exhaustive list. Generally speaking, you’ll find ChatGPT will deliver competent code in older, more established languages since it has more training to back it up. That’s not to say you can’t use it for a newer language like Kotlin, just that the results may 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 improves the chatbot’s capabilities. Unfortunately, getting access to GPT-4 requires a subscription to ChatGPT Plus, which costs $20 per month. OpenAI is also testing a Plus tier beta feature dubbed Code Interpreter that should make it much easier to test Python code in a sandbox or virtual machine.

I’ll use the free version of ChatGPT throughout this article to keep things simple; just know that you can expect even better results with GPT-4.

How to use ChatGPT to write code

chatgpt who are you stock photo
Calvin Wankhede / Android Authority

There are a few different ways in which you can use ChatGPT to write code. But regardless of the approach, remember that the usefulness of code will depend entirely on your input prompt. In other words, you must specify exactly what you want from the end product – the more detail in your input prompt, the better.

Another thing to keep in mind is that even though you can ask ChatGPT to write entire apps or projects, that’s likely not the best strategy. You see, the chatbot is prone to making mistakes so it’s better to generate short snippets and verify that each one works. And on that note, it’s also worth noting that ChatGPT’s training data is nearly two years old so you might run into outdated conventions and links. We’ll highlight one such example soon.

For now, here are some ways in which ChatGPT can help you write code.

1. Writing 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 displays the current trading price of a particular stock. Or in this case, the S&P 500 index.

In its first attempt, ChatGPT generated a bit of code that looked like it would work but I wasn’t too happy with its approach. Specifically, I didn’t like the fact that the code called for jQuery, an external JavaScript library. So I asked ChatGPT if it could do away with that requirement and use vanilla JavaScript instead. The chatbot obliged and generated code without any jQuery, as you can see in the second screenshot.

ChatGPT can refine the code it generates too, just mention your personal preferences.

Did ChatGPT’s code for the website work? Yes, as long as I followed its instructions. You see, fetching the live price for a particular stock requires connecting to a third-party website. ChatGPT used Alpha Vantage’s API so I needed to sign up for my own access key and add it to the code. With that in place, I had a working webpage that displays the current price of the S&P 500 index.

chatgpt create website result
Calvin Wankhede / Android Authority

From this point on, I can modify the website according 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 displaying the S&P 500 and have the page auto-refresh every few minutes. That way, I’ll always have the latest price without needing to manually refresh it each time.

2. Use ChatGPT to explain a function

Have you ever come across a piece of code that you don’t fully understand? ChatGPT can help with that. You can simply copy and paste the function in a prompt and ask for an explanation as 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.

As you can see, it nailed the context of the function and even wrote down a line-by-line breakdown. ChatGPT also accurately stated that the code used the python-telegram-bot library even though I did not explicitly mention it.

3. Improving or simplifying code

The above example showed how ChatGPT understands functions, but can it improve something you’ve already written? Yes, and it does a really good job too. Continuing with the above function that fetches a random XKCD comic, I used a bit of prompt engineering to ask ChatGPT if there were any ways to improve my code.

Much to my surprise, ChatGPT managed to deliver genuinely helpful suggestions and it even rewrote the function with all of the fixes included. When compared side-by-side, the updated function is clearly the superior one. It includes proper error handling, follows Python’s variable conventions, and even includes comments.

4. Can ChatGPT 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 snippet of code that needs to be converted into a different language? ChatGPT can help with that too.

Yet again, we’ll continue using my random XKCD comic function and ask ChatGPT to rewrite it in JavaScript. Remember, I wrote the original in Python, a completely different language.

ChatGPT can translate programming languages, but it can't always find perfect replacements.

ChatGPT converted the syntax from one language to another as expected. However, the lines that involved sending a Telegram message back to the user weren’t accurate. Even though it told me to import a different library for JavaScript, I didn’t find one that supported ChatGPT’s usage. That leads me to believe that ChatGPT either hallucinated or made up a JavaScript function that looks convincing but won’t actually interface with Telegram.

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

5. Finding bugs in code

Debugging can be a time-consuming process, but ChatGPT can take care of it if you include the code in your initial prompt. For this example, I asked it to generate a piece of buggy Python code and point out its flaws.

Why doesn’t my ChatGPT code work?

ChatGPT stock photo 10
Edgar Cervantes / Android Authority

As we saw in the above examples, ChatGPT doesn’t always generate ready-to-use results. In fact, it can even make major mistakes that prevent the code from running at all. Here are a few reasons why this might happen and how you can fix them:

  1. Syntax errors: If the code doesn’t work or compile at all, ChatGPT has most likely made a glaring mistake. Simply 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 around and offer a revised version that actually works.
  2. Missing dependencies: ChatGPT may generate code where it calls or references 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 any dependencies.
  3. Outdated information: As mentioned previously, ChatGPT doesn’t know about anything that has happened post-2021. So chances are that you’re using newer versions of libraries, languages, and other tools that ChatGPT simply cannot account for. There’s no easy solution for this, but you could try coaching it with newer information through a series of prompts. Alternatively, you can try your luck with Bing Chat, since it has the ability to search the internet.
  4. Character limit: If you attempt to generate a complex program, chances are that it will stop abruptly at some point. This is because of ChatGPT’s hidden character limit, which currently hovers around 4,000 characters. To overcome this limitation, simply 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 out there. Some of them are more competent as well. GitHub Co-pilot, for example, tops our recommendation list of ChatGPT alternatives for coding. That’s because it has been explicitly trained on large codebases, which makes it far more accurate than ChatGPT for writing code.


FAQs

No, it’s unlikely that ChatGPT will entirely replace programmers. Instead, programmers will likely use chatbots to speed up their existing workflows.

Yes, ChatGPT can write code but it doesn’t always produce the best results. If you get an error, you’ll have to ask the chatbot to rectify it. The whole process could take longer than writing good code in the first place.