One of the more intriguing discoveries about ChatGPT is that it can write pretty good code. I first tested this out in 2023 when I asked it to write a WordPress plugin my wife could use on her website. ChatGPT did a fine job, but it was a simple project.
Also: The best AI for coding in 2025 (and what not to use)
So, how can you use ChatGPT to write code as part of your daily coding practice? Here’s a quick summary:
- ChatGPT can produce both useful and unusable code. For best results, provide clear and detailed prompts.
- ChatGPT excels in assisting with specific coding tasks or routines, rather than building complete applications from scratch.
- Use ChatGPT to find and choose the right coding libraries for specific purposes, and engage in an interactive discussion to narrow your options.
- Be cautious about who owns AI-generated code and always verify the code’s reliability. Don’t blindly trust the generated output.
- Treat interactions with ChatGPT as a conversation. Refine your questions based on the AI’s responses to get closer to the desired output.
- Paid-tier users gain access to some additional features for more comprehensive coding support, including Deep Research analysis and Codex full installation coding.
Now, let’s explore ChatGPT in considerably more depth.
What types of coding can ChatGPT do well?
There are two important facts about ChatGPT and coding. First, the AI can write useful code.
The second is that the AI can get completely lost, fall into a rabbit hole, chase its tail, and produce unusable garbage.
Also: Tech prophet Mary Meeker just dropped a massive report on AI trends – here’s your TL;DR
I found this fact out the hard way. After I finished the WordPress plugin for my wife, I decided to see how far ChatGPT could go.
I wrote a very careful prompt for a Mac application, including detailed descriptions of user interface elements, interactions, what would be provided in settings, how they would work, and more. Then I fed the prompt to ChatGPT.
ChatGPT responded with a flood of text and code. Then it stopped mid-code. When I asked the AI to continue, it vomited even more code and text. I requested continue after continue, and it dumped out more and more code. However, none of the output was usable. The AI didn’t identify where the code should go, how to construct the project, and — when I looked carefully at the code produced — it left out major operations I requested, leaving in simple text descriptions stating “program logic goes here.”
Also: How ChatGPT scanned 170k lines of code in seconds and saved me hours of work
After repeated tests, it became clear that if you ask ChatGPT to deliver a complete application, the tool will fail. A corollary to this observation is that if you know nothing about coding and want ChatGPT to build something, it will fail.
Where ChatGPT succeeds — and does so very well — is in helping someone who already knows how to code to build specific routines and get tasks done. Don’t ask for an app that runs on the menu bar. But if you ask ChatGPT for a routine to put a menu on the menu bar and paste that into your project, the tool will do quite well. The new Codex feature for paid tiers might expand on this, but even OpenAI says that Codex is best for maintenance and support rather than doing all original writing.
Also, remember that while ChatGPT appears to have a tremendous amount of domain-specific knowledge (and often does), it lacks wisdom. As such, the tool may be able to write code, but it won’t be able to write code containing the nuances for specific or complex problems that require deep experience.
I have worked with ChatGPT a lot while trying to write and debug code, and some of the inane suggestions the AI has made have been stunningly stupid and unhelpful. But it has also proved helpful, which is why I keep using it. You just need to be able to differentiate between the two.
Also: How AI coding agents could destroy open source software
Use ChatGPT to demo techniques, write small algorithms, and produce subroutines. You can even get ChatGPT to help you break down a bigger project into chunks and then you can ask it to help you code those chunks.
With that in mind, let’s look at some specific steps for how ChatGPT can help you write code.
How to use ChatGPT to write code
The first step is to decide on what you want to ask ChatGPT, but do not ask it yet. Decide what your function or routine will do, or what code you want to incorporate. Decide what parameters you will pass into your code, and what you would like to get out. Then, think about how you will describe it.
Also:
How to write better ChatGPT instructions
Imagine that you are paying a programmer to perform this task. Are you providing enough information for that person to be able work on your assignment. Are you too vague, and the person who you pay is more likely to have questions or submit something completely unrelated to what it is you want?
This is an example. Let’s say that I want to be capable of summarizing any web page. I want to give the AI an article and receive a well-considered, appropriate summary. I will specify a URL as my input. My output is a block of texts with a summary.
In the old days, the only way to extract data from a web page was by finding the text in between HTML paragraph tags.
With the advent of AI tools you can use a library to perform an intelligent extraction and summary. One of the places ChatGPT excels (and it’s also an area you can easily verify to avoid its authoritative-but-wrong behavior pattern) is finding libraries and resources. Also: The best AI courses for free
OpenAI, the maker of ChatGPT, sells API access to their LLMs so that we can do exactly what you want. In this example, we’ll assume that we don’t wish to pay transaction fees.
Let’s take a look at how to interact with ChatGPT in order to learn how to use this tool for free with a PHP project.
Initially, I used a prompt to get information about the libraries that would provide the functionality. For those who aren’t programmer, a library is a collection of code that a programmer has access to and which does the heavy lifting. This is a great place to start because a large part of modern programming involves finding and selecting the right libraries.
I’m looking for blocks of code that have been written by others to summarize text. Here’s my very first prompt:
List ten open source AI libraries and the languages that they work with, which I can use to generate an overview of the main content of any web page.
I got exactly what I was looking for, including an mention of OpenAI. I think OpenAI could do a great job here, but I don’t have the budget to pay for API fees for this hypothetical project. I’ll narrow it down to
Which of these are free?
ChatGPT hedged their bets by its answer: “Yes, all ten of these AI libraries are open source and free to use. However, some of them may have usage limits or require payment for access to additional features or resources.” Based on that response, my query was clarified:
What libraries do not have usage limits or require additional licensing or payment?
This is a very conversational exchange. I don’t need to ask the question again — I’m just digging deeper as I would if I were chatting with an expert. This time ChatGPT offered me eight libraries, but none of them mentioned the PHP language that I planned to use to code. Next prompt:
Can I use any of those eight libraries with PHP?
I was unsure what each library did. So I asked a second question:
How do you differentiate between Gensim and NLTK?
Still not sure, I clarified my plan of use and asked:
Which library would be better if I wanted to create summaries for web-page articles?
I was pleasantly surprised by the answer: “Sumy is specifically designed for text summarization, which is the task of creating a summary that captures the most important information from a piece of text.” It was now time to learn how to use Sumy in PHP. I asked my final question for this project:
Could you explain how to use Sumy with PHP?
You can play along with these prompts on your computer by pasting them into ChatGPT. In step one, you can see that I chose which program module to seek help with. In this step, I spoke with ChatGPT about which library to use and the best way to integrate it into my program.
The Best AI Chatbots
This approach might not look like programming but it is. Programming is more than just slapping lines of code on a page. Programming is about figuring out the best way to integrate your resources and systems and how to communicate with all the components in your solution. ChatGPT was able to help me with this integration analysis.
I was curious if Google’s Gemini AI can help in a similar way. Gemini provided some additional insights into the planning aspects of programming compared to ChatGPT’s answers.
Don’t be afraid to use multiple tools in order to triangulate answers. Google Gemini and Microsoft Copilot both improved dramatically in the past few years. Early on, (to put it politely) they were terrible. Now they are really good. Gemini’s Jules competes with ChatGPT Codex. I haven’t had the chance to test Codex yet, but Jules looks impressive. These “agentic” are tools that run behind the scenes and modify GitHub repositories.
How to test an AI chatbot’s coding abilities – and how you can do it too
Next up is coding.
Let’s take a moment to pause. This article is titled “How to use ChatGPT to write code.” It will. What we’re really doing is asking ChatGPT to write an example of code.
And: The rise and the fall in programming language popularity since 2016 and what it tells
In this case, things have changed in the last few months. I wrote previously that ChatGPT cannot write your final code unless you are writing a small feature (like the line-sorter/randomizer ChatGPT created for my wife). You’ll first have to maintain the code. ChatGPT does a terrible job of modifying code that has already been written. It’s terrible, in the sense that it doesn’t work. To get new code, you need to ask ChatGPT for something different. ChatGPT can change the prompt it gives you even if it is almost identical.
The basic ChatGPT is still the same: ChatGPT cannot maintain or tweak your code.
This limitation means that you will have to do all the legwork by yourself. We know that the first draft is rarely the final version of a code piece. Even if you expected ChatGPT would generate final code, you’d still need to finish it. You’d have to integrate it into your larger project, test, refine, debug, etc.
Now, there’s Codex. Its sole purpose is to maintain existing code. Codex is limited in many ways, including the fact that it does not remember anything between instructions. It’s also in a totally different context than the rest of your ChatGPT interaction, so it’s like you’re using completely unrelated tools. But Codex is able to maintain. That’s a new thing.
Even if you don’t use Codex doesn’t mean that the example code is useless — far from it. Let’s take a look at the prompt I wrote, based on the earlier project. Here’s the first step:
Create a PHP function named summarize_article.
The URL of an article from a news site such as ZDNET.com, or Reuters.com will be passed to summarize_article.
ChatGPT should use the programming language I specify. I also tell the AI what input to use and provide two sites as examples to help ChatGPT better understand article style. I’m not certain ChatGPT heeded that advice. I’ll now tell it how to perform the bulk of its work:
Within summarize_article retrieve the contents of a web page at the provided URL. Sumy, the PHP library, and any other libraries you need to use, should be used to extract the main content of the article. Ignore any ads or embedded material and reduce it down to about 50 words. Be sure to use complete sentences in the summary. If necessary, you can use more than 50 words to complete the last sentence.
I would use this approach to instruct an employee. I would want them to know they are not limited to Sumy. I wanted to encourage them to use other tools if they needed it.
IBM will also train you in AI basics for free and give you a credential in 10 hours.
In addition, I specified an approximate number words to create bounds on what I wanted for a summary. Later versions of the routine may take that number as parameter. I ended my statement by stating what I wanted to happen:
After processing is complete code summarize_article in order for it to return the summary as plain text.
This code is simple. ChatGPT called another library (Goose), to retrieve the contents of the article. It then passed the summary to Sumy, with a limit of 50 words. Once the basic code is written, it’s just a matter of programming to add tweaks, customize the information passed to the libraries, and deliver results:
One interesting point of note: When I originally tried this test in early 2023, ChatGPT created a sample call to the routine it wrote, using a URL from after 2021. At that time, in March 2023, ChatGPT’s dataset only went to 2021. Now, the ChatGPT knowledge base extends to the end of June 2024 and can search the web. But my point is that ChatGPT made up a sample link that it couldn’t possibly know about:
https://www.reuters.com/business/retail-consumer/teslas-musk-says-fremont-california-factory-may-be-sold-chip-shortage-bites-2022-03-18/
I checked that URL against Reuters’ site and the Wayback Machine, and it doesn’t exist. Never assume ChatGPT is accurate. Always double-check everything it gives you.
Here are a few ways ChatGPT can make mistakes or have hallucinations. Even AI makes mistakes.
You can do a few things to refine your code, debug issues, and anticipate possible errors. My favorite new AI-enabled hack is to feed code into a ChatGPT session that’s different (or to a chatbot of a completely different type) and ask: “What’s wrong with this code?”
No, not now or at least, not yet. ChatGPT is a program that can perform at the same level as a first-year student of programming, but it is lazy (like this first-year student). The tool could reduce the need for entry level programmers.
At its current level, AI will make it easier for entry-level (and even more experienced) programmers to write code and look up information. The AI is a time saver, but it can’t perform many programming tasks on its own — at least for now. In 2030? Who knows?
Where can I find coding answers on ChatGPT?
Just Ask it. You can see how I narrowed the answers using an interactive dialog. Do not expect a single question to solve all your problems. Use the AI as an assistant and resource and you will get a lot of useful information.
Want a job in programming? Learn these three programming languages
Test that information, because, as John Schulman a cofounder of OpenAI says, “Our biggest concern was around factuality, because the model likes to fabricate things.”
Is it guaranteed that the code generated by ChatGPT is error-free?
Hell, no! You can’t even trust the code that human programmers create. I don’t believe any code that I have written. The code that comes out of the coding process is incredibly flawed. Bugs are inevitable. You need to test and test again before you ship. Then, beta test with a small group of chosen victims. Then, beta test your product with a larger user community.
There will still be bugs, even after all the work. Just because an AI has a go at coding doesn’t mean that it can produce bug-free code. Never trust. Verify everything. You won’t get a bug-free result. This is the nature and purpose of the universe.
How do I proceed if the code that I receive is incorrect?
Consider the chatbot like a subordinate or a student who is a little uncooperative. What would you do, if the person returned code that did not work? You would send them out again with instructions on how to do it correctly. I’ve tested it with ChatGPT 4. When things don’t seem to work, I say “That didn’t work. Please try again.”
Also you can use OpenAI Codex’s super powerful AI agent Codex now for only $20
This is what the AI does. It often gives me different variations of the same problem. I’ve repeated the process four or even five times until I got a good answer. Sometimes, however, the AI runs short of ideas. Sometimes, the answer you get when you ask for a try-again is completely unrelated to your request.
If you reach the limit of the AI’s ability, you will have to code the problem yourself. Nine times out of ten, especially when it comes to basic coding and interface-writing challenges the AI is able to do its job.
What level of detail should I provide when I ask ChatGPT about a programming problem?
Detailed. The more you leave up to interpretation, the more AI will do its own thing. When I use ChatGPT as a tool to help me program, I imagine that I am giving a programming assignment to one of my students.
6 ways to improve your ChatGPT prompts and get better results faster
Have I given enough information to the person to be able to create a draft or will they need to ask me more questions? Will that person be so poorly guided that they will go in the wrong direction? Do not be lazy. ChatGPT will save you hours, or even days, of programming. It has done so for me. But only if you provide it with useful instructions.
Who owns my code if I use ChatGPT?
It turns out that there is not much case law to answer this question. In the US, Canada and the UK, copyrighted items must have been created by humans. Code generated by an AI tool, therefore, may not be protected by copyright. Liability issues also arise based on the source of the training code and the way the code is used.
ZDNET conducted a deep dive into this topic and spoke with legal experts. They produced three articles. If you are concerned about this (and you should be if you use AI to help you with code), I recommend that you read these articles:
- Ownership of the code? Does it still belong to your company if ChatGPT’s AI helped you write your app? What is your liability exposure if you use AI-generated codes?
- The question is: Who owns the code, images and narratives created by AI?
Which programming languages does ChatGPT understand?
The majority of languages. I tested modern languages like PHP, Python and Java, Kotlin Swift C# and more. Then I asked the tool to write code in obscure languages such as COBOL (the report generator, not the role playing game), Fortran, Forth and LISP.
To top it off, I gave the tool this prompt:
Create a sequence to display ‘Hello world’ in ASCII blinking lights on a PDP8/e’s front panel
My first computer was a PDP8/e, and ChatGPT instructed me on how to toggle a program by using front-panel buttons. I was amazed, gleeful and ever so slightly scared.
How can ChatGPT assist me with data visualization and analysis tasks?
You can do a lot without any code. You can read my entire article about this topic: The Moment I Realized ChatGPT Plus Was a Game-Changer for My Business.
In addition, I wrote a piece about how to create charts and tables using ChatGPT.
Here’s where the fun begins. In the article, I asked ChatGPT plus, “Make a bar chart of the top five cities in the world by population,” which it did. But do you want to code? Try asking
Create a bar graph of the top five cities around the world based on population in Swift. You can find the population data online. Include any necessary libraries.
By adding the “in Swift” you specify the programming language. By specifying the source of the data and forcing ChatGPT to include libraries, AI brings in other resources that the program needs. It is for this reason that, in order to program with an AI, you need to have some programming knowledge. If you know how to program, you can get a chunk annotated code in just three sentences. Nice, huh? How does ChatGPT deal with differences in dialects and implementations between languages?
OpenAI doesn’t have the exact answer to this question, but we can shed some light by understanding how ChatGPT works. Remember that dialects, implementations, and little quirks of programming language (and their little tweaks) change more quickly than the full languages. This makes it difficult for ChatGPT and many other programming professionals to keep up.
How I used ChatGPT for a custom JavaScript Bookmarklet
I would work on these two assumptions.
- the more recent the dialectic changes, the less likely ChatGPT will know about them.
- the more popular a particular language is, the more data it has to learn from, and the more accurate the results.
So what’s the bottom-line? ChatGPT is a useful tool. Don’t give it superpowers. Yet. You can follow me on social media. Follow me on Twitter: @DavidGewirtzon Facebook at Facebook.com/DavidGewirtzon Instagram at Instagram.com/DavidGewirtzand on YouTube at YouTube.com/DavidGewirtzTV.
Receive the top stories of the day in your inbox every morning with our Tech Today Newsletter.