Using ChatGPT to Port a Python Package to JavaScript

March 19, 2023

Background: I had written this Python package, and realized that I needed a version written in JavaScript.

I used ChatGPT for the following purposes: translating the Python code to JavaScript; creating a test suite from the code; creating a README with API documentation and examples from the code; creating package.json and typescript configuration files; and asking for naming advice for the function and filenames.

ChatGPT did a first pass on translating the code. (This was good enough that I only needed a few minutes of cleanup.) Then it wrote the initial README, the test files, and the package.json and tsconfig.js files. (Ditto.)

(Spoiler: here is the final JavaScript version.)

I didn’t time the amount of work this took, but I would estimate ChatGPT saved me 80% of the effort, and 90% of the drudgery. Most of the remaining work was adding some material to the README, and adding a feature that I had omitted from the Python version that I gave to ChatGPT. (I decided to port this feature without ChatGPT, at least in part because I wanted a change of pace to coding from prompt whispering.)

The responses shown were the first responses. Nothing was cherry-picked.

Initial Translation

image

[Here, I posted this version of the code.]

😓
The code in that file doesn’t meet my standards for code readability, or something that I would contribute to a team. As a side project of a side project, I was just using this library as an excuse to write in what I think of as an old-school Lisp-y style. Excuses, excuses…
The ChatGPT response. It goes on; this is only the first page.
The ChatGPT response. It goes on; this is only the first page.
The end of the response.
The end of the response.

Notes:

  • “Note that I made a few changes” is ChatGPT, not me.
  • The import … from "typed-python" is non-idiomatic (and probably doesn’t work). Also, @dataclass is both unnecessary and invalid. I edited these out by hand. An alternative would have been to refine the prompt, or include a follow-up directive. This was only partly an exploration of ChatGPT, though, and I was eager to get back to coding.

Test Cases

image

Note that ChatGPT is generating these from the source code. It only has access to the function names and implementations. I didn’t enter the Python README or test cases, and these didn’t exist on the web until a few days before this chat.

Generating a README

ChatGPT generated this from the source. It also had access to the test cases that it previously generated, and which it therefore could have used as a form of loud thinking.

image
image

Generating Configuration Files

Here’s an area that can take a while, if I’m new to a framework or language, or have been away from it for a while.

image
image

Naming Advice

When I return to a programming language or framework after a break, these are some questions I get hung up on. (It’s an internal form of bikeshedding.) Asking ChatGPT can a sophisticated form of talking to the rubber duck, only about more than just debugging.

image
image

image