Practical prompts to build faster with ChatGPT
Most programming tasks can be done much faster by asking ChatGPT first. Here are prompts I've found useful.
I’ll assume you know what ChatGPT is. If you don’t, sign up and try it here.
I spent this weekend exploring ways ChatGPT can help you build software faster. My conclusion is that every software engineer, in addition to using Github’s Copilot, should be adding this to their toolkit immediately.
It makes bootstrapping code fast, refactoring fast, testing fast, documentation fast, understanding code fast. It is an incredible multiplier, especially in the hands of someone experienced.
The following are some recipes that work well on codebases I tested with. I’ve omitted the sample responses, instead I encourage you try each of them on a codebase you are familiar with, perhaps one that could use some tests, or you have wanted to refactor for some time.
Prompts to try on your codebase
Since ChatGPT does not yet offer a live internet connection, you generally need to paste in source code. Here’s a convenient command to copy all code in the current directory recursively. Don’t consume your `.env` files or other secrets.
# copy source into
$ find ./src -type f -exec cat {} + | pbcopy
# or if no pbcopy
$ find ./src -type f -exec cat {} + > allsrc.txtHave it help you rapidly understand code:
What does this code do?Summarize the main things this code doesAdd short inline comments describing what $function doesExplain why this component needs a propExplain why `name` is validated that way
Have it document code for you:
Add comments to the non trivial parts of the codeAdd a jsdoc to each of the functions
Have it write testing for you. You can specify to use certain libraries. It knows most.
Write tests for this codeWrite tests for this code using jestWrite integration tests for the code
Have it refactor for you. Again you can generally ask it to use a library of choice.
Rewrite this but with css modulesRewrite this but in typescriptRewrite this express app in fastifyRefactor this code into reusable functions
Have it optimize your code
How can I refactor this code? Give examples with diffs.Show me how I can make this solidity code more gas efficient
Bootstrap your code.
Write a rest api that allows users to sign up and follow topics of interest. It should use the following tools: next.js and next.js auth, prisma with postgres, typescript, and zod for validation.Write a form in typescript and react-hook-form that can use this endpoint. It should have client side validation with field level errors and understand the previously server side errors.
Do database things for you
Given the following schema, write a query that shows active users week over week.Diagram this schema in plantuml
All of these questions can be refined, for example:
Give me a short summary of the changes I should make.Show me short code snippets of the changes in the form of a diffShow me just the functionsShow me the new implementation for $functionAdd short comments in the code to explain what it’s doing
ChatGPT has limits on answer length, you can get around it by:
Show me the rest of the answer, orShow me the code starting from function $function
If ChatGPT won’t answer, you can generally change the phrasing of your question with follow ups. Making questions more closed works well.
Closing Remarks
Anecdotally, most of the outputs ChatGPT generates are very good. They’re not always correct and there are obvious caveats for each command, but it doesn’t matter. It’s often easier and faster to recognize something wrong than it is to write it from scratch. You will save an immense amount of time doing small edits and verifying as oppose to writing from scratch. It feels very similar to reviewing someone’s code who is new to a team or codebase.
Run it on your own source and see for yourself.
