How to use chatGPT to write code snippet

Recently, I heard many people saying how they used chatGPT to help them write code. So I decided to try it myself.

Simple task

I decided to ask chatGPT to write a small helper function that has specific requirements.

A perfect example immediately came up to my mind. This is a util function I needed in the ETHSF hackathon. I assigned it to one of my teammates and it took a few back-and-forths commits to get it right. The time between the first and last commit is 4 hours.

So let’s assign the same task to chatGPT. My initial message to it:

/*
arr is an array of strings. examples:
[
  '--syncmode=full',
  '--datadir=<datadir>',
  '--nodekey=<nodekey>',
  '--port=<port>',
  '--http',
  '--http.addr=0.0.0.0',
  '--http.port=<httpport>',
  '--networkid=<networkid>',
  '--mine',
  '--miner.threads=1',
  '--miner.gasprice=1',
  '--unlock=<unlock>',
  '--password=<password>',
  '--allow-insecure-unlock'
]
[ '<httpport>:<httpport>', '<port>:<port>' ]
[ '<datadir>:<datadir>' ]
pairs is an object to specify how to do the replacement. example:
{
  '<datadir>': '/path/to/datadir',
  '<nodekey>': '/path/to/nodekey/file',
}
example return:
[ '<datadir>:<datadir>' ] => ['/path/to/datadir:/path/to/datadir']
should return a new array of strings that applies the replacements
*/
export function replaceStringArray(arr, pairs) {
}

please implement the `replaceStringArray` function based on the requirement above it

Then I got the response:

It also gave some test input and output to help me verify the correctness:

However, there is a bug and I pointed out:

chatGPT tried to fix it:

Unfortunately, chatGPT misunderstood the requirement. It’s worth noting that it’s my mistake that I didn’t specify clearly that it should “replace all” in the requirements. So I clarified it and this time it gave the right solution:

However, I noticed that it differs from my solution where replaceAll is used. But it uses regular expression. So I asked it:

chatGPT nicely explained its choice and helped me learn this new knowledge.

Then I asked it to optimize the code:

And chatGPT gave a few suggestions:

Conclusions

chatGPT is an effective tool for quickly finding solutions to coding problems, saving developers valuable time and effort.

However, if you don’t specify the requirements clearly, it might misunderstand and give solutions that have bugs in it.This also implies that you should know how to solve the coding question yourself and verify the solution carefully. It’s dangerous to blindly trust the solution given by it.

It’s also worth noting that if you find the bug and described it clearly to chatGPT, it can fix it.

It’s also interesting that if you have a different solution in mind, you can ask chatGPT why it’s not chosen. You might be surprised and learn something new.

You can also ask chatGPT to optimize the code. You can even repeat that for a few times until you feel the code is too hard to maintain.

Subscribe to bap2pecs
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.