• GPT Hacks
  • Posts
  • Best practices when coding with ChatGPT

Best practices when coding with ChatGPT

Get the most out of ChatGPT when coding and avoid common pitfalls

Together with

Hey — It’s Hussein 👋

It’s been a while… I know! I’ve been working on a new project that has taken up much of my free time. I will get into a better rhythm of writing more articles regularly soon.

The good news is that my new project gave me the materials I needed to write this article on best practices when coding with ChatGPT.

Let’s get into it.

The ISO 27001 Compliance Checklist

Are you building a business? Achieving ISO 27001 compliance can help you win bigger deals, enter new markets, and prove security practices to customers anywhere in the world — but it can also cost you real time and money.

Download Vanta’s ISO 27001 checklist to learn how to successfully implement an Information Security Management System (ISMS) according to the standard, prepare for an independent audit of your ISMS to obtain ISO 27001 certification, and streamline the process with automation. 

Out of the box, ChatGPT does a pretty good job at handling coding requests. But it makes mistakes, requires multiple rounds of requests, and doesn’t always give you the results you need right away.

By using these best practices, you’ll decrease the mistakes and time it takes to get a usable output from ChatGPT while improving code quality.

Structure of a ChatGPT Coding Prompt

There are four parts to an effective coding prompt.

Part I - Be specific

This applies to non-coding-related requests as well. When working with ChatGPT, the more specific you are, the better. With coding requests, that’s even more important as the code you get is heavily influenced by your requests for obvious reasons.

In your prompt, in addition to your direct request, include the context of why this feature is important for your project and describe how this code interacts with other parts of your project.

Part II - Include relevant code

Whether you are fixing a bug or adding a new feature, you’ll need to include relevant code in your prompt. When you do, make sure you enclose the code with starting and ending brackets so ChatGPT does not have to think too hard to figure out where the code starts and ends. You can also include code from multiple files if relevant. Also include that path and name of the file the code is coming from, this gives ChatGPT more context to work with.

✨ Pro Tip: if you tend to paste the contents of an entire code file often, add a comment with your file name to the top of your files. This way, you won’t have to retype the file name each time. 

[Code start]
// file path / file name with extension
Then follow with the code
[Code ends]

Part III - Include project details

When a software engineer joins a new project, one of the first things they do (the good ones, at least) is to get some background about the product as a whole before dealing with the specific task. Having the context of what the product is meant to do and how the features (or bugs) they are working on relate to the rest of the product is crucial in making sure their work satisfies the requirements.

The second thing they’ll do is understand the overall structure of the code, what languages are being used, what styles, patterns, organization techniques, etc. are being used so that they can follow these with their code.

ChatGPT is no different.

You can’t expect to get good code that you can use without giving those same details in your prompt. You can be as specific as you want or at least cover the basics.

Here is a simple example to get you started:

About my app:
Sus To Dos is a task management app with a playful twist aimed at engaging a younger, internet-savvy audience. It allows users to flag tasks as “sus” when they are time-sensitive or urgent. It also includes typical features you expect in a to-do list app. Because we target a younger audience, we also provide different themes and ways to customize the app to match their style.

Client:
iOS app using Swift and UIKit

Backend:
Node, hosted on AWS, NoSQL database

Integrations:
Stripe for subscriptions, OpenAI for AI that helps teens with their sus tasks

Part IV - Coding style

Include details on coding style, techniques, patterns, etc that you want ChatGPT to follow. For example:

  1. If you always work with specific languages or packages, include those

  2. If you are new and learning, ask for explanations of the code provided

  3. If you are advanced, ask for concise answers that focus on the code with minimal explanation

  4. Favor using existing libraries over creating new functionality

  5. Properly manage errors when needed

  6. Implement logging to facilitate debugging

  7. Ensure the code passes checks from tools like ESLint or Prettier

  8. Use coding best practices

  9. Follow DRY principles

  10. Ask questions to clarify the objectives of the code

  11. Do not introduce security vulnerabilities

  12. Include a note about code security risks in your response

