ChatGPT can write code better than me 100% ( OMG! Its Awesome)

ChatGPT can write code

I’m going to introduce you to the brand new AI chatbot released by open AI called Chat GPT. It is an AI chatbot trained to effectively have dialogue so it can keep context and keep answering your questions, knowing the previous context. And it has a cut off point on its knowledge in the end of 2021.

So if something after the end of 2021 has been released, its knowledge is very limited. Now, the interesting thing is that one of the suggestions on the examples over here is how do I make an Http request in JavaScript?

So I’m going to put it to the test and see what it can do for me as a Csharp developer. I’m going to get it to write some code, try to optimize some code and see how far it can go and how much it understands, maybe get it to write some unit tests. ChatGPT can write code

Now, this is not a blind reaction. I actually had a two and a half hour playing around yesterday. But from what I’ve seen, I’m very impressed and I can confidently say that this will give Google and Stack overflow a run for its money.

And from what I’ve seen until now, I believe it’s even better than GitHub compiled. And I’m going to start simple and say write a C sharp method that accepts a guide as a string and returns a base 64 version of the guid. So let’s go ahead and ask it.

ChatGPT can write code

And the moment I say it, it starts writing the method. So two base 64 good string good here, and then it returns the thing. And this is clearly valid Csharp. You can take this and it works. ChatGPT can write code

Now, there’s one thing I actually do not like, and that is that it is using guide.Parse, and guide.Parse can throw if the input is invalid.

So I can say, please also handle invalid input. So if the guide is not a valid input and it understands that, yes, if you don’t have a valid guide, then I should use Tryparse.

And it knows that if the string is not a valid good, return an empty string and then I can give it more context on how I want it to handle this thing. So maybe throw or return something else. It’s up to me.

Now, there’s one thing I don’t like. It uses this two bite array here and we’re wasting memory allocations. So I can ask it, can you optimize this code to not have heap allocations?

ChatGPT can write code

Just go straight away, and not only go straight away, it says, oh yeah, I’m going to use span of byte and I’m going to stack allocate 16 bytes over here to write the bite of the good and then convert it to by 64.

This is actually things you would do if you wanted to optimize it. It’s very, very impressive. ChatGPT can write code

Now let’s take it a step further. I’m going to say that please give me a method that went this morning you return good morning. When this afternoon You return good afternoon. And when it’s evening you return good evening.

So write a c sharp method that returns good morning, When it’s morning, good afternoon, When it’s afternoon, good evening.

ChatGPT can write code

When it’s evening, immediately it starts writing and it goes get greeting. It gets the current hour getting datetime now. And it says if the hour is from zero to twelve, then good morning. If it’s twelve to 16, good afternoon else good evening. Very, very accurate.

It says this method is using the datetime now property to get the current date and time and then extracts the hour. So it explains the code.

But what I want to tell it now is I wanted to write unit tests for this method. So please write unit tests for this method immediately. It goes ahead and it writes tests. ChatGPT can write code

ChatGPT-can-write-code-1

So test morning, test afternoon, test evening, create a new DateTime. And here’s where the insane thing comes in.

It actually parameterized get a greeting. Because if you remember get greeting didn’t have a parameter. Now DateTime is a parameter in the greeting itself.

Because we need to inject the DateTime as a dependency because otherwise this would not be unit testable.

So I can say, can I see the new code or get greeting? And it’s going to go ahead and show you the new chains. Now that it accepts datetime, it is so good

let’s see how far it can go. So obviously now it’s using any unit.I don’t use any unit. So I wanted to use X unit as my test library.

I also wanted to use what I consider to be my standard way of writing a test name which is test method underscore should do something underscore when something happened and then I’m using fluent assertions for my assertions. So let’s go ahead and write that. Can you use X unit for the test instead?

Also use fluent that’s so good assertions for assertions. And since I prefer using the naming pattern method name underscore when should do something underscore when something happened, then use that pattern instead.

Okay, I added an extra F in the end but it understood it was a type when it went ahead and it goes ahead and it does it. So it now changed to use X unit affluent assertions. ChatGPT can write code

ChatGPT can write code

It uses the fact correctly arranged act as said correct parameter over here and it says get greeting and the name is get greeting.

The method name should return good morning when it is good morning. It’s so good. I can’t actually believe this. How it goes in depth and explains and it says, oh, I use the naming pattern because basically you told me. And yeah, I’m using Fluent assessions correctly.

It’s so impressive. And it doesn’t even end there. So I could say, for example, now use the theory feature of x unit instead. So measure all three tests into one that uses the theory and has inline data to represent the DateTime and the items. It knows.

It just knows. And it changed the text as well. So should return correct greeting when given time of day. I can’t believe this. Okay, let’s see how far we can go with this then.

So I’m going to reset. I’m going to say create an ASP.net core six API that returns the weather or a given city using the Open Weather Map API for its data. Let’s see what it can do. It goes okay, straight away you download net there. Net six specifically. ChatGPT can write code

ChatGPT can write code

That’s the right location, by the way. Then it gives you details on every step of the way you inject the Open Weather Map API client. So it did that very efficiently. Did not have the logic in here. Okay, I want to see the code that it would write for that thing.

So show me the code for I open weather Map API client and the implementation which is open weather Map API clients. Let’s see what it does. This is super impressive.

ChatGPT can write code

So it explains that this is an interface explaining how I can call this API. So the contract and then the implementation is here and it is injecting the client, which is a best practice in Aspirate core. ChatGPT can write code

Most people do this wrong and this thing could do it correctly. This is insane.

So it’s going to use the a specific client. I’m so impressed. It knows it needs a key and it’s injecting the key from the constructor writes comments to explain what it’s doing. So the API URL is here. This is all correct code valid C sharp that you would write it deserializes using.

I wouldn’t want to use Newtons of JSON. But it’s not wrong. It’s just you can use system text adjacent. This is nuts. Okay, so we have that.

Can I push it even further? Let’s say add resilience by adding poly in the project. Use it to implement retries and circuit breaker. Let’s see what it does. Now, sometimes it takes longer because it’s so heavily used by so many people around the world. ChatGPT can write code

ChatGPT can write code

So it can take for a second to start typing sometimes. And I am also getting rate limited. So just know that it’s not the thing being slow. It is just that so many people use it that it is being slow. Here you go and it goes ahead and add it.

So to add the poly library into the project, it just gives you all the instructions. It tells you use the policy handle except and it knows to cache it so it’s not created every single time. Oh, my God. I’m sure I can tell it to add exponential back off and a jitter and will all work.

I am so impressed with this thing. Wow. And it will just keep going. What I want to tell you to do after this, and of course I can say continue, and we’ll continue writing the thing. It’s so interesting.

Now it’s your turn, use it and enjoy this free AI Bot and share your experience

Rate this post

Leave a Comment

error: Content is protected !!