Writing entire programs with ChatGPT is not easy.
Too high are the risks for inconsistent style, using the wrong libraries, losing track over longer conversations. So what is the secret to building high-quality, maintainable and effective software with ChatGPT?
This article serves as an introduction to a topic we will examine in depth over the next couple of days: real-world programming with ChatGPT.
Solving longer tasks with ChatGPT: 2 essential skills
Solving longer tasks with ChatGPT requires two fundamental steps:
decomposition: ChatGPT can't just solve any problem that is presented to it in 2 sentences. It is most effective at doing small moves around the ladder of language.
context information: ChatGPT also needs to have all the information required for it to do a rock-solid job, either through its training corpus or by having it explicitly provided in its context window.
This is where your knowledge as a software engineer is crucial: decomposing a problem into steps along with knowing which information is required for that step is a skill that only comes through experience and deep study.
From prototype to CLI tool: 3 steps to get high-quality results
I use the following 3 steps to create useful command-line applications (we will use "docstring", a tool that extracts structured data out of source code documentation string, as an example):
Ask ChatGPT to create a prototype: I will often ask ChatGPT to create a prototype for a small task, and give it minimal guidance. I chose a task small enough (we will come back to what is small enough): "extract docstrings out of a file using a set of regexp". Note how I already decomposed the problem within that first step.
Adapt and refine code: Once I get the first version back, I will rework the code, either by hand or by continuing to prompt ChatGPT with small tasks. When I do hand edits, I paste my changes back into ChatGPT.
Fix bugs, extract methods, clean up datastructures
Ask ChatGPT to create unit tests, docstrings
Incrementally add more functionality. In the docstring case, for example: parse attributes within docstrings, extract a "Scanner" struct, support more languages
Transform into a known pattern: When I'm satisfied with my prototype (this is the stage "docstring" is at, I will transform into a well known application pattern, usually by pasting concise instructions. I usually transform my tools into glazed applications by using these instructions (we will come back to gathering context information and the tools I use soon).
This short excursion into building real-world programs is all you need to get started: decompose and transform, by providing the right context information.