How to write better code with ChatGPT

chatgpt coding
Image source: 123RF (with modifications)

Generating software code is one of the powerful applications of ChatGPT and other instruction-following large language models (LLM). Given the right prompt, LLMs can produce code that could have otherwise taken hours to write.

However, LLMs cannot do the all the work of programmers. They can’t break down complex problems, think about logic and structure, and create multi-layered solutions. They work one token at a time, predicting the next piece of code that is likely to follow the user’s prompt and their current output.

Here are four tips that will help you make the best use of the impressive coding abilities of ChatGPT while avoiding its pitfalls.

Don’t trust ChatGPT if you can’t verify

One of the defining characteristics of LLMs like ChatGPT is their authoritative voice. They will always reply with confidence, even when their output is nonsense. On several occasions, ChatGPT has answered my questions with convincing but false answers.

My rule of thumb with ChatGPT is to only use it in topics that I fully understand and can verify. For example, I would not use it to write an explainer on quantum physics, because I don’t know enough about the subject. But ChatGPT could help me write a fun essay on machine learning basics, because I can fully check and correct its output.

Likewise, when using ChatGPT to generate code, only trust it with tasks that you can fully verify. ChatGPT can write code that doesn’t work—or worse, which works but is insecure. I consider it as an automation tool that can do the laborious work that would take me a long time to write or required multiple visits to documentation pages or online forums like Stack Overflow. For example, you can ask it to write a sort algorithm, the code to launch a web server in Python, a SQL query from your database schema, or a data visualization command in Matplotlib.

source code

Iterate one chunk at a time

LLMs tend to struggle with tasks that require reasoning and step-by-step planning. Therefore, don’t expect ChatGPT to successfully write a full program or a complicated chunk of code for you. However, this does not mean that ChatGPT can’t assist you in complex programming tasks. If you provide it with a simple task (such as the ones mentioned above), it will have a much higher chance of getting it right.

Break down your task into smaller steps and prompt ChatGPT one step at a time. A successful approach is to start by giving ChatGPT a step-by-step outline of the logic of the program you want to write.

This helps prime the model for the larger task. Then start by prompting the LLM one step at a time. In a nutshell, you do the reasoning, ChatGPT does the work. (Incidentally, this approach of starting with an outline and working through it step by step is also successful for other tasks, such as writing some types of articles.)

If you don’t have a clear vision of what the step-by-step process will be, you can get help from ChatGPT itself. Start the coding session by prompting the LLM to generate a list of steps to accomplish the task. Then correct the outline if needed and start prompt it to generate the code for the first step.

Provide feedback to ChatGPT

You can’t expect ChatGPT to provide clean, secure, and working code every step of the way. As you review its code and enter it into your IDE, you’ll make corrections and tweaks. When you do so, it is good practice to provide the corrected code as feedback to ChatGPT, along with explanations if applicable.

One thing to note is that ChatGPT is very sensitive to context and its behavior can change based on the chat history. You can use this to your advantage, which is why providing feedback and corrected code snippets can be very helpful.

For example, you can say, “This is how I changed the code you provided: [insert corrected code here]. Try to do [insert behavior correction] moving forward.” This can help steer ChatGPT in the right direction and avoid repeating its mistake when answering future prompts. (Again, I’ve also had success using this feedback approach with ChatGPT in other tasks, including writing articles.)

Sometimes you can use ChatGPT to get feedback on its own code. Try opening a separate chat session, in which you provide ChatGPT with the code it had generated and ask it to improve it or correct it. Sometimes, it produces interesting results and new directions to explore.

Source code on a monitor

Clean ChatGPT’s context

If you’re working on an especially large task, your chat history can become very long, especially if you do a lot of back and forth with ChatGPT. Depending on the model you are using, the LLM might run out of context memory. The free version of ChatGPT has a memory of 4,000 tokens. (For language tasks, 100 tokens cover around 75 words. For programming it is usually a lot less.)

One successful trick is to occasionally clean your context. For this, you can start a fresh chat session with a prompt in which you provide ChatGPT with the outline of the task, the steps you have so far accomplished, the code you have so far generated, and some of the general guidelines you want it to follow. Then you tell it to continue from the next step. By removing the clutter from previous interactions with the LLM, you provide a much cleaner context and improve the accuracy of the code that the model generates.

LLMs are changing programming

The field of LLM-aided programming is changing rapidly. For example, ChatGPT Plus now has several plugins that can assist programmers beyond predicting code. Other LLM-based coding tools such as GitHub Copilot and Amazon CodeWhisperer are helping developers increase their speed and productivity impressively. Retrieval augmentation techniques can enable programmers to customize the behavior of LLMs to their own proprietary code base or libraries that were not included in the training dataset. We are still discovering what LLMs can do for programmers.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.