DEV Community

Cover image for How to solve coding issues using ChatGPT
JS for ZenStack

Posted on • Updated on

How to solve coding issues using ChatGPT

The Issue I need to solve

Simply put, I need to check if a node package is installed in runtime. The reason is that I’m building a new toolkit ZenStack, that supercharges Prisma with a powerful access control layer and API generation. Since it is built above Prisma, when running the CLI provided by ZenStack, it must check whether Prisma has already been installed. Otherwise, it would prompt the user to install Prisma first.

Get the answer

Let’s try to ask ChatGPT directly:

gpt-1

Cool! The answer looks compelling. Problem solved!

The side effect

It does work, but it brings one side effect. In my CLI project, I use async-exit-hook to handle uncaught errors like the below:

// You can hook uncaught errors with uncaughtExceptionHandler(), consequently adding 
// async support to uncaught errors (normally uncaught errors result in a synchronous exit).
exitHook.uncaughtExceptionHandler(err => {
    console.error(err);
});
Enter fullscreen mode Exit fullscreen mode

After the require(’prisma’) is executed successfully, which means the Prisma is there, if later in other code you throw any uncaught exception, the uncaughtExceptionHandler would never be triggered. But if require any other existing module like require(’uuid’), then the uncaughtExceptionHandler would still be triggered as before.

Although I haven’t found the real cause, from the above It looks like some code is running when Prisma is imported. I’m not sure whether Prisma provides any flag to control it; even if it does, it still doesn’t sound like a clean solution to me because it is like Pandora's box. You won’t know what gets executed.

Clean solution

The most direct way to avoid a side effect is to use a separate process. So how would you do that if the job is assigned to you? I will use the npm command. So let’s ask the omniscient again:

gpt-2

This looks like a clean solution to me.

Push harder

As good developers, we should always try to think further. What if the npm command is not installed or is broken? If we know how npm finds the module, we could do that ourselves.

Let’s ask again:

gpt-3

As the npm command obviously won’t execute any code for the package, it does find the name and version as the answer specified:

It will display the names and versions of all the installed package

So, where does it get the version information? Of course, from package.json file. Actually, you can easily verify it by creating dummy-package folder under node_modules, and then creating the package.json file with the below content:

{
    "name": "dummy-package",
    "version": "9.9.9"
}
Enter fullscreen mode Exit fullscreen mode

Then after running npm list --depth=0 dummy-package , you could see the package info:

helloworld@1.0.0 /Users/jiasheng/branch/helloworld
└── dummy-package@9.9.9 extraneous
Enter fullscreen mode Exit fullscreen mode

Therefore, instead of requiring the module like the original solution, we could change it to require the package.json file like:

const prisma:any = require('prisma/package.json')
Enter fullscreen mode Exit fullscreen mode

Not only does it get rid of the side effect, but also you could get more information for that package, like version, etc.

Final Words

Anyway, you can see ChatGPT really could help us a lot, even writing the code for us, but it is us who actually think and resolve the issue thoroughly. Coming to the thought that AI would replace developers, I think Google should worry about that rather than us. 😉


Follow up

There is one comment left by Anthony that brings up one “side effect” of using ChatGPT “while ChatGPT arguably helped you solve your problem, you still failed to actually learn what was going on”, which I can’t agree more. Therefore, I investigated a little bit about the source code of Prisma, it turns out that the side effect of the first solution ChatGPT given is that when Prisma is required, it registered unhandledRejection event handler in the below line:

https://github.com/prisma/prisma/blob/main/packages/cli/src/bin.ts#L57

process.on('unhandledRejection', (e) => {
  debug(e)
})
Enter fullscreen mode Exit fullscreen mode

The exception thrown later is in the Promise handler, so it got intercepted by this event handler. Therefore, another way of fixing it is to also register unhandledRejection event handler using exitHook like below:

// You can hook unhandled rejections with unhandledRejectionHandler()
exitHook.unhandledRejectionHandler(err => {
    console.error(err);
});
Enter fullscreen mode Exit fullscreen mode

But I would still stick to the required package.json solution as it has no side effects at all. 😁


P.S. We're building ZenStack: a toolkit that supercharges Prisma with a powerful access control layer and unleashes its full potential for full-stack development

