ChatGPT for Coding Tasks: Best Practices

As software engineers, we are either excited or worried for the prospect of ChatGPT automating our jobs.

Maybe you have tried using ChatGPT for writing code, but it hasn’t worked out as well as you hoped. Or maybe you are just getting started and want to make sure you are using ChatGPT effectively.

The question boils down to: “How to use ChatGPT for coding?

I have been using ChatGPT for coding tasks for several months and replaced about 80% of my coding tasks with ChatGPT. Here are some best practices I have learned along the way.

Use ChatGPT Classic

With recent reveal of the ChatGPT system prompt in this tweet, people are realizing that ChatGPT has a lot of extra instructions built into the system prompt:

These extra instructions allow ChatGPT to offer additional capabilities such as image generation and web browsing. However, it is at the expense of taking up previous token limit for the entire conversation, and affecting how ChatGPT responds to instructions given by the user.

Luckily, OpenAI has provided a solution to this problem: ChatGPT Classic.

ChatGPT Classic is a “classic” version of ChatGPT that does not have the extra system prompt instructions, and it is offered as one of the official “GPTs” by OpenAI.

ChatGPT Classic uses GPT-4 in the backend, so you can expect the same quality of responses as ChatGPT.

You can find ChatGPT Classic in the “Explore GPTs” tab or click here.

There are two advantages of using ChatGPT Classic with less system prompt:

  1. By reducing the system prompt, you can provide more context and instructions for the task without being cutoff by the token limit. This helps ChatGPT to provide more accurate and relevant code suggestions.
  2. Less system prompt also means your instructions will not be affected by the extra instructions in the system prompt, so ChatGPT will be more likely to follow your instructions.

Be Specific

When approaching coding tasks with ChatGPT, be specific about the task.

Just like you would with a human developer, ChatGPT needs clear instructions to provide accurate code suggestions.

Here is table of examples:

Instead of Use
convert string to date in JavaScript convert string in ‘%Y-%m-%d’ format to Date object in JavaScript
write an SQL script write a MySQL script
add a new route using Next.js add a new route using Next.js App Router
logging in Python logging to external file in Python Flask framework

Instruct, Don’t Ask

It helps to be assertive in your requests. Instead of asking ChatGPT how to do something, instruct it to do the task.

Here are some examples:

Instead of Use
How to write a MySQL script to do X Write a MySQL script to do X
How to declare a environmental variable in a Dockerfile Declare environmental variable X as Y in a Dockerfile
How to convert an image into a base64 string in Python Convert an image into a base64 string in Python given a file path variable image_path

This might make you feel like you are being rude, but it’s actually more efficient.

Telling ChatGPT exactly what you want makes the response more accurate and succinct, saving precious time for both you and ChatGPT.

You can always thank ChatGPT as a polite gesture after it has provided the code.

Provide Examples

When possible, provide examples or snippets of code to help with a part of the task.

For example, you want to write a complex script which involves conversion of date format, and you want it to follow an existing implementation, you can provide the existing code as a reference, even if it cannot be directly reused verbatim.

Here is an sample prompt to illustrate this:

Write an SQL procedure to migrate data from database A, table ABC to database B, table XYZ.

// other complex requirements
// ...

Use the following code as reference to convert the date format:

```sql
SET @formattedDate =
    CASE
        WHEN LENGTH(Date) > 10
            THEN DATE_FORMAT(STR_TO_DATE(Date, '%d/%m/%Y %h:%i:%s %p'), '%Y-%m-%d')
        ELSE DATE_FORMAT(STR_TO_DATE(Date, '%d/%m/%Y'), '%Y-%m-%d')
    END;

```

Formatting Instructions

To ensure the code ChatGPT generates is immediately usable, provide clear formatting instructions.

Here’s what has worked for me:

Instructions for the output format:
- Output code without descriptions, unless it is important.
- Minimize prose, comments and empty lines.
- Only show the relevant code.
- Make it easy to copy and paste.
- Consider other possibilities to achieve the result, do not be limited by the prompt.

Relevant Code Context

For those working within an existing codebase, context is crucial.

You need to provide the code that needs to be modified, or the code that the new code will interact with.

However, be mindful of the token limit, as GPT-4 on ChatGPT has a token limit of 8192, and its system prompt actually takes up 1700 tokens according to this tweet.

It is important to provide just the relevant code context, and not the entire codebase.

For example, if a file has 1000 lines of code and 10 different functions, but the task is to modify only one function, then provide only the code for that function. Wrap the relevant code context in triple backticks and provide them as part of the prompt.

You might find it helpful to break up large files into smaller, more manageable pieces (less than 200 lines is good). This will help you to provide the relevant code context easily without hitting the token limit.

Prompt Structure

When crafting prompts, it’s important to structure them in a way that’s easy for ChatGPT to understand.

What I find helpful is to separate the task instructions from the code references. You can either put the references at the end of the prompt or at the start. I didn’t notice any difference in the quality of the response.

Here is a sample prompt structure with references at the end:

  • Task Description
  • Formatting Instructions
  • Sample Code Snippets (if any)
  • Existing Code Context

Here’s sample complete prompt:

You are tasked to implement a feature. Instructions are as follows:

Display the current version on settings page and add a button to download the latest version.

The version api is provided in `route.ts` at https://example.com/api/version

Do the network request without external libraries.

Instructions for the output format:
- Output code without descriptions, unless it is important.
- Minimize prose, comments and empty lines.
- Only show the relevant code.
- Make it easy to copy and paste.
- Consider other possibilities to achieve the result, do not be limited by the prompt.

```SettingsPage.vue
// source code for SettingsPage.vue
```

```route.ts
// source code for route.ts
```

Follow-up Changes

Keeping ChatGPT updated with your latest code is important for making follow-up changes.

It’s tempting to tweak code outside of ChatGPT, but remember, these changes won’t be visible in later sessions. If you need to make more changes via ChatGPT, you might have to redo the work you did outside of it.

To avoid redoing work, update ChatGPT with any modifications or directly make changes through it.

Alternatively, you can provide the updated code in a new session and ask follow-up questions.

Streamlining the Process

After using ChatGPT for coding for a few months, I have developed a custom workflow to help streamline the process to cut down the number of manual steps.

It was simple script that combines task description, formatting instructions and source code snippets into a complete prompt, which I can then copy-paste into ChatGPT web UI.

Then I realized that others might benefit from it too. So I built 16x Prompt, a desktop app that helps you generate prompts for coding tasks easily.

It offers a structured interface for crafting prompts, allowing you to input tasks and select source code files as code context efficiently.

Formatting instructions, token limit counter, and a final prompt preview are also included.

You copy the final prompt to paste into ChatGPT web interface, or send directly to GPT-4 via the OpenAI API.

Are you ready to become a professional ChatGPT Prompt Software Engineer?

Download 16x Prompt for free


Subscribe to receive latest updates