Will ChatGPT replace .NET developers?

Published: Sunday 18 December 2022

ChatGPT is a new free artificial intelligence model from OpenAI.

It is trained so it can interact in a conversational way.

C# coding challenges

C# coding challenges

Our online code editor allows you to compile the answer.

Using the Playground, we asked it to build an ecommerce application using ASP.NET Core Web API. The answers it gave included a high-level overview of how to build it, and getting it to write code for the classes, controllers and unit tests.

Could this model be a threat to the job as a .NET developer?

Build an ecommerce system

The first thing we asked ChatGPT to do is to build an ecommerce system with orders, customers and products in ASP.NET Core Web API.

It gave us a high-level overview of how to build it, which included:

  • Installing .NET Core SDK
  • Creating a new ASP.NET Core Web API project in Visual Studio
  • Defining models for orders, customers and products
  • Set up a database
  • Configuring Entity Framework
  • Implementing logic for controllers
  • Testing the API endpoints
Build an .NET ecommerce system with ChatGPT

Build an .NET ecommerce system with ChatGPT

A good start! But what about writing code?

Writing the classes for the models

Next, we asked it to write the code for the classes of the model. This included a class for OrderCustomer and Product.

It did well in writing the syntax as well as coming up with some common properties for the entities. But, there were some questions around how it stored details about the items ordered.

Rather than creating a separate entity for order items, it tried to store it all in the Order class.

ChatGPT writing code for a .NET class

ChatGPT writing code for a .NET class

Let's see if we can separate the order items into a separate class.

Separate order items into it's own class

The results were a lot better when we asked it to separate the order items out to it's own class.

It created a new OrderItem class, and added the ProductIdQuantity and Price into that class.

The Order class then had a list of order items so it was able to have that relationship between the two.

ChatGPT rewriting code for a .NET class with better properties

ChatGPT rewriting code for a .NET class with better properties

We noticed it created the order items as a List. We asked it why it didn't use an IList?

Why use List over IList?

It gave us a very detailed explanation on the benefits of using List over IList.

This included giving a concrete implementation of all methods and properties in the IList interface, and storing the elements internally. This means that element access is faster and memory overhead is lower compared to other implementations.

However, it did give some reasons for using IList, such as using it for a mock implementaiton, or passing a list to a method that expects an IList interface.

ChatGPT answering why you should use List over IList

ChatGPT answering why you should use List over IList

Writing controllers

With the entities created, the controllers was the next thing we got it to code.

With the Order entity, it wrote a controller with common CRUD methods needed, such as reading all orders, or a single order, as well as creating, updating and deleting an order.

To reference the services that populate the data for this controller, it created a unspecified interface. The IUnitOfWork seemed to have methods that supplies the input and output of the order data.

However, there was no sign of how this interface was constructed in the final result.

Writing an ASP.NET Core Web API controller in ChatGPT

Writing an ASP.NET Core Web API controller in ChatGPT

Another thing was when it started to write the controller for the Customer, it suddenly stopped half way through.

It might be because of the load of the server, or maybe it was unable to complete the task. Maybe we were asking it for too much?

ChatGPT stops half way through writing a .NET controller

ChatGPT stops half way through writing a .NET controller

We did ask it to try it again, but it stopped at the same place. So there are definitely some limitations there.

Writing tests

We asked it to write unit tests using XUnit.

It had some good use of mocking by creating a mock object of our IUnitOfWork interface. Afterwards, it went through and wrote some good unit tests for our controllers. It called the endpoints in the controllers and set an assert as to what type should be returned.

Once again though, it got stuck half way through writing the tests in a similar fashion to last time. It looked like we used ChatGPT at the wrong time of day.

ChatGPT gets stuck when writing XUnit tests

ChatGPT gets stuck when writing XUnit tests

Server issues

At this point, we were getting a number of errors when asking it questions. Errors ranged from the server experience an error while processing the request, to us sending it too many requests.

Server issues with ChatGPT

Server issues with ChatGPT

At this point, we decided to conclude our experiment.

See ChatGPT in action

Watch our video where you can see us using ChatGPT to build an ecommerce application with ASP.NET Core Web API.

In-addition, we created an ASP.NET Core Web API in Visual Studio and see if the code that ChatGPT generates actually compiles.

Is a .NET developer's job at risk?

At the moment, ChatGPT can only really be used as a guide. It takes a .NET developer to question the code quality that it writes.

In-addition, there are a number of occassions were it was unable to complete the answer to a question that was asked. This was particular with lengthy pieces of code.

In our example, it was felt that it was unable to actually run the code, particularly as it created an unknown IUnitOfWork interface which would never compile.

So for now in our opinion, a .NET developer's job is safe.

But this is a huge step towards writing code using artificial intellegence. Where will this technology be in 5-10 years time?