The fact that ChatGPT can create reasonably nice code is one of the more exciting things we’ve learned about it. When I asked it to create a WordPress plugin that my wife could use on her website in February, I put this to the test. It did a good job, although the assignment was extremely straightforward.
How can ChatGPT be used to write code as a regular part of your coding routine? That’s what this article will look at.
What coding disciplines does ChatGPT excel in?
There are two crucial details about code and ChatGPT. The first is the fact that it is capable of producing usable code. The second is that it has the potential to become totally disoriented, disappear down a rabbit hole, chase its own tail, and create wholly useless junk.
I had to learn this the hard way. When the WordPress plugin was complete, I made the decision to see how far ChatGPT could go. I carefully outlined a prompt for a Mac application, with explanations of all the user interface components, interactions, settings that would be available, how they would operate, and other details. I then fed ChatGPT with it.
The only response from ChatGPT was a barrage of text and code. Then, mid-code, it halted. It continued to spew out text and code after I requested it to. I repeatedly pressed the continue button, and more and more code was output. All of it, though, was useless. When I carefully examined the code generated, it left out the major actions I had asked and replaced them with basic text descriptions that stated, “Programme logic goes here.” It also failed to specify where the code should go or how to build the project.
I realised after conducting numerous tests that ChatGPT will not be able to offer a finished product. This observation has the implication that ChatGPT will not be able to construct you anything if you don’t know how to code.
ChatGPT excels at assisting someone who is already proficient in coding to create particular routines and complete particular tasks. Don’t request an application that uses the menu bar. However, it will work fine if you ask ChatGPT for a procedure to add a menu to the menu bar and then copy that and paste it into your project.
Also keep in mind that despite appearing to have a wealth of domain-specific knowledge (which it frequently does), ChatGPT lacks wisdom. As a result, while it could be able to produce code, it won’t be able to write code that has the intricacies for really niche or intricate issues that call for a wealth of knowledge.
Use ChatGPT to create short algorithms and subroutines, as well as to demonstrate techniques. You can even ask ChatGPT to assist you in segmenting a larger project into smaller parts before asking it for assistance with the coding of individual parts.
In light of this, let’s examine some detailed instructions for using ChatGPT to develop code.
How ChatGPT can help you write code
1. Make your request more specific and focused.
Choosing your questions for ChatGPT during this initial step prevents you from actually asking it anything. Establish the purpose of your function or routine and any learnings you wish to include into your code. Determine the input parameters you’ll supply to your code and the desired output. Next, consider your phrasing for describing it.
Consider hiring a person to do this programming for you. Are you supplying that person with enough details so they can complete your assignment? Or are you being overly unclear, which increases the likelihood that the person you’re paying will either ask you questions or deliver something completely unrelated to what you want?
Here is one instance. Say I want the ability to sum up any online page. I want to feed it content similar to this post and receive a brief, relevant summary in return. I’ll enter a URL for a web page as my input. It is a block of text with a summary as my output.
2. Examine libraries and resources using ChatGPT
Using the aforementioned case as an example, a relatively archaic method of retrieving web page data was to just look for the content inside HTML paragraph tags.
But with the development of AI technologies, it makes more sense to perform an intelligent extract and summary using an AI library. Finding libraries and resources is one area where ChatGPT thrives (and it’s also a location you can easily test to prevent its authoritative-but-wrong behaviour pattern).
The GPT-3 and GPT-4 engines from OpenAI, the company that created ChatGPT, sell API access that will carry out our requests. However, let’s assume for the purposes of this example that we don’t wish to pay transaction fees.
So let’s take a look at interacting with ChatGPT to learn how to use such a tool with a PHP project for free.
I began by posing a question intended to collect information about which libraries might offer the features I required. A library is a set of code a programmer can access that does a lot of the labor-intensive work for a particular purpose (for those of you reading along who aren’t programmers). Finding and selecting the proper libraries is a significant aspect of contemporary programming. So, this is a fantastic place to start.
In this instance, I’m looking at code snippets created by others that will condense text. Here is my first question:
Describe ten different open source AI libraries (and the languages they work with) that I can use to generate a summary of the main core contents of any web page, ignoring any ads or embedded materials.
I got all I wanted from this, including a mention of OpenAI’s services. OpenAI would perform admirably in this situation, however I don’t want to pay for API costs in my fictitious project. I’ll focus the query accordingly.
Are any of these free?
With its response, ChatGPT covered its bases. It read, “Yes, each of these ten AI libraries is open source and cost-free to use. However, some of them can have usage restrictions or charge a fee to access premium functions or information. I then clarified my question in light of that.
Which of these libraries have no usage limits and don’t require any additional payment or licensing
Take note of how conversational this is. I don’t need to ask the original query again. Just like if I had an expert on hand and was looking for clarification, I’m digging deeper. In this instance, ChatGPT offered me eight libraries to choose from, but none of them specified the PHP programming language I intended to use. So, this is the following prompt:
Of those 8 libraries, can I use any with PHP?
I wasn’t sure what each of the three libraries did after they were returned, though. So here’s another query:
What’s the difference between Sumy, Gensim, and NLTK?
I reiterated my use plan because I was still unsure, and then I questioned:
If I want to create summaries of web page news articles, which library would work better?
I received a straightforward and encouraging response: “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 examine the requirements for using it with PHP. I posed the following final query for this section of the project:
Can you explain how to use Sumy from PHP?
You are welcome to follow along on your computer by pasting these directions into your ChatGPT instance. You’ll see that I choose the programme module for which I wanted assistance in step 1 of the process. I then discussed which library to utilise and how to incorporate it into my project with ChatGPT in this stage.
Programming may not seem to be involved, but I can tell you that it is. There’s more to programming than just slapping lines of code on a page. Programming is the process of figuring out how to communicate with and integrate all the many systems and resources that make up your solution. In this case, ChatGPT assisted me with the integration analysis.
By the way, I wondered if Google’s Bard could be of similar assistance. Although Bard cannot really produce code, compared to ChatGPT’s responses, it did provide some more insights into the planning component of programming. Therefore, don’t be afraid to triangulate your results using several tools. This is the tale: ChatGPT vs. Bard: Bard, can you help me code? Google has since given Bard some coding powers. However, they aren’t really that good. Read more about it here: I evaluated Google Bard’s updated coding abilities. It went horribly.
Coding is next.
3. Request sample code writing from ChatGPT
Let’s pause for a while, then, shall we? The title of this article is “How to Use ChatGPT to Write Code.” Although we’re really asking ChatGPT to write sample code, it will happen. Wait. What?
Just to be clear. ChatGPT won’t be able to write your final code unless you’re developing a very short function (like the line sorter/randomizer ChatGPT wrote for my wife). You’ll need to keep up with it first. ChatGPT is awful at editing previously written code. Terrible in the sense that it falls short. You must therefore ask ChatGPT to produce new code if you want fresh code. As I’ve previously discovered, ChatGPT may vary the prompt it provides you even though it’s nearly identical.
ChatGPT can’t maintain your code, or even tweak it.
You must therefore complete it yourself. As we all know, a piece of code is rarely perfect in its initial iteration. Even if you were to expect ChatGPT to produce finished code, you would still only be getting started and would need to finish it, incorporate it into a larger project, test it, improve it, debug it, and so on.
However, that does not imply that the example code is useless. Not at all. Let’s look at a prompt I created based on the earlier-mentioned project. This is the first section:
Write a PHP function called summarize_article.
As input, summarize_article will be passed a URL to an article on a news-related site like ZDNET.com or Reuters.com.
I’m instructing ChatGPT to utilise a particular programming language. In order to help ChatGPT grasp the style of the article, I’m also giving it the input while simultaneously offering two websites as examples. In all honesty, I’m not convinced ChatGPT didn’t disregard that instruction. I’ll then instruct it on how to complete the bulk of the work:
Retrieve the information from the web page specified by the provided URL inside summarize_article. Remove the article’s primary body, ignoring any adverts or embedded content, and condense it into about 50 words using the PHP library Sumy and any additional libraries required. Make sure all of the sentences in the summary are complete. If further words are required to complete the last statement, you may do so.
This is exactly how I would train a worker. That person should be aware that they are not limited to Sumy alone. I wanted them to utilise another tool if they needed to.
I also gave a rough word count in order to set limits for the length of the summary I needed. The routine might accept that number as a parameter in a subsequent version. Finally, I stated what I desired as a result:
Code summarize_article such that it returns the summary as plain text after processing is finished.
The resulting code is quite straightforward. A other library (Goose) was contacted by ChatGPT to acquire the article’s contents. With a word restriction of 50, it then passed that on to Summy before returning the outcome. I’m done now. Once the fundamentals are developed, however, it is only a matter of programming to return and make adjustments, alter the information supplied to the two libraries, and present the results.
There is one noteworthy point. Utilising a URL from after 2021 (when ChatGPT’s dataset expires), the programme built by ChatGPT made a sample call to the routine.