How to use ChatGPT to write code: What it can and can't do for you

Updated 5 months ago on June 08, 2024

One of the most intriguing things to discover about ChatGPT is that he can write some pretty good code. I first tested this last year when I asked him to write a WordPress plugin that my wife could use on her website. ChatGPT did a great job, but it was a very simple project.

How to use ChatGPT to write: Resume | Excel Formulas | Essays | Cover Letters

So, how can you use ChatGPT to write code as part of your daily coding practice? Here's a brief description:

  1. ChatGPT can produce both useful and unusable code. For best results, give clear and detailed hints.
  2. ChatGPT helps you solve specific coding challenges, rather than building a full-blown app from scratch.
  3. Use ChatGPT to find and select appropriate coding libraries for specific purposes, and participate in an interactive discussion to narrow down your options.
  4. Be careful about the ownership of AI-generated code and always check its reliability. You should not blindly trust the generated result.
  5. Treat your interaction with ChatGPT as a conversation. Refine your questions based on the AI's answers to get closer to the desired outcome.

Now let's explore ChatGPT in more detail.

What types of coding does ChatGPT do well with?

There are two important facts about ChatGPT and coding. The first is that AI 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 unusable garbage.

I realized this with great difficulty. After I finished working on the WordPress plugin, I decided to see how far ChatGPT could take it.

I wrote a very thorough prompt for a Mac application, including a detailed description of UI elements, interactions, what the settings should provide, how they would work, and so on. I then submitted that prompt to ChatGPT.

ChatGPT responded with a stream of text and code. Then he stopped in the middle of the code. When I asked him to continue, he threw up more code and text. I requested continuation after continuation, and he threw up 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 left out the basic operations I asked for, leaving simple 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 write code to create specific procedures and perform specific tasks. Don't ask for an application that runs on the menu bar. But if you ask ChatGPT to create a procedure to put a menu on the menu bar and then insert it into your project, the tool will do quite well.

Also, keep in mind that while ChatGPT seems to have a tremendous amount of domain-specific knowledge (and often does), it lacks wisdom. So the tool can write code, but it won't 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 a few specific steps on how ChatGPT can help you write code.

How to use ChatGPT to write code

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

Imagine that you pay a human programmer to perform this task. Do you give him enough information to enable him to complete your assignment? Or are you being too vague, and the person you are paying is likely to ask questions or give out 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 a short summary in return, well thought out and appropriate. As input, I'll provide the URL of the web page. As output, I will get a block of text with the summary.

Continuing with the example above, a very 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 discovery.

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

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

I started with a tip that was designed to give me information about which libraries could provide the functionality I needed. A library (for those who aren't programmers) is a set of code that a programmer can access that does most of the 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 looking at blocks of code written by other people that summarize the text. Here is my first clue:

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 paying attention to ads or embedded content.

This tip 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 there any free ones among them?

ChatGPT backed up with a response. Here's what it stated: "Yes, all ten artificial intelligence 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 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 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 I use with PHP?

It returned three libraries, but I wasn't sure what each one was doing. So, one more 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 would be best?

The answer I received was clear and promising: "Sumy is specifically designed to summarize text, that is, to create a summary that gathers the most important information from a piece of text." So, it was time to see what using Sumy with PHP involves. 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 tips into your ChatGPT instance. Note that in the first step, I decided which software module I wanted 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 may not seem like programming, but I assure you that it is exactly that. Programming is not just lines of code on a page. Programming is figuring out how to integrate all the different resources and systems together, and how to communicate with all the different components of your solution. This is where ChatGPT helped me do my integration analysis.

By the way, I was wondering if Google's Gemini (formerly Bard) AI could help in the same direction. Gemini can't write code, but compared to ChatGPT's answers, it provided some additional insights into the scheduling aspect of programming. So feel free to use a few tools to get the answers you need. Here's that story: Gemini vs. ChatGPT: Can Gemini help you write code? Since I wrote this article, Google has added some coding capabilities to Gemini, but they aren't that great. You can read about it here: I tested Google Gemini's new coding abilities. It didn't go very well. And more recently, I dug into Gemini Advanced. It still fails many of the tests.

What follows is coding.

Okay, let's pause for a moment. This article is called "How to use ChatGPT to write code." And that's what it's going to be. But what we're really asking ChatGPT to do is to write sample code.

Let's be clear: 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 has already been written. Terrible, in the sense that it doesn't do it. So to get new code, you'll 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 correct it.

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

But that doesn't mean that the example code is useless - it's 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 example.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:

Inside summarize_article, get 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 or embedded content, and summarize it in about 50 words. Make sure the summary consists of complete sentences. If necessary, you can go beyond 50 words to 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 provided an approximate word count to create boundaries for what I wanted as a summary. A later version of the program may accept this number as a parameter. At the end, I specified what I wanted to get as a result:

When processing is complete, code summarize_article so that it returns the summary as plain text.

The resulting code is pretty simple. ChatGPT accessed another library (Goose) to get the content of the article. It then passed that summary to Sumy with a 50 word limit, and then returned the result. But once the basics are written, all that's left to program is to go back to it and add changes, customize what is passed to the two libraries, and get the results.

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). The tool may reduce the need for entry-level programmers, but at its 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 are few programming projects he can do on his own - at least right now. And in 2030? Who knows.

