19 Tips for Coding Faster with ChatGPT

19 Tips for Coding Faster with ChatGPT

By now, you've probably already heard that ChatGPT is a pretty big revolution for software developers. I've now done a couple of successful projects with it, both toy projects and for work. Here are a few things I've learned.

Model matters

The free version of ChatGPT is almost useless at writing code. If you pay $20/month, you get access to GPT-4, which is far better. But the best you can do is to use GPT-4 in Code Interpreter mode. This enables a larger context window and a higher-quality underlying model. You don't have to actually use the Code Interpreter functionality, which is intended for Python data analysis -- just turn on Code Interpreter mode and start from there.

Language matters

ChatGPT isn't equally proficient in every language. For more obscure languages like Rust, it likely doesn't have quite enough training data, and the results are often poor. For beginner-friendly programming languages like JavaScript and Python, it will often output beginner-quality code, ignoring current best practices and modern language features. Low-level languages like C are often too verbose to fit much code inside the limited context window. I've found it's pretty good at Go, probably because the majority of Go code in the training data is recent and high-quality.

If you must use it for Python or JavaScript, try to be a bit more specific. Tell it to write Python code with Flask and strict typing, or TypeScript with vue.js. This will encourage it to use higher-quality training examples.

Prompt matters

You probably already know this, but the exact details of your prompt really matter a lot. Use grammatically correct, complete sentences. Specify everything you can think of. This will encourage it to produce high-quality output. I loved this online course on ChatGPT Prompt Engineering For Developers.

One weird trick you can try is asking ChatGPT to improve its own prompt. Just paste in what you are thinking of, and ask it to improve the prompt with additional details. It seems self-referential but it actually works.

Let it ask questions

Before getting started, ask ChatGPT: "Do you have any questions?" It will almost ALWAYS have questions for you. Often they are surprisingly insightful!

Give it a few tries

A bit like a human, ChatGPT is often not great on the first try. If you run the code and get an error, ask it to fix the error. If the code is tangled, ask it to fix it. Be patient. Even allowing for 4 or 5 tries, it's generally faster than a person could do the same work.

Examples help

If the work is at all rote or repetitive, give it a few examples. ChatGPT is GREAT at extending an example to cover other cases. For example, "write a function to check that all fields are valid" will go much better if you show it how to validate ONE field.

Keep it small

ChatGPT still has a very limited context window (either 4K or 8K tokens, or roughly 2000-4000 words). This goes pretty quickly with code. Focus on having it write a few functions at a time, or one small module. As it gets toward the end of the context window, it won't tell you -- instead it will start just forgetting what was at the top.

Paste in headers

Ok, so how do you get ChatGPT to make a coherent larger program if it can only handle a few pages of code at a time? Keep track of the function headers it generates in a separate file. Then, when you're getting to the end of the context, paste them in again to remind it what code it has already generated. Think of it as a very forgetful intern who keeps writing code but forgetting what they have already written after a few minutes.

It's great at unit tests

Of all the things you can use ChatGPT for, unit tests are the absolute most valuable. Michael Feathers, the author of "Working Effectively with Legacy Code," said that the definition of legacy code is anything without unit tests. Now you have a tool that can automatically write unit tests for arbitrary code, quickly and cheaply!

But be careful. Automatically writing unit tests can "freeze in" any bugs that are already present. You need to really understand the original code, and the unit tests that are being generated, to be sure that the process isn't going off the rails.

It's great at code review

Most code review is pointing out common errors by rote. ChatGPT will happily critique your code. It doesn't replace human code review by any means, but it is a great first step to save your code reviewers time.

(But also use any automated tools you can, like black for Python or go vet for Go.)

It's great at translation

ChatGPT is shockingly, absurdly good at translating code from one language to another. Already have your concept working in Java, but need to translate it to Python? ChatGPT is perfect for this type of work.

It's great at data analysis

I mentioned Code Interpreter mode before. In Code Interpreter mode, you get a workspace of around 100MB where you can upload files in CSV or similar formats. ChatGPT can easily, fluently write Python code to answer all kinds of different questions. And like I said, Code Interpreter mode also gives you a more advanced model with a larger context window. It's the same price as GPT-4 -- why not use it all the time?

It's great at comments and documentation

Tired of writing comments? Just ask ChatGPT to do it. It's surprisingly good. Of course, always read them to make sure they make sense.

It's a little TOO good at copying training data

A lot of people have an experience like this:

  1. They try ChatGPT on a made-up problem, like coding a Snake game
  2. It's spectacularly good and works on the first try
  3. Then they try it on a real problem, and it's much less good

The explanation is simple: ChatGPT has seen hundreds, maybe thousands of Snake games in tutorials on the Internet. It hasn't seen anything about your work codebase at all (unless it's open source, of course). So don't expect nearly as good results.

It's terrible at anything visual

ChatGPT doesn't have eyes. It has no idea how colors, shapes, or objects interact. It can't even do simple line art. Don't ask it to design your GUI or make your company web site. It just won't work.

It CAN work with symbolic image formats like Mermaid, Graphviz or PlantUML though, if what you're doing works within those formats.

You'll still need to tweak

Often I've found that I can get the first 50-60% of a project done with ChatGPT fairly quickly, but I just can't convince it to do certain things. The code will still have bugs or features I can't get ChatGPT to output. Set your expectations accordingly.

Slick tooling is emerging

There's a great utility called aider.chat that lets you run ChatGPT in a terminal window. It automatically posts function headers to ChatGPT and asks for diffs, not full output of the entire code. Then it applies those diffs. It works shockingly well! However, since it uses the added-cost ChatGPT API, it gets a bit expensive -- I ran up an $8 bill in an evening of experimenting.

Be careful about privacy

Assume anything you send to ChatGPT is being recorded and potentially used in training the next version. Don't post your company's proprietary information under any circumstances. You could get in a lot of trouble if your company secrets show up in the next update!

A few simple things I've built

In addition to work projects, I've got a few fun things I can share:

A trivia game built completely with ChatGPT (including the questions)

A math game built completely with ChatGPT

Each took only a couple of hours!

What are you building with ChatGPT?

Zhu Liang

Filter & Organize F5bot Alerts with 16x Tracker | Creator of 16x Prompt

7 个月

Aider is great for command line, but the API cost can go up quickly. In order to avoid excessive API cost, you can try 16x Prompt, which is a desktop application that works similarly to Aider, but you can copy paste the final prompt into ChatGPT or Claude 3 directly. This way, you can leverage your monthly subscription for ChatGPT Plus or Claude instead of spending extra for API calls.

Daniel Feldman

Cloud Security Architecture - Open Source - Zero Trust

1 年
回复
Jenna Pederson

Principal Developer Advocate @ AWS

1 年

Great write up Daniel!

Raymond Wang

Software Development Engineer

1 年

Great summary! I have found that ChatGPT in its current iteration isn't very good at generating code to handle Frontend events (very noticeable when dealing with Frontend JS/TS framework). But it can certainly do great things with automated testing (such as cypress) to imitate actual user actions and behaviors.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了