Oldest comments (35)

Collapse
 
andrewbaisden profile image
Andrew Baisden

ChatGPT has so much potential I can't wait to see what else it will be capable of doing in 2023.

Collapse
 
jiasheng profile image
JS

me too! 😎

Collapse
 
jiasheng profile image
JS

Exactly. And we need chatGPT to work for us. 😄

Collapse
 
mitch1009 profile image
Mitch Chimwemwe Chanza

Practically speaking, All blogs I have read so far about ChatGPT even the ones from the very people we consider sane in our industry semm to be not factual. Yes chatgpt can do a lot of magic but it needs someone who knows what he/she is doing. Don't expect someone who has never done coding before to become a rocket scientist because of chatgpt. One boring thing I have read over the past two weeks also was the comparison of chatgpt to other ai models like palm which as of now no one has ever used in public. As if that is not insane enough some youtubers are posting things that put fear in developers minds. Take heart guys they will need developers even if an ai system becomes sentient it cannot be human. That's my take.

Collapse
 
ymc9 profile image
ymc9

The promoted fear of ChatGPT can be a useful filter to block people who're not psychologically strong enough to enter this profession ...

Collapse
 
jiasheng profile image
JS

Interesting idea. Sounds like it could improve the overall quality of developers from one more perspective. 😄

Collapse
 
jiasheng profile image
JS

Speaking of YouTubers, I think that’s part of the nature of media, exaggerating to exploding. Here Dev.to is more objectives as it’s really to Dev. 😄

Collapse
 
matthewbdaly profile image
Matthew Daly

I've done enough tinkering with it that I feel reasonably confident talking about what it can and can't do, and I'm confident it's not going to replace me. At this point it's basically not much more than a smarter search engine - it's better than Google at tracking down specific examples of a particular thing, and you can refine those examples progressively to get it closer to what you want, but you still need to understand those examples to be able to implement them. It does make more of the job about composition and editing than about writing code, but you still need to be able to understand what's going on, though.

I would be far more concerned about my job if it was the poorest sort of content creation though. Given how bad those sort of blog posts tend to be, and the fact that ChatGPT is noticeably better at producing them, I would not be surprised if those sort of poor quality blog posts are overwhelmingly written by AI in a year.

Collapse
 
cppshane profile image
Shane Duffy

they will need developers

Sure, but they will need less developers

Collapse
 
giampaolo44 profile image
Giampaolo Ferradini

Mmm... I suspect coding is like health care: the more the available offer, the larger the demand.
Consider how much manual work is still performed, and how much could be automated. It'll take us three generations to fill those requirements, and another two to redo stuff that was poorly done.

Collapse
 
paterne81 profile image
Paterne G. • Edited

Je suis totalement d’accord avec vous. Quelque soit alpha nous, les développeurs ne disparaîtrons pas. Bien vrai que ChatGPT aide énormément il a toujours besoin qu’on lui donne des directives bien définies pour pouvoir nous étaler nos besoins.
La seule question qui me traîne dans l’esprit 🤔 en ce moment : Pourquoi Google serait-il craintif ?
Sur ceux, je vous souhaite une bonne et heureuse année 2023.

Collapse
 
jiasheng profile image
JS • Edited

Because I think there would be more and more cases in that people would directly ask questions for ChatGPT instead of searching in google like what I did in this post. 😄

However, I guess Google would probably provide a similar service in the future as Larry Page said “we are really making AI” before google’s IPO. 😉

Thread Thread
 
paulchase profile image
Paul Ajonye

I heard they have dedicated a team to create something similar 🙂.

And I'm guessing Google tactics would be to make it free forever and start placing Ads later.

Thread Thread
 
jiasheng profile image
JS

😂 That does sounds like Google’s tactics.

Collapse
 
killianellie1 profile image
Killian Ellie

Yes google should worry about that 🤣
Did you try chatgpt x google search engine. It's developed by me. Try it out and let's me know what you think 😄😄
Try it

Collapse
 
jiasheng profile image
JS • Edited

Nice tool! You are actually threatening Gooogle. 😁

Collapse
 
killianellie1 profile image
Killian Ellie

hahaha

