Coding with Copilot and ChatGPT

AI | 0 comments

I love coding with AI.

There, I said it. “You’re not a real developer,” I can hear you saying. And no, my profession is not primarily development1, but development definitely makes my job in IT easier.

Here are some tips that will make Copilot and ChatGPT code faster, better and more accurately for you.

Start with a Readme

When you can, start with a readme that explains what your project or script will do, before you write a single line of code. In this project, the readme was the very first file I made. I then pasted it into ChatGPT and asked that ChatGPT write a script to match.

Use examples in the readme: what do you want the syntax to be? Are there code examples of JSON or expected output you want to see? Start with the end in mine, and describe it as much detail as matters to you for the output.

Examples, Specifically

Both Copilot and ChatGPT work very well with examples. If you want to see a specific output, show it an example of what you want. If you were writing an API that exposes an endpoint with user data you might tell ChatGPT something along the lines of:

This is an unfinished endpoint to return user data from this project through a restful API. Clients expect to see JSON responses such as:
{
   "user": "[email protected]",
   "id": "some-guid",
   "phone": "555-123-1234",
   "nickname": "JD",
   "favorite color": "blue"
}

Some attributes are optional, so responses like this are also valid:
{
   "user": "[email protected]",
   "id": "another-guid"
}

Comments Galore

Comments seem to make both Copilot and ChatGPT better so I use them liberally. When ChatGPT gives me output I ask it to comment the code for a “junior developer.” In Python I have it generate docstrings for each function (documentation that describes what the function does).

With Copilot, comments can write your code for you. Write a comment like:

# Regex to separate split emails from domains, then put the domains into a sorted list

and then just wait for the autocomplete to give you the correct code, such as:

pattern = r'@([a-zA-Z0-9.-]+)'
domains = re.findall(pattern, emails)
sorted_domains = sorted(domains)

I also add examples and documentation in comments when I think the reference will help. If I am consuming a JSON API I may write:

def SaveAPIResponse(*args):
"""Takes the json response from Acme API and saves to the database.
A sample response would be:
{
   "user": {
      id": 12345,
      "username": "johndoe",
      "email": "[email protected]",
      "firstName": "John",
      "lastName": "Doe",
      "createdAt": "2024-08-05T22:04:55Z",
      "github": "johndoe"
     }
}
"""

You’ll see the accuracy explode.

One Thing at a Time

For the first couple of responses you can ask ChatGPT to output a lot, a 100+ lines of code isn’t crazy. When you get to troubleshooting, feed it one section at a time.

For instance, in my most recent project I decided to have ChatGPT write unit tests for me — because who likes writing those? They didn’t all work correctly, so I gave it one failing test at a time, correct either the test or the original code, and then went on to the next test.

Even when there is a similar root issue, one at a time is usually better.

Know When to Restart

For a certain amount of time ChatGPT seems to be more accurate with the more information you give it. But at some point in the conversation it seems to suffer from brain fog and stops any critical thinking and gives recycled output2. At this point, start a new chat.

Don’t just pick it up again, start fresh.

Your new chat might start like, “I’m picking up this project from another developer. She was working on adding feature XYZ to the script below. Will you check for errors and then finish the function? <paste the script>”

If relevant, include the readme or other documentation (again). “She left me these notes…” (This is also where it can be helpful to have documentation in comments, such as those above)

Prep Materials with AI

Sometimes you may have a lot of material or code to go through and you don’t want to muddy up the conversation. Just use a separate chat. For example, I may take a large snippet of JSON and ask ChatGPT to write documentation on the structure of it, and then use that documentation in my readme.

I might take a 100+ lines of JSON from something like this, and then ask ChatGPT to “write documentation for this JSON from the provider’s perspective, as through a restful API.” Then I’ll take that output and put it in the readme or the comments.

It keeps the main conversation from getting brain fogged, and should give you some clear output about expectations as well.

  1. If you look at my Github you will see projects such as this WordPress plugin that uses ChatGPT to add alt text to images, a docker/syslog project, a Dehashed command line API client, and more. I’m not alien to programming either. ↩︎
  2. Yes I know ChatGPT doesn’t “think” but I don’t know what to call an. AI that is essentially emulating brain fog… ↩︎

0 Comments

What's your $0.02?

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