X
Innovation

How ChatGPT can rewrite and improve your existing code

We knew this generative AI tool could create code, but did you know ChatGPT also can explain and rewrite your code? Maybe we should call it the Code Whisperer.
Written by David Gewirtz, Senior Contributing Editor
floating-code
David Gewirtz/ZDNET

Over the past few months, we've been exploring ChatGPT's ability to create code. ChatGPT is considered a "generative AI," and creating code from a prompt certainly fits the generative term. We showed how it can create a complete, working WordPress plugin as well as write code in dozens of languages, both mainstream and obscure.

Also: How to use ChatGPT to write code

But in some recent experiments, I became aware of another capability: ChatGPT can read and deconstruct code. We got a hint of this capability when I explored using ChatGPT as a tool to assist with debugging. I fed it a small block of code and an error message, and it was able to show me where I'd gone wrong.

But what I'm going to show you today is how ChatGPT can help you understand and potentially rewrite code. This is a pretty big deal.

Explaining JavaScript

To illustrate this, I'll start with a small chunk of JavaScript code that validates whether the input value is currency. It uses some relatively arcane regular expressions in the first and third lines, which we'll ask it to help us with.

original-code
Screenshot by David Gewirtz/ZDNET

Let's start by having ChatGPT explain what the code does. This is a very powerful capability, especially if you're inheriting code from another programmer. While there's no guarantee that ChatGPT will be right, this could save you hours by letting you get a feel for how code works. I used the prompt:

Explain the following code

and I pasted in the code block. Here's what ChatGPT had to say:

explain-code
Screenshot by David Gewirtz/ZDNET

We can do some interesting things with this capability. For example, we can have ChatGPT rewrite code. As an example, I asked ChatGPT:

Rewrite the following line so it doesn't use regular expressions:

return !isNaN(n) && n >= 0 && /^\d+(\.\d{1,2})?$/.test(str);

Also: How to use ChatGPT to create an app

The following answer shows both the power of regular expressions in keeping code concise, and the ability of ChatGPT to take existing code and rewrite it:

rewrite-code
Screenshot by David Gewirtz/ZDNET

Explaining open source C code from a GitHub repo

In this second example, I'm working with Gimp, the open-source graphics editor. We'll be looking at a block of code starting at line 438 that runs in the .PSD import section of the open-source application. Here's that code:

gimp-code
Screenshot by David Gewirtz/ZDNET

Once again, I asked ChatGPT to explain the code, and here's its response:

gimp-explain
Screenshot by David Gewirtz/ZDNET

Where this becomes even more powerful is that you can use ChatGPT to perform language conversions. I asked ChatGPT to rewrite it from the original C to Python:

Rewrite that code in Python and make it as concise as possible.

gimp-python
Screenshot by David Gewirtz/ZDNET

ChatGPT even noted the following after the code: The g_free function from the original code is not included in the Python version since memory management is handled automatically by Python's garbage collector.

Improving existing code

In this example, I fed ChatGPT a section of my code I'm not particularly satisfied with. My code does a WordPress database search and updates certain items. Here's the original code:

ugly-code
Screenshot by David Gewirtz/ZDNET

I asked:

Can you improve the following code

It gave me a new block of code:

improved-code
Screenshot by David Gewirtz/ZDNET

It also gave me an explanation for what it did:

improved-explain
Screenshot by David Gewirtz/ZDNET

There is one oddity here. I ran the same code through the same query twice. The second time, it made one other improvement: The SQL query has been modified to only select the necessary columns (option_id, option_name, and option_value) instead of using SELECT. This can improve performance by reducing unnecessary data retrieval.

Here's that block of code:

oddity
Screenshot by David Gewirtz/ZDNET

This second version of the code actually contains the most valuable change, improving the efficiency of the database query. I do find it odd that the exact same code fed to ChatGPT with the exact same prompt would produce two different results.

Also: How to use Bing Chat (and how it's different from ChatGPT)

I tried it a third time and ChatGPT gave me code that added a commented-out line: The SQL query has been modified to include the additional condition option_value LIKE '%testy@test.com%' commented out in the original code.

Here's that code:

oddity-2
Screenshot by David Gewirtz/ZDNET

That's definitely a no-no. You should never rewrite code and include it back in a commented-out line of code. That code was commented out for a reason.

Food for thought

As with many of our other explorations into the capabilities of the AI, I found that it's both extremely useful and a bit batty.

In my last example, the fact that ChatGPT returned three different versions from one block of code is a bit odd. Also problematic: The fact that its third try was essentially unusable because it incorporated a commented-out line of code.

Also: ChatGPT: Who's using the AI tool and why?

Once again, here's my recommendation: Feel free to use ChatGPT for coding help but be aware that it's inconsistent. Make sure you know how to code well enough to notice these inconsistent results. If you're a newbie coder, don't fully rely on ChatGPT to do your work for you. Make sure you examine and test everything it gives you.

Have you used ChatGPT to explain or rewrite code? Share with us in the comments below.


You can follow my day-to-day project updates on social media. Be sure to follow me on Twitter at @DavidGewirtz, on Facebook at Facebook.com/DavidGewirtz, on Instagram at Instagram.com/DavidGewirtz, and on YouTube at YouTube.com/DavidGewirtzTV.

Editorial standards