How To Write Code With ChatGPT (Plus Expert Tips)

ChatGPT is an excellent programming tool for beginners and experts alike. Here’s how to turn it into your favorite programming tutor or coding partner.

By: R. Paulo Delgado
September 28, 2023
12 minute reading
writing code with gpt

Artificial intelligence (AI) has emerged as a powerful tool to help programmers write code more efficiently. ChatGPT is especially adept at writing code in various programming languages, debugging a piece of code, converting it from one language to another, or even serving as a real-time tutor to help you learn programming.

But ChatGPT isn’t perfect. As with all popular generative AI tools today, it’s prone to hallucinations—a technical term meaning it produces outputs unrelated to inputs. Nobody knows why language models do this, so we must work around it by verifying generative AI outputs and following best practices when using AI. 

Fortunately, if you learn the basics of writing ChatGPT prompts for generating code, you can use it to do everything from creating simple web pages to more complicated things, such as generating the basic code for an Android app. 

ChatGPT coding versus other AI coding tools

Several excellent AI tools exist for writing code, including GitHub CoPilot, Amazon CodeWhisperer, and Microsoft’s IntelliCode. Some AI coding tools work in real-time inside your programming development environment, suggesting code snippets based on the comments inside your file or the code’s overall context. Other tools are more chat-based, responding to the prompts you provide. 

Like generating image prompts using Midjourney, the output quality greatly depends on the input.

OpenAI, the creators of ChatGPT, used to maintain a separate large language model (LLM) called Codex, which specialized in AI coding. Codex descended from OpenAI’s GPT-3 models but has since been superseded by OpenAI’s chat models, which include its powerful GPT-4 model.

ChatGPT is particularly suitable for newcomers who want to explore programming languages, ask questions, and explain basic concepts. We don’t recommend it as a replacement for full-fledged tutorials, but ChatGPT can help you clarify questions more rapidly while making your way through the tutorial. 

ChatGPT definitely is not a beginner-only tool. You’ll find that ChatGPT code becomes easier to work with the more programming knowledge you have. Often, ChatGPT provides code with errors in it. Although ChatGPT can help debug those errors, it’s easier when you know some programming so you can guide it better. 

What programming language does ChatGPT write the best?

ChatGPT knows a wide variety of languages, including assembly, an old low-level programming language used widely before the emergence of more human-friendly languages such as C, C++, and Java. 

However, ChatGPT’s skills are strongest in languages with vast amounts of openly available code samples. For example, although it can technically write assembly language code, it rarely does so well. ChatGPT is far more likely to generate good code for popular programming languages such as:

  • Python

  • Java

  • Javascript

  • HTML and CSS

  • PHP

  • SQL

  • C/C++

  • C#

  • Ruby

ChatGPT produces erroneous code more often for less popular languages, such as Visual Basic for Applications (VBA), a programming language to automate Microsoft Office products. Although VBA is used extensively in an office context, it’s used in few open-source projects. 

Step-by-step guide for writing code with ChatGPT

The precise steps for writing code with ChatGPT vary slightly depending on:

  • The task 

  • Your level of experience as a coder 

But the procedure is roughly always as follows:

  • Design the tool (depending on the task). 

  • Decide on a programming language. 

  • Provide the specifications in the form of a ChatGPT prompt. 

  • Test the code that ChatGPT generates. 

  • Debug any errors using ChatGPT until the code works. 

Design the tool

You can use ChatGPT to help you design complex apps or tools and work out the tech stack you’ll be using. 

ChatGPT is useful when novice coders need quick answers on what tools or frameworks they might need to design a particular app. The easiest way to do this is to talk to ChatGPT and ask questions based on its outputs. If you struggle with this part, you might consider finding a prompt engineer to help you. 

For this tutorial, let’s assume we know nothing about programming, but we want to create a mobile app. We might start with a ChatGPT prompt like this: 

Prompt: I want to create a mobile app that logs my expenses. What programming language should I use?

ChatGPT’s answer to a prompt about creating a mobile app.

ChatGPT’s answer to a prompt about creating a mobile app.

ChatGPT’s knowledge cutoff date is September 2021. The above example recommends using a framework called Xamarin to create cross-platform mobile apps. This outdated platform has since been superseded by a more powerful framework called .Net MAUI (Multi-platform App UI), highlighting one of ChatGPT’s current limitations. 

It’s essential to remember that AI is best used with human skill. Taking the answer above, you can search the internet for more details or prompt it with another question. 

If the answers seem repetitive or unhelpful, you might need to search again and then come back and prompt ChatGPT with your new knowledge. You can also check out our tutorial on GPT examples to give you some more AI coding prompt ideas. 

