How I Used ChatGPT To Improve My Code
Published on: 2023-04-20 00:00:00 -0500 Jason Lutterloh
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?
(screenshot cut for brevity but it had 2 other suggestions…)
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.
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 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.