While you work with ChatGPT on coding, you’ll start noticing issues or mistakes in the code it gives you. If you find yourself repeating the same correction back to ChatGPT, add that information to the details of your prompt.

When you put it all together, your prompt should look something like this

In the task details screen, I want to add the ability for kids to comment on their tasks. We will include a list of comments they can choose from. Each comment also has a unique sticker that is included with the comment. Can you help add this functionality to my app? The relevant code is included below.

When providing code, follow these instructions:
- I want concise answers that focus on the code with minimal explanation
- Favor using existing libraries over creating new functionality
- Properly manage errors when needed
- Implement logging to facilitate debugging
- Ensure the code passes checks from tools like ESLint or Prettier
- Use coding best practices
- Follow DRY principles
- Ask questions to clarify the objectives of the code
- Do not introduce security vulnerabilities
- Include a note about code security risks in your response

[Code start]
// file path / file name with extension
Then follow with the code
[Code ends]

About my app:
Sus To Dos is a task management app with a playful twist aimed at engaging a younger, internet savvy audience. It allows users to flag tasks as “sus” when they are time sensitive or urgent. It also includes typical features you expect in a to do list app. Because we target a younger audience, we also provide differernt themes and ways to customize the app to match their style.

Client:
iOS app using Swift and UIKit

Backend:
Node, hosted on AWS, NoSQL database

Integrations:
Stripe for subscriptions, OpenAI for AI that helps teens with their sus tasks

ChatGPT Coding Custom instructions

Note: if you are not familiar with Custom Instructions, read my intro to ChatGPT custom instructions.

I fully expect that in the next few months, OpenAI will introduce the ability to have more than one set of custom instructions. That will give us the ability to maintain a general set of custom instructions and custom instructions for each of our projects.

For now, instead of copying your project background and coding preferences (parts 3 and 4 above) in every prompt, use the custom instructions to store these.

Remember to revisit your custom instructions periodically if you find yourself repeating the same correction to ChatGPT, that’s a good sign that you should add to your custom instructions.

Test thoroughly

We’ve talked about ChatGPT making mistakes before. This is no different, the code you get may or may not work, if it does it may or may not break other things or perform the functionality you even asked for.

It’s crucial that you test thoroughly before publishing your new code.

You can also ask ChatGPT to review the code it gave you and ask if there are potential bugs or issues to watch out for. You can even ask it for a list of test cases you should run.

Do not copy and paste ChatGPT code

It is very tempting to copy the output from ChatGPT and paste it directly into your project. Please do not do this.

Instead, you should type the code into your project while reading the response from ChatGPT. Here’s why:

  1. ChatGPT will sometimes emit some of your existing code. Without realizing it, you’ll delete code that your app still needs to function properly.

  2. Typing the code forces you to read it first, which is a good learning exercise and a good way to find potential bugs introduced by ChatGPT.

  3. ChatGPT will often leave gaps in the code with a comment for you to fill in or to leave from your existing code. If you are not paying attention, you may end up with missing code.

Beware security concerns

Just because the code works doesn’t mean it is secure and should be used in a production environment. ChatGPT is solving your specific request, not making sure the code is not open to security threats.

In the coding preferences above, I included an example about asking ChatGPT to pay attention to security when providing code in its response but that will not always be enough.

Read the code output and check for potential vulnerabilities.

You can also ask ChatGPT to review the code for potential vulnerabilities before using it with this simple prompt:

Review the code below for security concerns and potential vulnerabilities:

[insert code]

By following these best practices when coding with ChatGPT, you’ll quickly get better results.

Happy Coding.

— Hussein ✌️

P.S. If you enjoyed this email, please forward it to a friend. (Want rewards? 🎁 Use your custom link).

P.P.S. If you live in or visit San Diego, sign up here to be notified of tech and founder mixers I organize in the area. Would love to meet you there.

How did you like today's newsletter?

Login or Subscribe to participate in polls.

Join the conversation

or to participate.