Choose a programming language

If you’re an experienced programmer, the final results of the chat process above would depend on the programming language or framework best suited to your background. 

If you’re an absolute beginner at programming, gaining experience in core programming concepts is better before diving into creating a full-scape website app. The easiest way to achieve this experience is to choose a programming language that: 

  • Is beginner-friendly

  • Is easy to set up on your machine 

  • Requires minimal additional files to test and run 

Python matches all the above recommendations. It’s an incredibly easy language to learn, with the bonus that it’s well-suited to creating AI tools such as AI chatbots because of its extensive AI and natural language processing (NLP) libraries. 

The simplest way to get Python working on your machine is to download Microsoft Visual Studio Code and then install Python and the Python development extension

We’ll create a basic Python script for the rest of this tutorial. 

Provide the specifications in the form of a ChatGPT prompt

The great thing about Python is that you can create powerful utilities in a single code file, letting you get your toes wet in programming immediately while giving you some great tools for day-to-day productivity. 

Let’s prompt ChatGPT to create a Python script to run through all the files in a folder and reduce any JPG or PNG files so they’re less than 1 MB in size. 

Prompt: Write a Python script that runs through the files in a directory, checks if each file is a JPG or PNG, then checks the file size. If the file size is greater than 1 MB, reduce the file and save it in a subfolder called “Reduced.” If the file is less than 1 MB, copy it to the “Reduced” folder. Ask the user for which directory to check. 

When prompting ChatGPT to write code, try and think step by step, such as, “If this happens, do that, or else do the other thing.” Then write your prompts that explain this step-by-step procedure to ChatGPT. 

IMPORTANT: Badly written code can cause irreparable damage to your files. Notice that we asked ChatGPT to copy the files into a separate directory. This prevents the code from reducing the images in place and overwriting the original. 

ChatGPT Python script to reduce images.

ChatGPT Python script to reduce images.

ChatGPT informs us that we must install the “Pillow library” and then tells us how. If you don’t know what this means, you can search the internet or ask ChatGPT. 

Asking ChatGPT how to install a Python library.

Asking ChatGPT how to install a Python library.

Test the code that ChatGPT generates

Before going ahead and testing, you should look over the code for any major errors. Even if you’re new to coding tasks, Python’s syntax is highly readable and intuitive because of its similarities to natural language. Beginner programmers can look at Python code and get a feel for what it’s doing. 

The code ChatGPT generated for us has at least one glaring flaw: It blindly scales images to 800 pixels by 800 pixels when they’re over 1 MB. Although the code “works”—it achieves the purpose of reducing images to less than 1 MB—it doesn’t precisely fit the nuances of what we needed because the images will likely be too small. 

This is the faulty line: 

image.thumbnail((800, 800))

Let’s ignore this for now and run the code to see what happens. 

To run the code, copy ChatGPT’s generated code into a new file in Visual Studio Code and save it with a .py extension. Download a few images into a folder from Unsplash or Pixabay for testing, then press F5 on a Windows machine, or click Run → Start Debugging in the top menu of Visual Studio Code on Linux or Mac. A terminal opens at the bottom of Visual Studio Code, asking you to input the directory with the images. The script then processes the images. 

Output of Python script.

Output of Python script.

The code runs without errors, but the result isn’t great. The images it reduced are now far too small, as shown in the screenshot below. 

The code reduced images too much.

The code reduced images too much.

Let’s get ChatGPT to help us debug this. 

Debug any errors using ChatGPT until the code works 

The more experience you have in programming, the easier it becomes to debug code with ChatGPT. Often, it’s easier to tweak and fix the code yourself. 

This code lacks an efficient algorithm to progressively modify the image’s size until we reach a value just below 1 MB. Instead, it blindly reduces the images’ dimensions to a tiny size. 

Prompt: The code created images that are far too small. Improve it so the reduced images are no more than 10% smaller than 1 MB. 

Modified code by ChatGPT.

Modified code by ChatGPT.

Unfortunately, the new solution ChatGPT provides for us is awful coding and highly inefficient. It seeks to reduce the image’s size by iteratively reducing its quality, not its dimensions. The quality could go as low as 10% for huge images while the image width and height remain the same. That isn’t exactly what we wanted, and the screenshot below shows how this produces terrible-quality images. 

The result of faulty ChatGPT code for image reduction.

The result of faulty ChatGPT code for image reduction.