How do I get answers to my coding questions 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, "the thing we were most concerned about was factuality, because the model likes to make things up."

Is the code generated by ChatGPT guaranteed to be error free?

Hell no! But you also can't trust code that human programmers write. I certainly don't trust any code I write myself. The code comes out of the creation process incredibly imperfect. There are always bugs. You have to test, test, and test again before you submit it. Then alpha testing with a few select victims. Then beta testing with a wider range of users. Even after all that, there will be bugs. Just because AI plays coding doesn't mean it can produce bug-free code. Don't trust it. Always test it. And you still won't get completely bug-free code. That's the nature of the universe.

How detailed should my description of the programming problem be when accessing ChatGPT?

Details. Look at it this way: the more you leave room for interpretation, the more the AI will go its own way. When I give ChatGPT hints to help me program, I imagine I'm giving a programming assignment to one of my students or someone who works for me. Have I given that person enough details to be able to create a first draft, or will they have to ask me a bunch of additional questions? Worse yet, if the person has so little guidance that they go completely in the wrong direction? Don't be lazy. ChatGPT can save you hours or even days of programming (I did), but only if you give him useful instructions.

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

As it turns out, case law does not yet provide a definitive answer to this question. The US, Canada and the UK require that what is copyrighted be created by human hands, so code generated by an artificial intelligence tool cannot be copyrighted. 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 AI to code, you should), I recommend reading them.

  • Who owns the code? If ChatGPT artificial intelligence helps you write your app, does it belong to you?
  • If you are using code created by artificial intelligence, what is your level of responsibility?
  • The burning question: Who owns the code, images, and narratives created by AI?

What programming languages does ChatGPT know?

Most of them. I tested common modern languages like PHP, Python, Java, Kotlin, Swift, C# and others. But then I asked the tool to 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 'Hello, world' in ascii format with flashing lights 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 a little apprehensive.

Can ChatGPT help me with my data analysis and visualization tasks?

Yes, and a lot of it can be done without code. Check out my article on this topic: The moment I realized ChatGPT Plus was the gamechanger for my business.

I also did an article on creating graphs and tables: How to Use ChatGPT to Create Graphs and Tables

But here's where the fun part comes in. In the article above, I asked ChatGPT Plus to "Make a histogram of the top five cities in the world by population," and it did. But do you want the code? Try asking:

Make a bar graph of the top five cities in the world by population in Swift. Take population data from the web. Remember to include all necessary libraries.

By adding "in Swift", you specify the programming language. By specifying where the data comes from and forcing ChatGPT Plus to include libraries, it knows what other resources the program needs. That's why programming with AI requires you to know about programming. But if you have a knack for it, that's great. Because three sentences can give you a nice chunk of annotated code. Pretty cool, right?

How does ChatGPT handle differences between dialects and implementations of a particular programming language?

We don't have exact details about this issue from OpenAI, but our understanding of how ChatGPT learning happens may shed some light on the matter. Keep in mind that programming language dialects and implementations (and their little quirks) change much faster than the language itself. This makes it harder for ChatGPT (and many programming professionals) to keep up with the times.

So I would go with those two assumptions:

  1. The more recent the dialectical change, the less likely it is that ChatGPT is aware of it, and
  2. The more popular a language is in general, the more training data it has received, and therefore the more accurate it will be.

What's the bottom line? ChatGPT can be a very useful tool. Just don't ascribe superpowers to it. Nevertheless.

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