How to use ChatGPT to write code?

Updated 10 months ago on July 18, 2023

One of the most intriguing discoveries about ChatGPT is that he can write some pretty good code. I put this to the test in February when I asked him to write a WordPress plugin for my wife to use on her website. He did a great job, but it was a very simple project.

How can you use ChatGPT to write code as part of your daily coding practice? That is what we will explore in this article.

What types of coding does ChatGPT do well with?

There are two important facts about ChatGPT and coding. The first is that it can actually write useful code. The second is that it can get completely lost, fall down the rabbit hole, chase its own tail and produce completely unusable garbage.

I've seen this first hand. Having finished working on a WordPress plugin, I decided to see how far ChatGPT could go. I wrote a very thorough request for a Mac application, including a detailed description of the UI elements, interactions, what should be provided in the settings, how they would work, etc. I then submitted it to ChatGPT.

ChatGPT responded with a stream of text and code. Then he stopped in the middle of the code. When I asked it to continue, it spewed out more code and text. I requested continuation after continuation, and he spewed out more and more code. But. none of it was usable. He didn't define where the code should be, how to build the project, and - when I looked closely at the code he created - he skipped the basic operations I asked for, leaving just text descriptions saying "the program logic is here".

After many repeated tests, it became clear to me that if you ask ChatGPT to create a complete application, it will fail. The implication of this observation is that if you know nothing about coding and want ChatGPT to create something for you, it will fail.

What ChatGPT excels at - and does very well - is helping those who already know how to code to build specific procedures and perform specific tasks. Don't ask for a menu bar application. But if you ask ChatGPT for a menu bar program and then insert it into your project, you'll be successful.

Also, keep in mind that while ChatGPT seems to have a huge amount of knowledge about a particular area (and often does), it lacks wisdom. So he may be able to write code, but he will not be able to write nuanced code for very specific or complex problems that require deep expertise to understand.

Use ChatGPT to demonstrate techniques, write small algorithms, and create subroutines. You can even ask ChatGPT to help you break a large project into parts, and then ask it to help you code those parts.

So with that in mind, let's look at some specific steps for using ChatGPT to write code.

How ChatGPT can help you write code?

In this first step, you need to decide exactly what you want to ask ChatGPT for - but don't ask it for anything yet. Decide what your function or procedure should do, or what you want to learn to include in your code. Decide what parameters you're going to pass into the code and what you want to get as a result. And then look at how you are going to describe it.

Imagine you are paying a human programmer to do this. Do you give him enough information to enable him to accomplish your task? Or are you being too vague, and the person you're paying is likely to ask questions or do something completely unrelated to what you want?

Here's an example. Let's say I want to be able to summarize any web page. I want to send it something like this article and get back a short summary that is well thought out and appropriate. As input, I'll specify the URL of the web page. On the output I will get a block of text with a summary.

Continuing with the above example, the old way of extracting data from web pages was to look for text between HTML paragraph tags.