The code also does nothing to check the picture’s resolution. One of the simplest ways to reduce a digital image’s size is to check if it’s using a high dpi (dots per inch) value. Print images need a minimum of 300 dpi, but images for the screen can be saved as 72 dpi. This is one of the quickest ways to reduce an image’s file size. 

A bit of programming experience goes a long way when writing code with ChatGPT. 

We could keep chatting with ChatGPT to further refine the code, providing a new prompt to every new challenge. But leaving it all up to ChatGPT typically leads to one of two situations:

  1. ChatGPT hits a wall and gets confused. 

  2. ChatGPT starts to go around in circles until you feed it more specific instructions based on your understanding of the problem. 

ChatGPT will help you a lot as a tutor, but if you’re using it to create a business software solution, you’re far better off finding freelance programming services on Fiverr to tweak ChatGPT’s initial code drafts. For large-scale projects, AI is fantastic when used as a tool, not as a replacement. 

Let’s give ChatGPT more data to help it solve the problem better. 

Prompt: Modify the code as follows:

  • Don’t reduce image quality below 90%. 

  • Initially, reduce the image’s dimensions to 800 x 800 and save it to a temp image. 

  • Determine the temp image’s size. 

  • If the temp image’s file size is smaller than 10% of 1 MB, increase its dimensions by 10%. Don’t upscale the temp image; reduce the original image again using the new dimensions. 

This new code gave us what we needed. 

ChatGPT is best used to create basic boilerplate code or a template that a human programmer tweaks directly. Trying to get ChatGPT to create perfect code from prompts alone often becomes more time-consuming than doing it on your own. But the interactive nature of providing prompts and debugging code tremendously assists the learning process because of programming’s hands-on nature. 

Tips from professionals who use ChatGPT to write code

We reached out to several professionals who use ChatGPT to write code to give us tips on how they do it. 

Using ChatGPT to work on existing code

Nicholas Dragolea of Fetched, a dog health blog, is a novice coder who uses ChatGPT to write HTML and CSS code for modifying his WordPress website’s theme files. 

“ChatGPT is extremely useful for a coding novice like me because I can request something in plain language and ChatGPT understands it and replies with the required code,” he tells Fiverr. “Before ChatGPT, I had to Google for things and then look at threads on Reddit or Stack Overflow. AI has made me more efficient at editing and customizing my business website.”

Chatting with ChatGPT to flesh out a specification

Andrew Boyd, a serial entrepreneur and managing director of Finty, a website that compares financial products, uses ChatGPT to hash out the details of product specifications. 

“After determining that a new service or idea is in demand, I start to hash out a spec with ChatGPT,” says Boyd. “I write out what I want the service or function to do. For example, detailing who the proposed user would be, what they would do with the app, and its primary goal. I paste this into ChatGPT and ask if it makes sense. Typically, it gets this spot-on the first time.”

Boyd then asks ChatGPT to write the programming specification that he can hand to a developer. He gives ChatGPT details of the tech stack and frameworks they use. 

“At this stage, ChatGPT spits out a spec that is typically quite useful, but it’s not what I use,” Boyd continues. “I read it and then usually iterate a number of times, tweaking functions and objects to get what I need. I also ask it for advice on what option is better, since there’s always a trade-off in building software.”

Boyd then cross-references ChatGPT’s output with another AI tool. 

“Once I’m confident I’ve got a relatively robust brief, I’ll get ChatGPT to start writing scripts. GPT-4 is good for this. And, if it hasn’t done so already, I’ll ask it to write documentation and unit tests.”

Boyd says that what ChatGPT generates is obviously not ready for production and still requires a human programmer to review and improve it, but that it’s a helpful starting point to get working on. 

If you get ChatGPT to generate the first draft of a spec for an app, you can find freelance code-review services on Fiverr to review that draft and bring it to a final stage. 

Find AI programming services from expert Fiverr freelancers

Whether you want to develop an AI chatbot or integrate the ChatGPT API into your application, you can find ChatGPT programming services on Fiverr to help you. 

Fiverr is a marketplace of thousands of expert freelancers specializing in various creative and technical fields. In addition to its search functionality, Fiverr also matches buyers with sellers using its proprietary, neural network-powered AI called Fiverr Neo. You simply chat with Fiverr Neo, which will help you find the needed services. 

To get started, sign up for Fiverr today

About Author

R. Paulo Delgado Tech & Business Writer

R. Paulo Delgado is a tech and business freelance writer with nearly 17 years of software development experience under his belt, including WordPress programming. He is also a crypto journalist for Moneyweb, and proudly a member of Fiverr's Pro Seller program — hand-vetted professionals, verified by Fiverr for quality and service.