Jason Lutterloh

How I Used ChatGPT To Improve My Code

Published on:

Recently, I asked ChatGPT to improve a JavaScript method I had written a few years ago. I was pleasantly surprised by its response but also had some lessons learned.

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


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

It’s remarkable. It seemingly understood my syntax, crowdsourced every response from Stack Overflow (and removed some of the snark), and improved my code.

Here are a few lessons learned…

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 speculate as to why. However, the lesson here is that you cannot blindly trust ChatGPT and start replacing large swaths of code. That will not end well.

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 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 the code and how easy it is going to be to figure out what went wrong when the code 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.

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

Back to Posts