Collapse
 
sorydi3 profile image
sorydi3

YouTubers are clickbait hunters.

Collapse
 
jiasheng profile image
JS

I’m aware of this recently, as I see some has to literally put (Not Clickbait) in the title 😂

Collapse
 
soulfiremage profile image
Richard Griffiths

In other words, careful intelligent use turns it into another developer amplifier rather than a developer replacer.

I like it.

Collapse
 
jiasheng profile image
JS

Nice one, it’s concise and comprehensive!👍

Collapse
 
monimartin22 profile image
Moni Martin

There is an extension that can assist you in displaying ChatGPT responses alongside the search results from Google, Bing, and DuckDuckGo. Maybe you should try it: https://chrome.google.com/webstore/detail/chatgpt-for-search-engine/feeonheemodpkdckaljcjogdncpiiban/related?hl=en-GB&authuser=0

Collapse
 
tschallacka profile image
Tschallacka

I tried te debug a lua app concerning merging two lists, all the code the chat gave seemed logical, but variable names, logic, etc.. was all wrong in a way that led to weird bugs.

It can give hints, but often it will reference constants from an entire different language or app

Use it as a rubber duck, not to generate your code.

Collapse
 
jiasheng profile image
JS

exacly!

 
krzysiekhan profile image
Krzysztof Handzlik

i asked same question and answear was correct

Thread Thread
 
jiasheng profile image
JS • Edited

The answer is probabilistic. I actually have no doubt that AI could answer this kind of definite question correctly as evolving.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

It took me 5 days to steer it to making the blender script in needed and during that time it needed a lot of corrections from me, it didn’t know what was correct and modern and wasn’t aware of API changes until I told it. It also looped around a solution quite a few times or flat out misunderstood this visual task… it’s still incredible

Collapse
 
jiasheng profile image
JS

I like your attitude, It’s all about how we can utilize it. 😄

Collapse
 
anthonywhitaker profile image
Anthony Whitaker

What I find interesting is that while ChatGPT arguably helped you solve your problem, you still failed to actually learn what was going on. The reason your unhandledError hook didn't fire has nothing to do with prisma potentially having a flag or doing something odd. The solution ChatGPT provided involves handling the module not found exception... therefore by definition your unhandled hook is never going to fire. The whole subprocess hack that you're calling a _Clean Solution _ is completely unnecessary and misleading to others who may unfortunately stumble upon this article.

A better approach to become an adequate developer... always aim to understand a situation, then solve it. Searching for the solution seems like the fast path but in the end you're left with buggy, ineffective code that kinda/sorta sometimes works... all because you don't truly understand what's happening.

It's like going to the doctor saying your leg hurts. The doctor gives you morphine and all the sudden, you feel better! Problem solved!!! Except your leg was broken and you needed a cast. You can't just treat the symptom as a doctor, or a developer.

That said, maybe ask ChatGPT about unhabdled vs handled exceptions and understand how, why, and when the unhandled exception filter would get executed. Best of luck

Collapse
 
jiasheng profile image
JS

Hi Anthony, it seems that l didn’t specify the problem clearly, thanks for pointing it out. I have updated the corresponding content to make it clear.

As you can see I still believe it’s caused by some code of Prisma running when importing the Prisma module because using any other existing module would be fine.

Anyway, I think you are right that I still haven’t truly understand what’s happening under the hood 😂. I think it requires some investigation of the source code of Prisma. I will take a look later and update the result here.

Collapse
 
jiasheng profile image
JS

Updated in the end of the post.

Collapse
 
elr0berto profile image
elr0berto

Chatgpt + Unknown breakthrough = everyone is doomed. The unknown breakthrough could be anything.. something that makes us think about everything differently.. some new approach... Could happen any day...could take decades..

Collapse
 
eugenman profile image
Eugen

You order from menu, but have no idea how it was cooked.
You didn't go all the way to solve the problem.

ChatGPT doesn't help you, it harms you. He does your work for you. So maybe hire him instead of a lazy developer?

Collapse
 
eugenman profile image
Eugen

Yes, it's a tool, not a replacement.

Collapse
 
j4acks0n profile image
j4acks0n

Doesnt help at all instruction not clear....