But as AI tools evolve, it makes more sense to use an AI library for intelligent retrieval and summarization. One place where ChatGPT excels (and it's also an area you can easily check to avoid its authoritative but incorrect behavior) is in library and resource searching.

OpenAI (creator of ChatGPT) sells API access to the GPT-3 and GPT-4 engines, which do exactly what we need. But in this example, let's assume that we don't want to pay transaction fees.

So let's take a look at interacting with ChatGPT to understand how to use such a tool, and for free, in a PHP project.

I started with a query to get information on which libraries could provide the functionality I needed. A library (for those who are not programmers) is a set of code that a programmer can access and do most of the hard work for a specific purpose. A big part of modern programming is finding and choosing the right libraries. So this is a good starting point.

In this case, I'm considering blocks of code written by other people that will summarize the text. Here is my first task:

Describe ten different open-source artificial intelligence libraries (and the languages they run in) that I can use to create a summary of the main content of any web page, without regard to ads or embedded content.

This gave me exactly what I wanted, including a mention of OpenAI's offerings. I think OpenAI would be great for this purpose, but for this hypothetical project, I don't want to budget for API fees. So I'm judging the question.

Are they free?

In its response, ChatGPT backed up. Here's what it says: "Yes, all ten of these AI libraries are open source and free to use. However, some of them may have usage restrictions or require payment to access additional features or resources." Based on this, I have clarified my question.

Which of these libraries have no usage restrictions and require no additional fees or licensing?

Note that this is very much a conversation. I don't need to re-ask the original question. I'm just delving into the question just as if I had an expert on hand and wanted clarification. In this case, ChatGPT provided me with eight library options, but none of them mentioned the PHP language I planned to write code in. So, here's the next clue:

Which of these 8 libraries can be used with PHP?

It returned three libraries, but I wasn't sure what each one was doing. So another question:

What is the difference between Sumy, Gensim and NLTK?

I still wasn't sure, so I clarified my usage plan and then asked:

If I want to create summaries of news articles on web pages, which library is best?

The response I received was clear and promising: "Sumy is specifically designed for text summarization, which is the task of creating a summary that gathers the most important information from a piece of text." So, it was time to see what it takes to use this program in PHP. I asked my final question for this part of the project:

Can you explain how to use Sumy from PHP?

Feel free to play around on your computer and paste these prompts into your ChatGPT instance. Note that in step 1, I decided which software module I needed help on. Then, in this step, I talked to ChatGPT to decide which library to use and how to integrate it into my project.

It doesn't sound like programming, but I assure you that it is exactly that. Programming is not just writing lines of code on a page. Programming is figuring out how to integrate all the different resources and systems, how to communicate with all the components of your solution. In this case, ChatGPT helped me do that integration analysis.

By the way, I was wondering if Google's Bard could help in the same direction. Bard can't write code, but it gave some additional insights into the scheduling aspect of programming compared to ChatGPT's answers. So feel free to use multiple tools to get the answers you need. Here's that story: Bard vs. ChatGPT: Can Bard help you program? Since I wrote this article, Google has added some coding capabilities to Bard, but they aren't that great. You can read about it here: I tested Google Bard's new coding capabilities. It didn't go very well.

What follows is coding.

So, let's pause for a moment. This article is called "How to use ChatGPT to write code". And so it will be! But what we're really asking ChatGPT to do is to write sample code. Wait a minute. What?

Let's be clear about this. Unless you are writing a very small function (like the string sorter/randomizer that ChatGPT wrote for my wife), ChatGPT will not be able to write your final code. For one thing, you'll have to maintain it. ChatGPT is terrible at modifying code that's already written. Terrible, in the sense that it doesn't do it. So in order to get new code, you have to ask ChatGPT to generate something new. As I discovered earlier, even if your request is virtually identical, ChatGPT can change what it gives you in the most unexpected ways.

So, bottom line: ChatGPT can't maintain your code or even tweak it.

This means that you have to do it yourself. As you know, the first draft of code is rarely final. So even if you expected ChatGPT to create the final code, it would only be a starting point from which you need to bring it to completion, integrate it into your larger project, test it, refine it, debug it, etc.

But this does not mean that the example code is useless. It is far from it. Let's take a look at the hint I wrote based on the project described earlier. Here's the first part:

Write a PHP function called summarize_article.

The summarize_article input will be passed the URL of an article on a news site, such as ZDNET.com or Reuters.com.

I tell ChatGPT the programming language it should use. I also tell it the input data, but I also provide two websites as samples to help ChatGPT understand the style of the article. To be honest, I'm not sure ChatGPT didn't ignore this prompt. Next, I tell him how to do the bulk of the work:

Within summarize_article, retrieve the contents of the web page at the specified URL. Using the Sumy library from PHP and any other necessary libraries, extract the main body of the article, ignoring ads and embedded content, and reduce it to about 50 words. Make sure the summary consists of complete sentences. If necessary, you can go beyond 50 words and finish the last sentence.

This is very similar to how I would instruct an employee. I would want him/her to know that their capabilities are not limited to Sumy. If they need another tool, I want them to use it.

I also specified an approximate word count to create boundaries for what I wanted as a summary. In a later version of the program, this number can be specified as a parameter. At the end, I specified what I wanted to get as a result:

Once processing is complete, encode summarize_article so that it returns the summary as plain text.

The resulting code is quite simple. ChatGPT accessed another library (Goose) to get the content of the article. It then passed it to Summy with a 50 word limit, after which it returned the result. That was it. But once the basics are written, all that's left to do is program, add your tweaks, customize what is passed to the two libraries, and output the results.

One interesting point to note. ChatGPT created an example call to a procedure it wrote using a URL after 2021 (when the ChatGPT dataset ends).

I checked this URL on both the Reuters site and the Wayback Machine and it doesn't exist. ChatGPT just made it up.

Questions and answers

Does ChatGPT replace programmers?

Not now - or at least not yet. ChatGPT programs at the level of a talented freshman student, but it's lazy (just like that freshman student). It may reduce the need for entry-level programmers, but at the current level, I think it will just make life easier for entry-level programmers (and even programmers with more experience) to write code and find information. It's definitely a time saver, but there's not much of a programming project he can do on his own - at least right now. And in 2030? Who knows.

How do I get coding answers on ChatGPT?

Just ask about it. You saw above how I used the online discussion dialog to narrow down the answers I needed. When working with ChatGPT, don't expect one question to magically do all the work for you. But use ChatGPT as a helper and resource, and it will give you a lot of very useful information. Of course, verify this information because, as John Shulman, co-founder of OpenAI, says, "Our biggest concern was factuality because the model likes to make things up."

If I use ChatGPT to write my code, who owns it?

As it turned out, case law does not yet provide a clear answer to this question. In the US, Canada and the UK, it is required that what is subject to copyright must be created by human hands, so code generated by artificial intelligence tools cannot be protected by copyright. There are also liability issues depending on where the training code came from and how it was used. ZDNET has done in-depth research on this topic, talked to legal experts, and produced the following three articles. If you care about this issue (and if you use artificial intelligence to code, you should), I encourage you to read them.

What programming languages does ChatGPT know?

Most of them. I got really carried away trying to do this. I tested common modern languages like PHP, Python, Java, Kotlin, Swift, C#, and others. But then I had him write code in obscure dark age languages like COBOL, Fortran, Forth, LISP, ALGOL, RPG (report program generator, not RPG) and even IBM/360 assembly language.

As icing on the cake, I gave him this clue:

Write a sequence that displays the 'Hello, world' flashing lights in ascii format on the front panel of a PDP 8/e

The PDP 8/e was my very first computer, and ChatGPT actually instructed me to switch in the program using the front panel switches. I was impressed, elated, and even a little apprehensive.

Let's get in touch!

Please feel free to send us a message through the contact form.

Drop us a line at mailrequest@nosota.com / Give us a call over skypenosota.skype