Let ChatGPT Code for You: Essential Practices for Coding, Testing, and Documentation

Nobu C. Shirai, PhD
3 min readSep 10, 2024

--

When working with large language models (LLMs) such as ChatGPT for programming and refactoring tasks, it’s crucial to ensure the resulting code aligns with your goals. Although absolute correctness is never guaranteed, I have compiled some techniques to help preserve accuracy. The key is to make use of ChatGPT’s abilities not only for coding but also for checking the validity of the generated code.

Checking Executability and Input/Output

A key step in program development is to continuously verify the code’s ability to execute, which is quite distinct from natural language generation. Verifying executability is the essential baseline check you should perform. If an error occurs, either resolve it yourself or share the error information with ChatGPT. Then, check if the program becomes executable again. For programs with input and output, ensure that the actual output matches the expected output based on the provided input.

Suggesting Program Names

When creating a program with ChatGPT, it’s helpful to request a name suggestion at the same time. In most cases, it provides appropriate name suggestions. The name of a program should reflect its purpose and functionality, so make sure the suggested name is a good fit.

Generating Help Options

Adding a help option (`-h`) to your program allows ChatGPT to clearly explain its purpose and how to use it. Ensure that the help message accurately reflects the intended functionality. Providing command-line options allows users to request specific features more easily, giving them greater flexibility. Referring to specific libraries or functions, such as `argparse` for Python or `getopts` in Shell scripting, often yields effective outcomes.

Generating README.md

When starting a new repository or program, it’s important to create a README.md file that outlines its purpose and usage. However, writing a README.md can take time, and it requires updates whenever the program changes. ChatGPT can assist in generating or updating the README.md. Be sure to review the generated or edited README.md to confirm it accurately reflects your project’s functionality.

Reviewing VSCode Edit History

Visual Studio Code (VSCode) offers Git integration, allowing you to easily review code changes since the last commit. This feature helps track the modification history of your project. When you open a Git-tracked file in VSCode and make edits, the changes are visually highlighted: new lines appear in green, deleted lines in red, and modified lines in blue. This feature is particularly useful when working with ChatGPT for refactoring or adding new functionalities, as it allows you to easily track and understand which parts of the code have been generated or altered.

Generating Docstrings

Docstrings are annotations that explain the purpose and behavior of functions or classes. In VSCode, hovering over a function or class shows its docstring automatically, which is particularly useful for custom code. ChatGPT can generate these for you efficiently as well.

Generating Type Hints

Type hints clearly define the expected types for function parameters and return values. Introduced in Python 3.5, they improve code readability and reduce potential errors. You can request ChatGPT to help generate them for you.

Developing Test Programs

When you have ChatGPT create a program, it’s also a good idea to have it create a test program for that program. Having a test program allows you to understand and verify the program’s functionality from another perspective. Furthermore, if you have ChatGPT make further updates, you can check whether the changes are correct by running the test program. This approach can help compensate for the uncertainty of ChatGPT’s outputs, thereby improving program quality. The idea of “using ChatGPT to check its own work” is interesting.

Example Prompts

Here is an example prompt that integrates the various points mentioned above. This prompt serves as a postscript after outlining the contents of the program you’re developing.

I would like to add some additional specifications below.
- Add a help option using `argparse`.
- Add docstrings and type hints.
- Suggest some potential filenames for the script.
- Ask "(y)es/(n)o" before overwriting an existing file (default: no).
- Add the following shebang.
```
#!/usr/bin/env python3
```
Also, please generate a test code for each function of the generated code using pytest.

Visit the GitHub pages below to see examples of prompts and the generated Python scripts.

You can find the original Japanese article by the same author at: https://zenn.dev/nobucshirai/articles/chatgpt_programming.

--

--

Nobu C. Shirai, PhD
0 Followers

PhD in Physics/Assistant Professor at Mie University, Japan/LinkedIn: www.linkedin.com/in/nobu-c-shirai-024386300/