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.
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:
- C / C++ / C#
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. To keep things simple, we’ll use the free version of ChatGPT throughout this article; 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 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.
Writing code with ChatGPT
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.
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.
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.
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 asked 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.
Converting 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.
ChatGPT can translate programming languages, but it can’t always find perfect replacements.
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?
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:
- 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.
- 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.
- 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.
- 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.