How I Used ChatGPT To Improve My Code

Jason Lutterloh
3 min readApr 21, 2023

--

Recently, I asked ChatGPT to improve a JavaScript method that I had written a few years ago. The response pleasantly surprised me and I figured I’d share a few observations.

First, here is what happened — it analyzed the code, compiled a list of changes it wanted to make, and then gave me the final output with all the changes together. Crazy, right?

Original Request

ChatGPT Prompt — Improve my code

Response

ChatGPT Response (screenshot cut for brevity but it had 2 other suggestions…)

Remarkably, it seemingly understood my syntax, crowd-sourced every response from Stack Overflow (and removed some of the snark), and improved my code.

Here are the few observations…

ChatGPT is a fantastic code reviewer

I have a love-hate relationship with code reviews. They are the fun kind of challenging (when they are not 600 files changed) but they can also be labor-intensive. When I review code, I am primarily looking at how I can break it and how easy it is going to be to figure out what went wrong when it inevitably breaks. I do not care if you used ternary operators or arrow functions; linting can deal with that.

ChatGPT can fill the gap.

When I asked ChatGPT to improve the code, it did so, but it did so with reasons why. That is so crucial. Depending on the team makeup, most code reviews involve a senior dev reviewing a junior dev’s code. A good engineer will explain why to make changes so that the person being reviewed can learn and get better. Having a tool available that an engineer can use to get immediate feedback and coaching before putting it in front of another engineer will save everyone time. If OpenAI can solve the data security issues by allowing ChatGPT access to proprietary code, we would be in business.

ChatGPT can write your unit tests

I do not know of a developer who gets excited about unit tests. We get excited when they catch a bug or when they all pass but writing them is tedious. Enter ChatGPT. I fed it a JavaScript method and said, “write me unit tests”. Within seconds, it spit out happy path scenarios, null checks, and undefined checks — with proper naming conventions. Having the unit tests written “auto-magically” was amazing enough but the fact that it named variables based on my method name, was the icing on the cake.

I do not think I can overstate how incredible this really is. The amount of time my developers and I write unit tests is far greater than I would like to share with the business. It is value-added but it takes a long time. If we could use ChatGPT to handle the bulk of our unit tests and save our energy for the more complicated scenarios — that’s game changing.

ChatGPT might struggle with regex (among other things), so do not be too quick to replace your code

I asked it to improve a method that used regex to format a title. I had already written quite a few unit tests for this method and was getting the output I expected, so I replaced my method with ChatGPT’s and ran the unit tests.

They failed.

There is a lot at play here and many reasons why it could have failed so I will not even begin to speculate but the lesson here is that you cannot blindly trust ChatGPT to start replacing large swaths of code. That will not end well.

All that to say, utilizing ChatGPT as a tool can greatly improve a developer’s life. These are just a sampling of ideas and I am sure that others have tried these as well as hundreds of other experiments. Thanks for reading.

--

--

Jason Lutterloh
Jason Lutterloh

Written by Jason Lutterloh

0 Followers

Jason Lutterloh is a full-stack software engineer.

No responses yet